java-ee - Nginx 代理 Tomcat 問(wèn)題 request.getRequestURI(); 怎么讓他不帶項(xiàng)目名
問(wèn)題描述
如果 nginx 下面這樣的配置
我訪問(wèn) http://kaipizhe.com 這個(gè)時(shí)候 request.getRequestURI(); 這個(gè)值是 /kaipizhe/ 而不是 /我訪問(wèn) http://kaipizhe.com/all/ 這個(gè)時(shí)候 request.getRequestURI(); 這個(gè)值是 /kaipizhe/all/ 而不是 /all/
就是 request.getRequestURI(); 都會(huì)帶上 /kaipizhe/ ,怎么讓他直接是 / ,
是不是我 nginx 配置有問(wèn)題,應(yīng)該怎么處理
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;}
問(wèn)題解答
回答1:建議修改tomcat的配置,為你項(xiàng)目配置虛擬主機(jī),把項(xiàng)目的根目錄設(shè)置為 /usr/local/tomcat/webapps/kaipizhe (或者你項(xiàng)目實(shí)際的根目錄),這樣你訪問(wèn)就不需要加一個(gè) /kaipizhe 前綴了,自然 request.getRequestURI() 獲取的結(jié)果也是你想要的。
如果你采用了上述的訪問(wèn),記得還是要修改一下 nginx 的配置,應(yīng)該這樣就可以了。
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配置,不通過(guò)增加虛擬主機(jī)實(shí)現(xiàn), 也不使用nginx rewrite規(guī)則實(shí)現(xiàn);
server { listen 88; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location /SMSSupport/ { #靜態(tài)資源會(huì)自動(dòng)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 / { #非靜態(tài)請(qǐng)求,自動(dòng)轉(zhuǎn)發(fā)到對(duì)應(yīng)tomcat項(xiàng)目下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; }}
相關(guān)文章:
1. MYSQL 根據(jù)兩個(gè)字段值查詢 但兩個(gè)值的位置可能是互換的,這個(gè)怎么查?2. 求救一下,用新版的phpstudy,數(shù)據(jù)庫(kù)過(guò)段時(shí)間會(huì)消失是什么情況?3. mysql replace 死鎖4. mysql - C#連接數(shù)據(jù)庫(kù)時(shí)一直這一句出問(wèn)題int i = cmd.ExecuteNonQuery();5. javascript - 微信網(wǎng)頁(yè)開(kāi)發(fā)從菜單進(jìn)入頁(yè)面后,按返回鍵沒(méi)有關(guān)閉瀏覽器而是刷新當(dāng)前頁(yè)面,求解決?6. extra沒(méi)有加載出來(lái)7. android - 安卓做前端,PHP做后臺(tái)服務(wù)器 有什么需要注意的?8. python - 數(shù)據(jù)與循環(huán)次數(shù)對(duì)應(yīng)不上9. php傳對(duì)應(yīng)的id值為什么傳不了啊有木有大神會(huì)的看我下方截圖10. mysql - ubuntu開(kāi)啟3306端口失敗,有什么辦法可以解決?
