我在 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 (NOK)
代替
mysite.com/css (OK)
我也使用这个配置,这意味着重定向所有 exept 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;
}
}
关于tomcat - 如何在 nginx 中配置子域以指向同一 tomcat 主机中的特定 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54003503/