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. mysql優化 - mysql count(id)查詢速度如何優化?2. mysql主從 - 請教下mysql 主動-被動模式的雙主配置 和 主從配置在應用上有什么區別?3. angular.js - angularjs 用ng-reapt渲染的dom 怎么獲取上面的屬性4. 主從備份 - 跪求mysql 高可用主從方案5. css3 - [CSS] 動畫效果 3D翻轉bug6. node.js - node_moduls太多了7. angular.js - Angular路由和express路由的組合使用問題8. python如何不改動文件的情況下修改文件的 修改日期9. python - django 里自定義的 login 方法,如何使用 login_required()10. angular.js - 不適用其他構建工具,怎么搭建angular1項目
![css3 - [CSS] 動畫效果 3D翻轉bug](http://www.aoyou183.cn/attached/image/news/202304/110831f073.png)