Search code examples
phpdockernginx

Nginx php 2 location forward to 2 different php app using docker


currently i am trying to pass some endpoint to one app and the rest to another php app but i still get file not found issue here is my nginx config

resolver 127.0.0.11;

server {

    listen [::]:80;
    listen 80;

    root /var/www/public;  

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    index index.php index-admin.php; 

    charset utf-8;

    location ~ /${MINIO_BUCKET}/(.*)/(.*)$ {
        set $dir $1;
        set $last_path_component $2;
        proxy_pass ${MINIO_URL}/${MINIO_BUCKET}/$dir/$last_path_component$is_args$args;
        proxy_pass_request_headers off;

        proxy_http_version 1.1;
        proxy_set_header Connection "";
        chunked_transfer_encoding off;
            
        proxy_hide_header Strict-Transport-Security;
        proxy_hide_header Content-Security-Policy;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        proxy_buffering off;
    }                                                       

    location / {
        try_files $uri $uri/ /index-admin.php?$query_string;
    }

    location ~ ^/api/(get-deals|get-deals/[^/]+|get-categories) {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;
    error_page 404 /index-admin.php;

    location ~ /index\.php$ {
        fastcgi_pass ${PHP_CONTAINER}:${PHP_PORT};
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /index-admin\.php$ { 
        fastcgi_pass ${PHP_CONTAINER_ADMIN}:${PHP_PORT};
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }


    location ~ /\.(?!well-known).* {
        deny all;
    }
}

I have both of the file in my public folder in the php code also i am using docker and here is my docker image

FROM harbor.management.fib.dc/proxy_cache/nginx:stable-alpine
WORKDIR /var/www/public/

RUN apk add --no-cache tzdata && echo "<?php" > index.php
RUN apk add --no-cache tzdata && echo "<?php" > index-admin.php

COPY conf/nginx.conf /etc/nginx/templates/default.conf.template
COPY conf/config.template.json /etc/nginx/templates/config.json.template

I have tried a lot of approach for the location of the index|index-admin .php but none of them work , appreciated your help on this


Solution

  • Note: This fix bellow is not recommended in the production as it is if evil to use if statement inside the location block

    After taking a lot of time researching this issue changing the above configuration to this one fixed my issue

        location / {
            
            try_files $uri $uri/ /index.php?$query_string;
    
            location ~ \.php$ { 
    
                if ($request_uri ~ ^/api/(get-deals|get-deals/([^/]+))|get-categories ) {
                    fastcgi_pass ${PHP_CONTAINER}:${PHP_PORT};
                }
                fastcgi_pass ${PHP_CONTAINER_ADMIN}:${PHP_PORT};
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                include fastcgi_params;
            }
    
        }
    

    $request_url will give me the path of the location i want to access like /api/test then using if statement to change container.