java-ee - Nginx 代理 Tomcat 问题 request.getRequestURI(); 怎么让他不带项目名
问题描述
如果 nginx 下面这样的配置
我访问 http://kaipizhe.com 这个时候 request.getRequestURI(); 这个值是 /kaipizhe/ 而不是 /我访问 http://kaipizhe.com/all/ 这个时候 request.getRequestURI(); 这个值是 /kaipizhe/all/ 而不是 /all/
就是 request.getRequestURI(); 都会带上 /kaipizhe/ ,怎么让他直接是 / ,
是不是我 nginx 配置有问题,应该怎么处理
nginxlog_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;}
问题解答
回答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/;}回答2:
也可以不修改tomcat配置,不通过增加虚拟主机实现, 也不使用nginx rewrite规则实现;
server { listen 88; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location /SMSSupport/ { #静态资源会自动request.getRequestURI()头proxy_passhttp://127.0.0.1:8080;proxy_set_header Host $http_host;proxy_set_header X-Real-IP$remote_addr;proxy_http_version 1.1; } location / { #非静态请求,自动转发到对应tomcat项目下proxy_passhttp://127.0.0.1:8080/SMSSupport/;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Server $host;proxy_set_header X-Real-IP$remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_http_version 1.1; }}
相关文章:
1. css3 - 一个关于CSS雪碧图的问题2. Docker for Mac 创建的dnsmasq容器连不上/不工作的问题3. docker-machine添加一个已有的docker主机问题4. redis编译报错:cannot find -lgcc_s5. docker镜像push报错6. 如何解决Centos下Docker服务启动无响应,且输入docker命令无响应?7. mac里的docker如何命令行开启呢?8. angular.js - 如何在指令里使用ng-click9. node.js - antdesign怎么集合react-redux对input控件进行初始化赋值10. wordpress - nginx 反代 Varnish 走 Https ,WP后台 ‘您没有足够的权限访问该页面。’

网公网安备