Nginx proxy_pass using subfolder

nginxport-forwardingreverse-proxy

ok, this task should be simple but I just can't get it to work. I would like to have a subfolder after my domain name (actually after the IP of that domain name), which redirects to a specific port on the same server. Essentially, I want to get rid of having to use many ports.

Here is my nginx config for that

server {
    listen 80;

    index index.html index.htm index.nginx-debian.html index.php;

    server_name aaa.bbb.ccc.ddd;

    location ^~ /app2 {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://aaa.bbb.ccc.ddd:8001;
    }
}

So upon accessing aaa.bbb.ccc.ddd/app2 I would like this to resolve to http://aaa.bbb.ccc.ddd:8001.

This can note possibly be so complicated. What am I missing here?

Thank you
Pat

Best Answer

Since you tagged this as a reverse proxy question, I assume you mean that you want to proxy the request so that user only sees http://aaa.bbb.ccc.ddd/app2 URL in her browser.

You can change your location block to this:

location ~/app2(.*)$ {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://aaa.bbb.ccc.ddd:8001$1;
}

Here we capture the URI part after /app2 to $1 variable, and use it in the proxy_pass directive.

Related Topic