用例:-我正在开发一个 Web 应用程序,该应用程序允许创建 HTML 模板并将其发布在 amazon 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.希望我清楚
另一个服务器块:
# 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;
}
}
我也知道我将不得不在域的别名中进行DNS更改。我想我将不得不在模板域名的 CNAME 下添加app.mysite.com
?如果错误,请更正。
谢谢你的时间
发现这是文档的一部分,但花了一段时间才理解。我必须在第二个服务器块中添加一个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 ...
}
我认为您需要从中删除$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;
只是一个猜测