Search code examples
nginxurl-rewritingnginx-locationproxypass

NGINX rewrite location to another port


Have installed NGINX so I can divert

http://example.com/Dev/ to http://example.com:8080/apex/f?p=4550:1

I thought I found the solution and gave it a go:

Changed ngnix.config

   location /dev/ {
       rewrite ^/(/dev/)(.*)$ http://localhost:8080/apex/$1 break;
       rewrite_log on;
    }

.

sudo systemctrl reload nginx

But when I try http://example.com/Dev/ on a browser

I get the 500 error.

Website is listening to 8080 and NGiNX listening to 80

Also tried

location /dev {
    rewrite ^/dev(.*) /apex/$1 last;
    proxy_pass http://localhost:8080;
}

Solution

  • Credit return 301

    After reading the above answer I tried using return instead of rewrite and it worked.

    Anyway here is what worked.

    location ~ /dev/?$ {
            return 302 http://example.com:8080/apex/$1;
         }
    

    After playing around I also got it to work with rewrite looks like I just needed to add a '~' in the location line

    location ~ /dev {
               rewrite ^/dev(.*) http://example.com:8080/apex$1 last;
            }