0

Is there a way to put two NGINX server in series?

In my configuration, I have multiple docker-compose instances of containers, which all run the same web applications. In additions, I have two NGINX. The NGINX1 server is located on my physical machine, and the other NGINX server (NGINX2) is located inside a docker-compose container.

Is there a way, connecting to the NGINX1 server, to automatically reach the APP1 application (which is inside a container) passing through the second NGINX (NGINX2, which, also, is internal to the container) by simply typing in a browser the link "mydomain.com/app1"?

I know that a more simple solution would be to point directly the docker-compose container to the external NGINX, but could I apply the scenario described instead?

For better understanding, I made a simple images showing my architecture.

image showing the architecture of the project

Here is my NGINX1 config file:

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  9999;

   

    server {
        listen       80;
        server_name client1.nginx.loc;

        access_log  logs/nginx_client_loc-access.log;
        error_log logs/nginx_client_loc-error.log;



        location /loki{
            #proxy_http_version 1.1;
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection "Upgrade";
            #proxy_set_header Host $http_host;
            proxy_pass          http://172.29.161.227:3100;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

And here there is the second NGINX config (NGNX2, internal to the container)

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  99999;

         server {
            listen 80;
            server_name localhost 127.0.0.1;
            resolver 127.0.0.11;
            location /APP1 {
                proxy_pass          http://APP1/content;
                proxy_set_header    X-Forwarded-For $remote_addr; 
                proxy_read_timeout 300;
                proxy_connect_timeout 300;
                proxy_send_timeout 300;
            }

            location /App2 {
                include  /etc/nginx/mime.types;
                proxy_pass          http://APP2/targets;
                proxy_set_header    X-Forwarded-For $remote_addr;
            }

Thanks so much

Carex
  • 17
  • 5
  • Should be easily possible, just let the first nginx `proxy_pass` to the second one… – slauth Aug 26 '21 at 08:42
  • I'm with slauth. There are some things to keep in mind though. For example, mnake sure that the nginx2 instance is the only container available from "outside" (that is, Nginx1) and also make sure that Nginx2 and the Apps are in the same network so they can be reached by that Nginx instance. Lastly, make sure that Nginx has the right ports opened. – JustLudo Aug 26 '21 at 08:45

1 Answers1

0

If I understood correctly you want NGINX1 to pass into NGINX2 which would pass the packet onward to APP1? In this case, the solution is rather straight-forward:

Config NGINX1 to send the packet into a specific port, e.g. port 777. Then, add an NGINX2 listener which would listen on port 777 and send it away.

NGINX1:

http {
    ...
    server {
        listen       80;
        ...
        location /loki{
            #proxy_http_version 1.1;
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection "Upgrade";
            #proxy_set_header Host $http_host;
            proxy_pass          http://172.29.161.227:3100;
        }
        location /APP1 {
            proxy_pass  <URL for NGINX2>:777;
            proxy_read_timeout 300;
            proxy_connect_timeout 300;
            proxy_send_timeout 300;
        }
        #error_page  404              /404.html;
        ...
    }

NGINX2:

http {
    include       mime.types;
    ...
    server {
        listen 80;
        ...
    }
    server {
        listen 777;
        server_name localhost 127.0.0.1;
        resolver 127.0.0.11;
        location /APP1 {
            proxy_pass  http://APP1/content;
            proxy_set_header    X-Forwarded-For $remote_addr; 
            proxy_read_timeout 300;
            proxy_connect_timeout 300;
            proxy_send_timeout 300;
        }
    }
...

This way a packet that arrives to /APP1 is forwarded by NGINX1 into port 777 of NGINX2 which in-turn forwards it into the APP1 content.

Also, if you could next time include ports on your architecture diagram, thsi would make it clearer to understand packet-movement.

Hopes this helps.