question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I fix this 502 Bad Gateway?

See original GitHub issue

!!!PLEASE READ!!!

Questions

If you have a question, DO NOT SUBMIT a new issue. Please ask the question on the Q&A Group: https://groups.google.com/forum/#!forum/nginx-proxy

google group is shows “Content unavailable”. There is no place to join that group of view previous issues.

Bugs or Features

I am trying to run a WordPress site inside of a docker container on Ubuntu VPS using Nginx-Proxy.

I created the following docker-compose.yml file

version: '3.4'
services:
    nginx-proxy:
        image: jwilder/nginx-proxy
        ports:
            - 80:80
            - 443:443
        restart: always
        networks:
            - external
        volumes:
            - wordpress:/var/www/html
        ports:
            - 5500:5500
    wordpress_app:
        image: wordpress
        restart: always
        environment:
            - VIRTUAL_HOST=sub.domain.com
            - VIRTUAL_PORT=5500
            - WORDPRESS_DB_HOST=db
            - WORDPRESS_DB_USER=db_username
            - WORDPRESS_DB_PASSWORD=db_password
            - WORDPRESS_DB_NAME=db_name
        depends_on:
            - nginx-proxy
            - db
        networks:
            - internal
            - external
        volumes:
            - wordpress:/var/www/html
        ports:
            - 5500:5500
    db:
        image: mysql:latest
        restart: always
        hostname: mysql
        environment:
            - VIRTUAL_PORT=3306
            - MYSQL_DATABASE=db_name
            - MYSQL_USER=db_username
            - MYSQL_PASSWORD=db_password
            - MYSQL_ROOT_PASSWORD=root_password
        networks:
            - internal
        volumes:
            - db:/var/lib/mysql
        ports:
            - 3306:3306
volumes:
    wordpress:
    db:

networks:
    internal:
    external:

Now, executing docker-compose up prints the following

Attaching to root_db_1, root_nginx-proxy_1, root_wordpress_app_1
db_1                 | 2020-10-12 16:34:17+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
db_1                 | 2020-10-12 16:34:18+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1                 | 2020-10-12 16:34:18+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
db_1                 | 2020-10-12T16:34:19.127860Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.21) starting as process 1
db_1                 | 2020-10-12T16:34:19.146953Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1                 | 2020-10-12T16:34:20.318195Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
nginx-proxy_1        | WARNING: /etc/nginx/dhparam/dhparam.pem was not found. A pre-generated dhparam.pem will be used for now while a new one
nginx-proxy_1        | is being generated in the background.    Once the new dhparam.pem is in place, nginx will be reloaded.
nginx-proxy_1        | forego         | starting dockergen.1 on port 5000
nginx-proxy_1        | forego         | starting nginx.1 on port 5100
nginx-proxy_1        | dockergen.1 | 2020/10/12 16:34:18 Generated '/etc/nginx/conf.d/default.conf' from 2 containers
nginx-proxy_1        | dockergen.1 | 2020/10/12 16:34:18 Watching docker events
nginx-proxy_1        | dockergen.1 | 2020/10/12 16:34:20 Generated '/etc/nginx/conf.d/default.conf' from 3 containers
nginx-proxy_1        | dockergen.1 | 2020/10/12 16:34:20 Received event start for container a6b94da9d7a7
nginx-proxy_1        | dockergen.1 | 2020/10/12 16:34:20 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'
db_1                 | 2020-10-12T16:34:20.960375Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
db_1                 | 2020-10-12T16:34:21.394031Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1                 | 2020-10-12T16:34:21.394509Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
db_1                 | 2020-10-12T16:34:21.425181Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1                 | 2020-10-12T16:34:21.566194Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21'    socket: '/var/run/mysqld/mysqld.sock'    port: 3306    MySQL Community Server - GPL.
wordpress_app_1      | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.25.0.3. Set the 'ServerName' directive globally to suppress this message
wordpress_app_1      | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.25.0.3. Set the 'ServerName' directive globally to suppress this message
wordpress_app_1      | [Mon Oct 12 16:34:22.661957 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.11 configured -- resuming normal operations
wordpress_app_1      | [Mon Oct 12 16:34:22.662180 2020] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
nginx-proxy_1        | 2020/10/12 16:34:23 [notice] 49#49: signal process started
nginx-proxy_1        | Generating DH parameters, 2048 bit long safe prime, generator 2
nginx-proxy_1        | This is going to take a long time
nginx-proxy_1        | dhparam generation complete, reloading nginx

Then when I make a request to http://sub.domain.com I get the 502 error but I also get this in the server’s log

nginx-proxy_1        | nginx.1        | 2020/10/12 16:35:07 [error] 51#51: *1 connect() failed (111: Connection refused) while connecting to upstream, client: MY_PERSOANL_IP_ADDRESS, server: sub.domain.com, request: "GET / HTTP/1.1", upstream: "http://172.25.0.3:5500/", host: "sub.domain.com"
nginx-proxy_1        | nginx.1        | sub.domain.com MY_PERSOANL_IP_ADDRESS - - [12/Oct/2020:16:35:07 +0000] "GET / HTTP/1.1" 502 157 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0"
nginx-proxy_1        | nginx.1        | sub.domain.com MY_PERSOANL_IP_ADDRESS - - [12/Oct/2020:16:35:08 +0000] "GET /favicon.ico HTTP/1.1" 502 157 "http://sub.domain.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0"
nginx-proxy_1        | nginx.1        | 2020/10/12 16:35:08 [error] 51#51: *1 no live upstreams while connecting to upstream, client: MY_PERSOANL_IP_ADDRESS, server: sub.domain.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://sub.domain.com/favicon.ico", host: "sub.domain.com", referrer: "http://sub.domain.com/"

Potential Issue

The logs states

2020/10/12 16:34:20 Generated ‘/etc/nginx/conf.d/default.conf’ from 3 containers

However, while docker containers are running, I execute cd /etc/nginx/conf.d but I get -bash: cd: /etc/nginx/conf.d/: No such file or directory. Should the file /etc/nginx/conf.d/default.conf actually exists?

What can be causing this issue? How can I fix it?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
jaywork2020commented, Oct 14, 2020

@tkw1536 Thank you for you assistance. I have improved the docker-compose.yml file to the following

version: "3"
services:
    nginx-proxy:
        image: jwilder/nginx-proxy
        container_name: nginx-proxy
        ports:
            - "80:80"
            - "443:443"
        restart: always
        volumes:
            - /var/run/docker.sock:/tmp/docker.sock:ro
            - /etc/certificates:/etc/nginx/certs

    wordpress_app:
        image: wordpress:latest
        restart: always
        container_name: wordpress_app
        environment:
            VIRTUAL_HOST: usa.doamin.com
            WORDPRESS_DB_HOST: mysql_server:3306
            WORDPRESS_DB_USER: db_username
            WORDPRESS_DB_PASSWORD: db_password
            WORDPRESS_DB_NAME: wordpress_app
        depends_on:
            - nginx-proxy
            - mysql_server
        volumes:
            - wordpress_app_public_html:/var/www/html
        expose:
            - 80

    phpmyadmin:
        image: phpmyadmin
        container_name: phpmyadmin
        restart: always
        environment:
            VIRTUAL_HOST: adminusa.domain.com
            PMP_HOST: mysql_server:3306
            MYSQL_USERNAME: root
            MYSQL_ROOT_PASSWORD: db_root_password
        depends_on:
            - nginx-proxy
            - mysql_server
        expose:
            - 80
    mysql_server:
        image: mysql:latest
        restart: always
        container_name: mysql_server
        environment:
            MYSQL_DATABASE: wordpress_app
            MYSQL_USER: db_username
            MYSQL_PASSWORD: db_password
            MYSQL_ROOT_PASSWORD: db_root_password
        volumes:
            - mysql_server_data:/var/lib/mysql

volumes:
    wordpress_app_public_html:
    mysql_server_data:

networks:
    default:
       external:
           name: nginx-proxy

Additionally, in the /etc/certificates path, I have the following 4 files for the https certificate

  • adminusa.domain.com.crt
  • adminusa.domain.com.key
  • usa.domain.com.crt
  • usa.domain.com.key

When browsing http://usa.domain.com I get the “503 Service Temporarily Unavailable” error. At the same time, when browsing https://usa.domain.com I get a white page.

Any idea on what could be causing the 503 error now?

0reactions
dequeuescommented, Sep 11, 2022

Is this a dup of #490?

I’m hitting this too. I have another container that works perfectly, so I’m not sure what the difference is. Been trying all day to no avail. The container works perfectly when I drop the environment variables (VIRTUAL_HOST and VIRTUAL_PORT) that trigger nginx-proxy and access it directly by IP, but as soon as I add the environment variables, I get the errors about no live upstreams while connecting to upstream and connect() failed (111: Connection refused) while connecting to upstream

Edit: I see, I thought we had to expose the published port, rather than the internal host. Publishing ports is not necessary at all, so I fixed it by updating the port the service within my container listens to. I ended up hitting the same problem again, but it turned out to be because I forgot to add the correct network to the container, which I was able to see by following the debugging steps in the README.

Could you share your config? I’m having this exact same issue in #2049

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Simple Explanation of a 502 Bad Gateway Error & How to ...
How to Fix a 502 Bad Gateway Error · Reload the page. · Look for server connectivity issues. · Check for any DNS...
Read more >
How To Fix a 502 Bad Gateway Error - Kinsta
1. Try Reloading the Page · 2. Clear Your Browser Cache · 3. flush your local DNS cache · 4. Check With Your...
Read more >
502 Bad Gateway Error [Solved] - freeCodeCamp
Fix 1: Refresh the Page · Fix 2: Try to Access the Website on another Device · Fix 3: Switch to another Browser...
Read more >
502 Bad Gateway Error: What It Is and How to Fix It - Lifewire
Try loading the URL again by pressing F5 or Ctrl+R (Command+R on a Mac) on your keyboard, or by selecting the refresh/reload button....
Read more >
502 Bad Gateway Error Explained: What it is and How to Fix It
6 ways to troubleshoot a 502 error message · Refresh the page – Just click the refresh button in your browser window or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found