Score:1

NGINX Hosting multiple servers in same domain

ni flag

I have a website being served by Nginx and I'm trying to do something that I don't know if it's possible the way I'm trying to do but the case is:

I have Application A and B made with React / Node.

  • The application A is the main, it has the frontend and backend on Dockerized.
  • The application B is the Admin Panel, that has a separate frontend, backend and is also dockerized.
  • I am currently serving the application A on www.applicationA.com/, and I'm trying to serve the application B on www.applicationA.com/admin.

Bellow is my NGINX config:

    server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            index index.html index.htm index.nginx-debian.html;
            server_name  applicationA.com www.applicationA.com;
            root /var/www/html/;
    
            location / {
                   // *This is working*
                   rewrite ^ https://$host$request_uri? permanent;
                   proxy_pass http://applicationA.com:8080;
            }
    
            location /admin {
                // *This is **NOT** working*
                proxy_pass http://applicationA.com:5001;
            }
    }
    
    server {
            listen 443 ssl http2;
            listen [::]:443 ssl http2;
            server_name applicationA.com www.applicationA.com;
            server_tokens off;
            ssl_certificate /etc/letsencrypt/live/applicationA.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/applicationA.com/privkey.pem;
            ssl_buffer_size 8k;
            ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;
            ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
            ssl_prefer_server_ciphers on;
            ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
            ssl_ecdh_curve secp384r1;
            ssl_session_tickets off;
            ssl_stapling on;
            ssl_stapling_verify on;
            resolver 8.8.8.8;
            location / {
                    try_files $uri @server;
            }
    
            location @server { // **Working**
                    proxy_pass http://applicationA.com:8080;
                    add_header X-Frame-Options "SAMEORIGIN" always;
                    add_header X-XSS-Protection "1; mode=block" always;
                    add_header X-Content-Type-Options "nosniff" always;
                    add_header Referrer-Policy "no-referrer-when-downgrade" always;
                    add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
            }
    
            location /admin { // **NOT Working**
                    proxy_pass http://applicationA.com:5001;
                    add_header X-Frame-Options "SAMEORIGIN" always;
                    add_header X-XSS-Protection "1; mode=block" always;
                    add_header X-Content-Type-Options "nosniff" always;
                    add_header Referrer-Policy "no-referrer-when-downgrade" always;
                    add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 
                    'unsafe-inline'" always;
            }
    
            root /var/www/html/;
            index index.html index.htm index.nginx-debian.html;
 }
  • The response I get from accessing applicationA.com/admin is 502: Bad Gateway.
  • There is nothing relevant in the logs accusing any errors.
  • If needed, I will post the Node file or the docker-compose file.
cn flag
The `location` stanza can be nested.
Score:1
us flag

nginx returns 502 Bad Gateway error when the destination specified in proxy_pass cannot be reached. In this case, make sure the 5001 port is accessible from where nginx is running.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.