Nginx reverse proxy in nested locations

8,178

Solution 1

Perhaps instead of having many locations, you could use regex to match a particular URI to your upstream application:

location ~ /(app)(\d+) {
   proxy_pass http://127.0.0.1:808$2/$1$2;
   include static_file_config.conf;
}

You can see how it works here: https://regex101.com/r/sM3eS9/1

Solution 2

I had a related issue, but mine was related to Docker containers and solved it by doing the following:

server {
   listen       80;
   server_name  some_application_name;
   port_in_redirect off;
   location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://localhost:5555;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /api {
            rewrite ^/api(.*)$ $1 break;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://localhost:5000;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

The above has a container running on 5555 and the .NET Core Api running on 5000 and I did not want to change the routes in the .NET api so the rewrite ensured that the route was correct for the API to funciton correclty.

Share:
8,178

Related videos on Youtube

Jose Antonio
Author by

Jose Antonio

Updated on September 18, 2022

Comments

  • Jose Antonio
    Jose Antonio almost 2 years

    I'm using Nginx as a reverse proxy for multiple applications in multiple servers and I'm trying to add common cache directives to static files of each application.

    My original configuration is something like this:

    location /app1{
        ...
        proxy_pass http://127.0.0.1:8081/app1;
    }
    
    location /app2{
        ...
        proxy_pass http://127.0.0.1:8082/app2; 
    }
    
    ...
    

    To add the static file directives I can add a nested location to each location like this:

    location /app1{
        ...
        proxy_pass http://127.0.0.1:8081/app1;
        location ~* \.(css|js|ico|gif|jpg|jpeg|png)$ {
            expires 1d;
            ...
            proxy_pass http://127.0.0.1:8081;
        }
    }
    
    location /app2{
        ...
        proxy_pass http://127.0.0.1:8082/app2;
        location ~* \.(css|js|ico|gif|jpg|jpeg|png)$ {
            expires 1d;
            ...
            proxy_pass http://127.0.0.1:8082;
        }
    }
    

    Since I have something like 30 applications, I'm trying to simplify the code to something like this:

    location /app1{
        ...
        proxy_pass http://127.0.0.1:8081/app1;
        include static_file_config.conf;
    }
    
    location /app2{
        ...
        proxy_pass http://127.0.0.1:8081/app2;
        include static_file_config.conf;
    }
    

    Is there a way I can simplify the code so I don't end with 30 identical locations for static files?

    Please note that each application serves its own static files.

  • Jose Antonio
    Jose Antonio over 9 years
    It seams valid for someone with the exact configuration of the question. But in may case it will not work since it was only an example, I didn't use the real app names because of the company laws. The names are not numbered, they are real product names, the ports are not sequential, and the IPs can change, so I will not be able to simplify that much.
  • Jose Antonio
    Jose Antonio over 9 years
    The biggest problem I'm finding is the need to set the proxy_pass inside the nested location (for some reason was removed from the question). Does the variables pass to the nested location? Unfortunately, if they don't your solution will not work.
  • Vasili Syrakis
    Vasili Syrakis over 9 years
    I don't see why not, since it is one scope above the nested location's scope... But I haven't tested it so I don't know.
  • Jinna Balu
    Jinna Balu almost 4 years
    Works perfect multi-app multi-locations.