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

时间:2019-01-02 08:53:54

标签: 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(否)

代替

  

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?

1 个答案:

答案 0 :(得分:0)

您要将默认规则仅覆盖服务器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;
    }
}