docker nginx serve php fpm container to subdirectory URL

Viewed 15

I have two app that running php-fpm and listening to port 9000. I just wanna to make nginx running url like this

APPS 1 http://localhost/app1

APPS 2 http://localhost/app2

This is my docker-compose.yml

version: '3.2'
services:
    nginx:
        image: nginx:alpine
        restart: always
        ports:
            - 80:80
        volumes:
            - ./conf.d:/etc/nginx/conf.d/
            - ./logs/nginx:/var/log/nginx
            - ./app1:/var/www/html
            - ./app2:/var/www/html
        links:
            - app1
            - app2

    app1:
        image: php:7.4-fpm-alpine
        restart: always
        volumes:
            - ./app1:/var/www/html
            
    app2:
        image: php:8.0-fpm-alpine
        restart: always
        volumes:
            - ./app2:/var/www/html

Inside folder app1 and app2 i just insert my php app, yes it different version of PHP.

This is my nginx.conf

server {
    listen 80;
    server_name testapp.co;
 
    root /var/www/html;
    index index.php;
 
    access_log /var/log/nginx/hakase-access.log;
    error_log /var/log/nginx/hakase-error.log;
    
    location /app1 {
        alias /var/www/html/;
        try_files $uri $uri/ @app1;
        location ~ \.php$ {
            #include snippets/fastcgi-php.conf;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass app1: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 @app1 {
        rewrite /app1/(.*)$ /index.php/$1 last;
    }
    
    location /app2 {
        alias /var/www/html/;
        try_files $uri $uri/ @app2;
        location ~ \.php$ {
            #include snippets/fastcgi-php.conf;
            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass app2:9000;
        }
    }

    location @app2 {
        rewrite /app2/(.*)$ /index.php/$1 last;
    }

}

After i try it always show 404 Not Found, so anybody can help me please Thanks

0 Answers
Related