使用案例: –我正在开发一个Web应用程序,它允许创buildHTML模板并在亚马逊S3上发布它们。现在将我使用nginx作为代理服务器的网站发布出去。 代理服务器的function是,当用户input网站URL时,我想确定如何检查请求是否来自我的应用程序, 例如app.mysite.com (这不会改变),并将其路由到apache以进行常规访问,如果它来自一些其他的域像一个普通的URL www.mysite.com(这需要dynamic处理,可以是随机的),它转到承载模板的S3存储桶。
我目前的configuration是:
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;
更新,因为我不够清楚: –
我的问题是如何处理configuration文件中的两个域。我的nginx是一个EC2实例端口80上的代理服务器。这也承载我的应用程序运行在不同的端口上的Apache。所以任何请求来我的应用程序将来自一个域名app.mysite.com ,我也想代理S3上的托pipe模板,这是在一个桶里面说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 # Eg 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更改。我想我将不得不在模板域名的CNAME下添加app.mysite.com ?请纠正,如果错误。
感谢您的时间
如果你configuration了server_name,nginx会根据匹配域的configuration块来提供内容。
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 ... }