Score:0

Docker-compose: how to use Nginx Reverse Proxy with routing

it flag

I am using docker-compose with nginx reverse proxy to run it on server with different paths:

  • server_ip/backend
  • server_ip /frontend

should work on port 80.

I'm testing it with a docker-compose using nginx as the web container name and phpmyadmin as the container name 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

using the nginx default.conf file to do the reverse proxy:

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

but it doesn't work for me when I put the server ip with its routes, I don't know what to do.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.