tomcat - 如何在 nginx 中配置子域以指向同一 tomcat 主机中的特定 html 文件

标签 tomcat nginx

我在 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/

相关文章:

tomcat - 无法使 tomcat 记录基本 404 类型错误消息

networking - HAProxy 动态服务器地址

Prod 上的 Docker 微服务和 Nginx

django - Nginx - 如何使用上游模型提供静态文件

docker - 无法在Dockerfile中运行多个COMMAND,最后一个被忽略

javascript - Vuejs 应用程序显示无效的主机 header 错误循环

java - 为什么 serverSocket.accept() 只对一个请求执行多次

java - 没有server.xml可以运行tomcat服务器吗?

jakarta-ee - 无法加载类 [com.sun.jmx.mbeanserver.RepositorySupport] 以检查 @HandlesTypes

tomcat - 谷歌计算引擎 Windows 服务器端口