skip to Main Content

I have set up a docker composer with 2 PHP applications in Laravel and an Nginx container.

docker-compose.yml:

version: '2.0'
services:
 
  # PHP Service
  app:
    build:
      args:
        user: application
        uid: 1000
      context: ./
      dockerfile: Dockerfile
    image: myapp/app
    container_name: app
    restart: unless-stopped
    tty: true
    working_dir: /var/www/
    volumes:
      - ./:/var/www
      - ./php/laravel.ini:/usr/local/etc/php/conf.d/laravel.ini
    networks:
      - app-network

  admin:
    build:
      args:
        user: application
        uid: 1000
      context: ./
      dockerfile: Dockerfile
    image: myapp/admin
    container_name: admin
    restart: unless-stopped
    tty: true
    working_dir: /var/www/
    volumes:
      - ./:/var/www
      - ./php/laravel.ini:/usr/local/etc/php/conf.d/laravel.ini
    networks:
      - app-network
 
  # Nginx Service
  nginx:
    image: nginx:1.17-alpine
    container_name: nginx
    restart: unless-stopped
    tty: true
    ports:
      - "9000:80"
      - "443:443"
    volumes:
      - ./:/var/www/
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network
 
# Docker Networks
networks:
  app-network:
    driver: bridge

Things work great when I’m reaching the app (http://localhost:9000) but I can’t get nginx to reach the admin container when I access http://localhost:9000/admin

nginx.conf:

server {
    listen 80;
    set $root_path '/var/www/public';
    
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    
    root $root_path;

    location ~ .php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass app: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 / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }

    location /admin {
        try_files $uri $uri/ /index.php?$query_string;
        fastcgi_pass admin:9000;
    }
}

logs:

app      | 172.18.0.2 -  22/Aug/2022:15:46:21 +0000 "GET /index.php" 302
nginx    | 172.18.0.1 - - [22/Aug/2022:15:46:22 +0000] "GET /admin HTTP/1.1" 302 388 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
nginx    | 172.18.0.1 - - [22/Aug/2022:15:46:22 +0000] "GET /admin/login HTTP/1.1" 200 23138 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
app      | 172.18.0.2 -  22/Aug/2022:15:46:22 +0000 "GET /index.php" 200

What am I doing wrong?

2

Answers


  1. Chosen as BEST ANSWER

    Got it to work, the order of the locations was wrong and try_files was redirecting it to the app. I had to use rewrite ^/(.*)$ /index.php?$1 break; to be able to reach /index.php in the admin container

    server {
        listen 80;
        set $root_path '/var/www/public';
        
        index index.php index.html;
        error_log  /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        
        root $root_path;
    
        location /admin {
            try_files $uri @admin_proxy;
        }
    
        location @admin_proxy {
            rewrite ^/(.*)$ /index.php?$1 break;
            try_files $uri =404;
            fastcgi_split_path_info ^(.+.php)(/.+)$;
            fastcgi_pass admin: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 / {
            try_files $uri $uri/ /index.php?$query_string;
            gzip_static on;
    
            location ~ .php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+.php)(/.+)$;
                fastcgi_pass app:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
            }
        }
    
    }
    

    There's probably a better way to do this


  2. Check if http://localhost:9000 is reachable or not via nginx.
    If yes then trace the access.log and error.log file.

    Check the root path /var/www/public seems it would be like /var/www/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search