配置nginx服务器以处理来自多个域的请求

Kil*_*Bug 2 php nginx amazon-s3 proxy-server


使用案例: -我正在开发一个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;
Run Code Online (Sandbox Code Playgroud)

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

    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;
      }
 }
Run Code Online (Sandbox Code Playgroud)

}

加载所有网站

    include  /etc/nginx/conf.d/*.conf;
Run Code Online (Sandbox Code Playgroud)


更新,因为我不够清楚: -
我的问题是我如何处理配置文件中的两个域.我的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;
   }
}
Run Code Online (Sandbox Code Playgroud)

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

感谢您的时间

Kil*_*Bug 7

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

app.mysite.com

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

  # config for app.mysite.com

}
Run Code Online (Sandbox Code Playgroud)

其他网站

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 ...

}
Run Code Online (Sandbox Code Playgroud)