开发者社区> 问答> 正文

Nginx二级域名与Tomcat绑定问题:报错

问题情景:

当我访问hg.test.com的时候,Nginx 需将二级域名需转发到Tomcat的 http://localhost:10010/test/index/  意思就是要将不同的二级域名转发该tomcat的不同url地址。且不影响JSP页面中的路径。

现在的问题是,如果使用代理proxy_pass http://localhost:10010/test/index; 转发,那么程序中所有使用的相对路径前面都会加上test/index.

这种情况该怎么写? 哪位兄弟处理过这种情况,写个示例,不胜感激!

server {
  listen 80;
  server_name hg.test.com;
  location ~ \.(htm|html|gif|jpg|jpeg|png|ico|rar|css|js|zip|txt|flv|swf|doc|ppt|xls|pdf)$ {
            root /var/data/test/webapp;
         access_log off;
            expires 24h;
        }
  location / {
    proxy_pass http://localhost:10010/;
    include proxy.conf;
  }
}

展开
收起
kun坤 2020-06-09 22:28:02 666 0
1 条回答
写回答
取消 提交回答
  • nginx 做不了这个的,访问 nginx 之前的 url 后缀要跟 nginx 转到 tomcat 后的 url 后缀一致

    例如 http://www.oschina.net/question -> http://localhost:8080/question

    ######

    这个应该是要用到url重写吧。

    ######可以的. 请参考:
    http://wiki.nginx.org/NginxChsHttpProxyModule
    ######

    应该是 可以的吧,只要域名和应用是对应的

    server {
     listen 80;
      server_name   ~^(hg\.)?(?<domain>.+)$;
    
      location ~ \.(htm|html|gif|jpg|jpeg|png|ico|rar|css|js|zip|txt|flv|swf|doc|ppt|xls|pdf)$ {
    	root /var/data/$domain/webapp;
      access_log off;
      expires 24h;}
      
      location =/ {
      	rewrite ^/ http://$hostname:$server_port/$domain/index;}
    
      location / {
        proxy_pass http://localhost:10010/;
        include proxy.conf;}
    }
    没有测试######

    引用来自“红薯”的答案

    nginx 做不了这个的,访问 nginx 之前的 url 后缀要跟 nginx 转到 tomcat 后的 url 后缀一致

    例如 http://www.oschina.net/question -> http://localhost:8080/question

    你这个我做过,但是CSS有问题,子域名不能访问其他目录。

    我刚还提出了问题

    http://www.oschina.net/question/209667_32820

    2020-06-09 22:28:07
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
创业公司的域名想象 立即下载
“域”见更美好的未来——域名筑梦互联网+ 立即下载
“域”感——《域名投资从入门到精通》 立即下载