nginx url rewrite for reverse proxy

2019-03-29 11:19发布

问题:

I have an nginx on port 80 and a tomcat on port 8080 configured as upstream.

The war application in tomcat listen to /pwm.

I would like to configure nginx to a reverse proxy for tomcat and rewrite the url "/" to "/pwm".

example: user types "web.noc.local" in browser and nginx rewrites the url to web.noc.local/pwm and redirects to tomcat on port 8080.

my nginx config:

upstream pwm_server  {
    server 127.0.0.1:8080 fail_timeout=0;
}

server {
    listen       80;
    server_name  web.noc.local;     
    access_log  /var/log/nginx/log/web.noc.local.access.log  main;
    error_log  /var/log/nginx/log/web.noc.local.error.log;      
    location / {
        if ($is_args != "") {  
            rewrite "^$" /pwm  break;
            expires     7d;
            proxy_pass http://pwm_server; 
        }           
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;    
        proxy_max_temp_file_size 0;     
        proxy_buffering off;
        proxy_connect_timeout 30;
        proxy_send_timeout 30;
        proxy_read_timeout 30;
        proxy_pass http://pwm_server;
    }
}

now when I open the url the, nothing happens, only a blank screen.

thx for help.

回答1:

Ok, I found a solution for me:

location / {
    rewrite ^ http://web.noc.local/pwm/ last;
}

location /pwm {

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;

    proxy_max_temp_file_size 0;     
    proxy_buffering off;
    proxy_connect_timeout 30;
    proxy_send_timeout 30;
    proxy_read_timeout 30;
    proxy_pass http://pwm_server;
}


标签: nginx