Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx url rewrite for reverse proxy

Tags:

nginx

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.

like image 738
luk3 Avatar asked Oct 02 '22 21:10

luk3


People also ask

Can NGINX be used as reverse proxy?

The benefits of using Nginx as a reverse proxy include: Clients access all backend resources through a single web address. The reverse proxy can serve static content, which reduces the load on application servers such as Express, Tomcat or WebSphere.

Does proxy change URL?

These proxy servers change the URLs in web pages so that requests for web pages from licensed databases are routed back to the proxy server.

What is return 301 in NGINX?

Permanent redirects such as NGINX 301 Redirect simply makes the browser forget the old address entirely and prevents it from attempting to access that address anymore. These redirects are very useful if your content has been permanently moved to a new location, like when you change domain names or servers.


1 Answers

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;
}
like image 172
luk3 Avatar answered Oct 05 '22 04:10

luk3