Is this a Docker or Nginx problem?

Hello,
The compose file looks like this:

phpmyadmin:
    image: phpmyadmin:latest
    container_name: phpmyadmin
    restart: unless-stopped
    environment:
      PMA_SOCKET: /run/mysqld/mysqld.sock
      PMA_HOST: 172.20.2.58
      MYSQL_ROOT_PASSWORD: 123456
    ports:
      - "127.0.0.1:8080:80"
    volumes:
      - /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock

The Nginx configuration file is also as follows:

location /phpmyadmin/ {
        proxy_pass http://127.0.0.1:8080/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Script-Name /phpmyadmin;

        # Add these critical headers:
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-Server $host;

        # Cookie path rewrite
        proxy_cookie_path / /phpmyadmin/;

        # Rest of your existing config...
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # Buffer settings
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;

        # Security headers
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-Content-Type-Options "nosniff";
        add_header X-XSS-Protection "1; mode=block";
    }

    # Redirect /phpmyadmin to /phpmyadmin/
    location = /phpmyadmin {
        return 301 /phpmyadmin/;
    }

I use the address http://172.20.2.58/phpmyadmin to log in to PhpMyAdmin. After entering my username and password, I am redirected to the address http://172.20.2.58/index.php?route=/ and the following message is displayed:

502 Bad Gateway

When I go to http://172.20.2.58/phpmyadmin again, the problem is resolved.

Any idea?

Thank you.

​Based on the information provided in the SitePoint forum post, the issue appears to be related to the Nginx configuration, particularly how it handles the /phpmyadmin/ path and the subsequent redirection.​

Problem Summary

  • You’re running PhpMyAdmin in a Docker container, accessible via http://172.20.2.58/phpmyadmin.​
  • After logging in, you’re redirected to http://172.20.2.58/index.php?route=/, resulting in a 502 Bad Gateway error.​
  • Revisiting http://172.20.2.58/phpmyadmin seems to resolve the issue temporarily.​

Likely Cause

The redirection to /index.php?route=/ suggests that PhpMyAdmin is not aware it’s being served under the /phpmyadmin/ subpath. This mismatch can cause it to generate incorrect URLs, leading to the 502 error.​

Suggested Solution

To address this, you can modify the Nginx configuration to correctly handle the subpath. Here’s an adjusted configuration:​

nginx

CopyEdit

location /phpmyadmin/ {
    proxy_pass http://127.0.0.1:8080/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    # Adjust the script name to inform PhpMyAdmin of the subpath
    proxy_set_header X-Script-Name /phpmyadmin;

    # Rewrite cookie paths to maintain session
    proxy_cookie_path / /phpmyadmin/;

    # Additional headers and settings...
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
    add_header X-XSS-Protection "1; mode=block";
}

# Redirect /phpmyadmin to /phpmyadmin/
location = /phpmyadmin {
    return 301 /phpmyadmin/;
}

Ensure that the X-Script-Name header is correctly set, as it informs PhpMyAdmin of the base path it’s being served under. This should help PhpMyAdmin generate the correct URLs and prevent the 502 error upon redirection.​

After making these changes, reload the Nginx configuration and test the setup again.​

If you continue to experience issues, please provide additional details or error logs, and I’ll be glad to assist further.

1 Like

Hello,
Thank you so much for your reply.
I used the Nginx configuration you wrote. After entering my username and password I was redirected to http://172.20.2.58/index.php?route=/!:

The phpmyadmin container logs are as follows:

# docker logs phpmyadmin
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.19.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.19.0.2. Set the 'ServerName' directive globally to suppress this message
[Mon Apr 14 12:13:04.568313 2025] [mpm_prefork:notice] [pid 1:tid 1] AH00163: Apache/2.4.62 (Debian) PHP/8.2.28 configured -- resuming normal operations
[Mon Apr 14 12:13:04.568402 2025] [core:notice] [pid 1:tid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
172.19.0.1 - - [14/Apr/2025:12:13:37 +0000] "GET / HTTP/1.1" 200 6109 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
172.19.0.1 - - [14/Apr/2025:12:13:39 +0000] "GET /js/messages.php?l=en&v=5.2.2&lang=en HTTP/1.1" 200 8229 "http://172.20.2.58/phpmyadmin/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
172.19.0.1 - - [14/Apr/2025:12:14:25 +0000] "POST /index.php?route=/ HTTP/1.1" 302 927 "http://172.20.2.58/phpmyadmin/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"

The PhpMyAdmin container uses Apache internally. Is it possible that the problem is with the Apache configuration within the container?