1

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.

1 Answers1

1

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.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63