java-ee - Nginx proxy Tomcat problem request.getRequestURI(); How to make it not include the project name
黄舟
黄舟 2017-05-16 17:23:41
0
2
405

If nginx has the following configuration

I visit http://kaipizhe.com At this time request.getRequestURI(); this value is /kaipizhe/ instead of /
When I visit http://kaipizhe.com/all/ this time request.getRequestURI(); the value is /kaipizhe/all/ instead of /all/

That is, request.getRequestURI(); will bring /kaipizhe/, how to make it directly /,

Is it me? nginx There is a configuration problem, what should I do?

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

黄舟
黄舟

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

reply all(2)
阿神

It is recommended to modify the configuration of tomcat, configure a virtual host for your project, and set the root directory of the project to /usr/local/tomcat/webapps/kaipizhe (or the actual root directory of your project), so that you do not need to add a / when accessing kaipizhe prefix, naturally the result obtained by request.getRequestURI() is also what you want.

If you use the above access, remember to modify the nginx configuration, it should be fine.

    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/;
        }
迷茫

You can also implement it without modifying the tomcat configuration, adding a virtual host, or using nginx rewrite rules;

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;
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!