注:转自nginx实现不同域名映射到一个端口转发到不同路径
在一个服务器中,两个不同的域名a.site.com和b.site.com访问80端口,转发到不同的地址。若通过ip访问,默认转发至pic.mydomain.com对应的地址
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name pic.mydomain.com;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
add_header backendIP $upstream_addr;
add_header backendCode $upstream_status;
proxy_pass http://mysvr;
proxy_set_header Host myoss.oss-cn-shenzhen-internal.aliyuncs.com;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
## one port,mutil domain,access difference route
server {
listen 80;
server_name www.mydomain.com;
location / {
proxy_pass http://localtomcatserver/mywebsite;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location /50x.html {
}
}
upstream mysvr {
server oss-cn-shenzhen-internal.aliyuncs.com;
}
upstream localtomcatserver {
server localhost:8080;
}
这里的每个地址都是反向代理,如果需要,可以改为不同的root即可
网友评论