java-ee - Nginx 代理 Tomcat 問題 request.getRequestURI(); 怎麼讓他不帶項目名
黄舟
黄舟 2017-05-16 17:23:41
0
2
412

如果 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;
}

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回覆(2)
阿神

建議修改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/;
        }
迷茫

也可以不修改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_pass        http://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_pass        http://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;
    }
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!