Docker 代理中带有 nginx 的 PHPMyAdmin 不起作用

Docker 代理中带有 nginx 的 PHPMyAdmin 不起作用

我有一个 docker 设置,其中 PHP/MySQL 和 PHPMyAdmin 在 nginx 网络服务器上运行

我想在 nginx 上创建一个子目录/phpmyadmin,用于代理 phpmyadmin 容器。其他所有内容都应由 php 和 nginx 提供。

问题:/phpmyadmin只加载了登录屏幕,没有其他内容。我无法登录,因为 nginx 显示 404 页面

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

文件docker-compose.yml

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

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;
            }
    }
}

我试过,location /phpmyadmin/但还是不行。好像PMA_ABSOLUTE_URI不正确?

答案1

尝试一下这个:

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/;
}

^~位置将处理以以下开头的每个 URI:/phpmyadmin/ 包括那些以后缀结尾的.php。这个命令也会/phpmyadmin因为上游名称后面的斜杠而切断前缀,并且比使用rewrite ... break指令更有效率。

保持您的PMA_ABSOLUTE_URI设置与现在的设置完全一致。

答案2

谢谢,这有效,这是我的完整配置:我正在使用 nginx、phpmyadmin 和 mysql 默认容器。

**default.conf**    
server {
        listen 80;
        server_name my.domain.here;
        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://container_phpmyadmin/;
        }
    }

**docker-compose.yml**
  database:
    image: mysql:5.7
    restart: unless-stopped
    container_name: container_mysql
    environment:
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
    volumes:
      - '../docker/mysql/data:/var/lib/mysql'
    networks:
      - app-network
  
  phpmyadmin:
    container_name: container_phpmyadmin
    image: phpmyadmin/phpmyadmin
    links:
      - database
    environment:
      PMA_HOST: database
      UPLOAD_LIMIT: 300M
      PMA_PORT: 3306
      PMA_ARBITRARY: 0
    restart: always
    depends_on:
      - database
    networks:
      - app-network
  

nginx:
    restart: unless-stopped
    image: nginx:alpine
    container_name: container_nginx
    working_dir: /app
    volumes:
      - ../docker/nginx/conf/nginx.conf:/etc/nginx/conf/nginx.conf:ro
      - ../docker/nginx/conf.d:/etc/nginx/conf.d:ro
    ports:
      - "80:80"
    links:
      - database
    networks:
      - app-network

相关内容