我在tomcat上有一个网站,例如www.mysite.com:2121和nginx点(作为代理),如下所示:
mysite.com => www.mysite.com:2121
www.mysite.com => www.mysite.com:2121
我想创建一个像ui.mysite.com这样的子域,以指向mysite.com:8080/index_ui.html这样:
ui.mysite.com => mysite.com:8080/index_ui.html
我在nginx.conf中尝试此操作:
server {
listen 80;
server_name mysite.com www.mysite.com;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass htt://www.mysite.com:2121;
}
}
server {
listen 80;
server_name ui.mysite.com ;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass htt://www.mysite.com:2121/index_ui.html;
}
}
此代码仅适用于HTML,不适用于CSS和JS,因为这些文件位于同一主机中,并且浏览器在以下位置查找它们
ui.mysite.com/css(否)
代替
mysite.com/css(确定)
我也使用此配置,这意味着重定向所有实例index_ui.html:
rewrite ^(?!^(/index_ui.html)$)/.*$htt://www.xrdini.com/$1 permanent;
这项工作效果更好,但是效果不佳,因为它对SEO不利,并且使重定向用户转到
mysite.com/index_ui.html
不
ui.mysite.com/index_ui.html
如何配置Nginx和/或Tomcat?
最佳答案
您要将默认规则覆盖为仅服务器index_ui.html,需要为根目录设置特定规则,并使用默认规则照常反向代理:
server {
listen 80;
server_name ui.mysite.com ;
# Location block for specifically URL /
location = / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass htt://www.mysite.com:2121/index_ui.html;
}
# Location block for all other URLs
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass htt://www.mysite.com:2121;
}
}