开发者社区> 问答> 正文

Nginx 代理 Tomcat 问题 request.getRequestURI(); 怎么让他不带项目名?

如果 nginx 下面这样的配置

我访问 http://kaipizhe.com 这个时候 request.getRequestURI(); 这个值是 /kaipizhe/ 而不是 /
我访问 http://kaipizhe.com/all/ 这个时候 request.getRequestURI(); 这个值是 /kaipizhe/all/ 而不是 /all/

就是 request.getRequestURI(); 都会带上 /kaipizhe/ ,怎么让他直接是 / ,

是不是我 nginx 配置有问题,应该怎么处理

log_format  kaipizhe.com  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';

server
{
    listen       80;  
    server_name  kaipizhe.com;
    root  /usr/local/tomcat/webapps/kaipizhe;

    include none.conf;

    location / {
            proxy_pass http://localhost:8080/kaipizhe/;
            proxy_cookie_path /kaipizhe /;
            proxy_set_header   Host    $host;
            proxy_set_header   X-Real-IP   $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect http://localhost:8080/kaipizhe/ http://kaipizhe.com/;
        }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

    location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

    access_log  /home/wwwlogs/kaipizhe.com.log  kaipizhe.com;
}

展开
收起
小旋风柴进 2016-03-04 10:20:30 3841 0
1 条回答
写回答
取消 提交回答
  • 建议修改tomcat的配置,为你项目配置虚拟主机,把项目的根目录设置为 /usr/local/tomcat/webapps/kaipizhe (或者你项目实际的根目录),这样你访问就不需要加一个 /kaipizhe 前缀了,自然 request.getRequestURI() 获取的结果也是你想要的。
    记得还是要修改一下 nginx 的配置

     location / {
                proxy_pass http://localhost:8080/;
                proxy_cookie_path / /;
                proxy_set_header   Host    $host;
                proxy_set_header   X-Real-IP   $remote_addr;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_redirect http://localhost:8080/ http://kaipizhe.com/;
            }
    2019-07-17 18:51:53
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
《Nginx 代理系统常用手册》 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载