大网数据广告活动
服务器运维问答

PHPMyAdmin with nginx in Docker proxy not working

· 52CE 我爱测

I have a docker setup with PHP/MySQL and PHPMyAdmin running on nginx webserver I want to create a subdirectory on nginx /phpmyadmin which will proxy to the phpmyadmin container. Everything else should be served by php and nginx. …

问题原文

原帖链接 · Kristof Rado · CC BY-SA 4.0

I have a docker setup with PHP/MySQL and PHPMyAdmin running on nginx webserver

I want to create a subdirectory on nginx /phpmyadmin which will proxy to the phpmyadmin container. Everything else should be served by php and nginx.

The problem: /phpmyadmin loads with the login screen, but nothing else. I can't login, because nginx shows 404 page

https://example.com/phpmyadmin/index.php?route=/ 404

The docker-compose.yml file:

version: '3.8'
services:
  web:
    container_name: web
    image: php:7.3-fpm
    volumes:
      - ./data/src:/code

  nginx:
    container_name: nginx
    image: nginx:latest
    ports:
      - 8085:80
    volumes:
      - ./data/nginx/site.conf:/etc/nginx/nginx.conf
      - ./data/src:/code
  db:
    container_name: db
    image: mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=pass
      - MYSQL_DATABASE=mysql
      - MYSQL_USER=user
      - MYSQL_PASSWORD=user

  phpmyadmin:
    container_name: phpmyadmin
    image: phpmyadmin
    restart: always
    environment:
      - PMA_HOST=db
      - PMA_ABSOLUTE_URI="https://example.com/phpmyadmin/"
    depends_on:
      - db

The nginx.conf:

worker_processes 4;
events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    server {
        listen 80;
        index index.php index.html;
        server_name localhost;
        error_log  /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        root /code;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass web:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }

        location ~ \/phpmyadmin/ {
                rewrite ^/phpmyadmin(/.*)$ $1 break;
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
                proxy_pass http://phpmyadmin;
            }
    }
}

I have tried with location /phpmyadmin/ that also did not worked. It seems like the PMA_ABSOLUTE_URI is not correct?

已采纳解答

原帖链接 · Ivan Shatsky · CC BY-SA 4.0

Try this one instead:

location ^~ /phpmyadmin/ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://phpmyadmin/;
}

The ^~ will made this location to handle every URI starting with /phpmyadmin/ including those that are finishing with the .php suffix. This one will also cut the /phpmyadmin prefix because of the trailing slash after the upstream name, and will do it more efficiently that using the rewrite ... break directive.

Keep your PMA_ABSOLUTE_URI to be set exactly as it is set right now.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-06-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。