dockernginxreverse-proxybad-gateway

Nginx Reverse Proxy: Throwing 502 Bad Gateway


I was following this tutorial to learn about Reverse Proxy https://www.youtube.com/watch?v=ZmH1L1QeNHk&t=227s

I'm running the docker image like this

sudo docker run -d --name nginx-base -p 80:80 nginx:latest

I was able to edit the default.conf Here is the file

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /test {
        proxy_pass http://localhost:8086/test;
    }

    location /home {
        proxy_pass http://localhost:3000;
    }

   location /home/auth {
        proxy_pass http://localhost:3000/auth;
    }

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
     
    

I get the Nginx Welcome screen, when I go to http://localhost/

But when I try to access http://localhost/test or http://localhost/home

enter image description here

Also, I'm able to access localhost:3000 and localhost:8086/test

Not sure why nginx is throwing 502, did I miss any configuration?


Solution

  • 🤨 There is a really very few info about tuning up Nginx Proxy Manager. So let's take a look at common issues/errors:

    Welcome to nginx default page

    Thats happens because your nginx/app container files not volumed to /usr/share/nginx/html. By default NPM use this root location, not /var/www/.... Just add the right volume to your container.

    502 Bad Gateway error

    This is a complex issue. First of all make sure you volumed to the /data folder docs

    - ./data:/data
    

    💡 ⚠️ If folder is volumed, you can see Nginx Proxy Manager errors log in your folder /your/docker/path/data/logs/proxy-host-1_error.log. In my case error was:

    peer closed connection in SSL handshake while SSL handshaking to upstream, client: [bla-bla-bla]

    ⚡ The reason was because in Proxy Host edit at SSL tab i didnt checked Force SSL and HTTP/2 Support (if you use HTTPS/SSL/443 port).

    👉 Full guide how to tune up your host in NPM with HTTPS/SSL:

    1️⃣ You have to setup Nginx Proxy Manager via Docker and start a container. See setup docs. An example with MySQL storage:

    services:
      app:
        image: 'jc21/nginx-proxy-manager:latest'
        restart: unless-stopped
        ports:
          - '80:80'
          - '81:81'
          - '443:443'
        environment:
          DB_MYSQL_HOST: "db"
          DB_MYSQL_PORT: 3306
          DB_MYSQL_USER: ${DB_USER}
          DB_MYSQL_PASSWORD: ${DB_PASS}
          DB_MYSQL_NAME: ${DB_NAME}
        volumes:
          - ./data:/data
          - ./letsencrypt:/etc/letsencrypt
      db:
        image: 'jc21/mariadb-aria:latest'
        restart: unless-stopped
        environment:
          MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
          MYSQL_USER: ${DB_USER}
          MYSQL_PASSWORD: ${DB_PASS}
          MYSQL_DATABASE: ${DB_NAME}
        volumes:
          - ./mysql:/var/lib/mysql
    

    Dont forget to create .env file with MySQL variables values.

    Run docker compose up -d --build and you can go to http://your-ip:81 and see NPM GUI.

    Default access data:

    admin@example.com - login/email
    changeme - password
    

    2️⃣ You have your nginx/app container with:

    ⚠️ 8093 and 9201 ports are just examples

    3️⃣ In Nginx Proxy Manager admin panel go to Hosts -> Proxy Hosts -> Add Proxy Host

    Details tab

    Nginx Proxy Manager details on Proxy Host

    SSL tab

    Nginx Proxy Manager SSL settings on Proxy Host

    Advanced tab

    Leave empty, if you really don't know how to manage it in NPM.

    Hope that helps!