2


用例:-我正在开发一个 Web 应用程序,它允许创建 HTML 模板并将它们发布到亚马逊 S3 上。现在发布我使用 nginx 作为代理服务器的网站。代理服务器的作用是,当用户输入网站 URL 时,我想确定如何检查请求是否来自我的应用程序,即app.mysite.com(这不会改变)并将其路由到 apache 以进行常规访问,如果它来自其他域,如常规 URL www.mysite.com(这需要动态处理。可以是随机的)它会转到托管模板的 S3 存储桶。

我目前的配置是:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

events {
  worker_connections  1024;
}


http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

charset   utf-8;
keepalive_timeout  65;
server_tokens       off;
sendfile            on;
tcp_nopush          on;
tcp_nodelay         off;

用于捕获未定义主机名的默认服务器块

    server {
      listen 80;
      server_name  app.mysite.com;

       access_log  off;
       error_log off;

      location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        Host            $host;
        proxy_redirect          off;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
        client_max_body_size 10m;
        client_body_buffer_size 128k;
        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
      }
 }

}

加载所有站点

    include  /etc/nginx/conf.d/*.conf;


更新,因为我不够清楚:-
我的问题是如何处理配置文件中的两个域。我的 nginx 是 EC2 实例上端口 80 上的代理服务器。这也托管了我在不同网络上的 apache 上运行的应用程序端口。因此,对我的应用程序的任何请求都将来自域app.mysite.com,并且我还想代理存储桶内的 S3 上的托管模板。因此,sites.mysite.com/coolsite.com/index.html如果有人点击coolsite.com我想将其代理到文件夹sites.mysite.com/coolsite.com/index.html而不是 app.syartee .com.Hope 我很清楚
其他服务器块:

     # Server for S3
server {
    # Listen on port 80 for all IPs associated with your machine
    listen 80;

    # Catch all other server names
    server_name _; //I want it to handle other domains then app.mysite.com

    # This code gets the host without www. in front and places it inside
    # the $host_without_www variable
    # If someone requests www.coolsite.com, then $host_without_www will have the value coolsite.com
    set $host_without_www $host;
    if ($host ~* www\.(.*)) {
       set $host_without_www $1;

    }

    location / {
        # This code rewrites the original request, and adds the host without www in front
        # E.g. if someone requests
        # /directory/file.ext?param=value
        # from the coolsite.com site the request is rewritten to
        # /coolsite.com/directory/file.ext?param=value
        set $foo 'http://sites.mysite.com';
       # echo "$foo";
        rewrite ^(.*)$ $foo/$host_without_www$1 break;


        # The rewritten request is passed to S3
         proxy_pass http://sites.mysite.com;
         include /etc/nginx/proxy_params;
   }
}

另外我知道我必须在域的cname中进行DNS更改。我想我必须app.mysite.com在模板域名的CNAME下添加?如果错误请更正。

感谢您的时间

4

2 回答 2

7

发现这是文档的一部分,但需要一段时间才能理解。我必须在第二个服务器块中添加一个default_server属性才能使其余域正常工作。
如果我们配置 server_name,nginx 将根据与域匹配的配置块提供内容。

app.mysite.com

server {
  listen 80;
  server_name  app.mysite.com;

  # config for app.mysite.com

}

其他网站

server {
  listen 80 default_server; #To handle domains apart from the fixed domain(In my case app.mysite.com)
  server_name _ ;

  # config for coolsite.com, anotherdomain.com ...

}
于 2013-11-18T06:01:35.707 回答
1

我认为你需要$foo

rewrite ^(.*)$ $foo/$host_without_www$1 break;

使用 foo 你会通过

http://sites.mysite.com/coolsite.com/directory/file.ext?param=value

proxy_pass http://sites.mysite.com;

只是猜测

于 2013-11-13T08:44:58.160 回答