Skip to main content

Raymii.org Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

NGINX: Proxy folders to different root

Published: 04-04-2013 | Author: Remy van Elst | Text only version of this article


Table of Contents


This tutorial shows you how to have NGINX use different folders as differentupstream proxy's.

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

By default, if you have a location block which has a proxy pass, and thelocation block is a folder, for example /wiki, the folder is sent back tothe proxied server:

location /nagios/ {    proxy_pass  http://10.0.21.8:80/;    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_set_header X-Forwarded-Proto https;    proxy_redirect    off;}

The above block will, when browsed, sent you to http://10.0.21.8/nagios/,because that is the nginx location. However, if you want it to go tohttp://10.0.21.8/ you either have to rewrite or use the / location.

The below example has the correct rewrite rule:

location /nagios/ {    rewrite ^/nagios(/.*)$ $1 break;    proxy_pass  http://10.0.21.8:80/;    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_set_header X-Forwarded-Proto https;    proxy_redirect    off;}

As you can see, this line:

rewrite ^/collectd(/.*)$ $1 break;

fixes the above problem, and sends you to http://10.0.21.8/ instead ofhttp://10.0.21.8/nagios.

Tags: apache, folder, nginx, proxy, subfolder, tutorials