Docker-compose:如何使用带路由的 Nginx 反向代理

Docker-compose:如何使用带路由的 Nginx 反向代理

我正在使用带有 nginx 反向代理的 docker-compose 在具有不同路径的服务器上运行它:

  • 服务器 IP/后端
  • server_ip /前端

应该在端口 80 上工作。

我正在使用 docker-compose 对其进行测试,使用 nginx 作为 web 容器名称,使用 phpmyadmin 作为容器名称 myadmin (mysql):

services:
    web:
        image: nginx:alpine
        networks:
            docker-network:
               aliases:
                   - frontend-name
        container_name: web
        volumes:
            - "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf"
            - "./etc/ssl:/etc/ssl"
            - "./web:/var/www/html"
            - "./etc/nginx/default.template.conf:/etc/nginx/conf.d/default.template"
        ports:
            - "${HTTP_PORT}:80"
            - "${HTTPS_PORT}:443"
        environment:
            - NGINX_HOST=${NGINX_HOST}
            #- VIRTUAL_HOST= a.com
        command: /bin/sh -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
        restart: always
        depends_on:
            - php
            - mysqldb

    php:
        image: nanoninja/php-fpm:latest
        networks:
            docker-network:
               aliases:
                   - php-name  
        container_name: php-fpm
        restart: always
        volumes:
            - "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
            - "./web:/var/www/html"

    myadmin:
        image: phpmyadmin/phpmyadmin
        networks:
            docker-network:
               aliases:
                   - backend-name  
        container_name: myadmin
        ports:
            - "${PHPMYADMIN_PORT}:80"
        environment:
            - PMA_ARBITRARY=1
            - PMA_HOST=${MYSQL_HOST}
           # - PMA_ABSOLUTE_URI: https://localhost/pma/
        restart: always
        depends_on:
            - mysqldb

    mysqldb:
        build:
            context: .
            dockerfile: Dockerfile-mariadb
            args:
                MARIADB_VERSION: ${MARIADB_VERSION}
        networks:
            docker-network:
               aliases:
                   - mysql-name
        container_name: ${MYSQL_HOST}
        restart: always
        env_file:
            - ".env"
        environment:
            - MYSQL_DATABASE=${MYSQL_DATABASE}
            - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
            - MYSQL_USER=${MYSQL_USER}
            - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        ports:
            - "127.0.0.1:${MYSQL_PORT}:3306"
        volumes:
            - "./mysql-bbdd:/var/lib/mysql"

networks:
    docker-network:
       # driver: bridge

使用nginx default.conf文件做反向代理:

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

    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php: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 / {
          
        proxy_pass http://web;
         
    }

    location /api {
  
        proxy_pass http://myadmin:8080/;
    }

    location = /404.html {
 
        internal;
    }
}

但是当我将服务器 IP 与其路由放在一起时,它对我来说不起作用,我不知道该怎么办。

相关内容