Report this

What is the reason for this report?

How to Reverse proxy for nginx configuration for subpath in ROOT folder?

Posted on April 29, 2022

I have two servers with different IP addresses:

Nginx is running on port 443, and I’m using it to reverse proxy my webapp this way:

    location /app1/ {
    rewrite ^/app1(.*) /$1 break; 
    proxy_pass https://app1.domain.com/;  }

This way, I normally access my webapp via nginx through https:// nginx.domain.com/app1/.

Secondly, in the ROOT folder of my webapp, I installed the birt-viewer application in the ROOT/birt-viewer folder. I normally access the birt-viewer application when I use the link https:// app1.domain.com/birt-viewer.

However, I don’t normally access the birt application when I use the link https:// nginx.domain.com/app1/birt-viewer. when I copy for example the link /birt-viewer/Task.jsp?__report=Recare.rpgn&sample=my+parameter&__sessionId=2026 and I paste it after the link https:// nginx.domain.com/app1 to obtain the final link https:// nginx.domain.com/app1/birt-viewer/Task.jsp?__report=Recare.rpgn&sample=my+parameter&__sessionId=2026, I access the birt-viewer application but I lose settings such as cookies and sessions.

You understand that to access my webapp via nginx I have to do it manually; the disadvantage is the loss of cookies, sessions and other parameters. Yet access should be done automatically without problems.

This is a part of my nginx config:

 server_name nginx.domain.com; 
    location /app1/ {
     rewrite ^/app1(.*) /$1 break; 
     proxy_pass https:// app1.domain.com/;  }
 
 location /app1/folder1/ {
 rewrite ^/app1/folder1(.*) /$1 break; 
 proxy_pass https:// app1.domain.com/folder1/;  }

I also realize that once my webapp behind nginx, some urls are not up to date and still keep the old access.

So my concern is to access the birt-viewer application automatically (not manually) through nginx via https:// nginx.domain.com/app1/birt-viewer

Does anyone have a solution for me?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

In case that I’m not missing anything, the following should work for you:

server {
    listen 443 ssl;
    server_name nginx.domain.com;

    # SSL settings
    # (you should already have these in your config)

    location /app1/ {
        proxy_pass https://app1.domain.com/;
        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 $scheme;
    }

    location /app1/birt-viewer/ {
        rewrite ^/app1/birt-viewer(/.*)$ $1 break;
        proxy_pass https://app1.domain.com/birt-viewer/;
        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 $scheme;
        proxy_cookie_path /birt-viewer/ /app1/birt-viewer/;
    }

    location /app1/folder1/ {
        rewrite ^/app1/folder1(/.*)$ $1 break;
        proxy_pass https://app1.domain.com/folder1/;
        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 $scheme;
    }
}

Overview:

  1. Added a specific location block for /app1/birt-viewer/ that proxies the requests to https://app1.domain.com/birt-viewer/.
  2. Added the proxy_set_header directives to pass the appropriate headers to the backend server, so it knows the original request information.
  3. Added proxy_cookie_path directive to adjust the path of cookies set by the backend server, so they work correctly when accessing through the /app1/birt-viewer/ URL.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.