0

I'm trying to setup 2 Symfony 4 apps in the same server block (same domain) but php don't render the php files. Here is my config :

server {
    server_name mydomain.com;
    root /var/www/;

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    }

    location ~ ^/app1(/.*)$ {
        alias /home/app1/html/public;
        try_files $uri /index.php$is_args$args;

        location ~ ^/index\.php(/|$) {
                fastcgi_pass   php_stream;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include        fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME $request_filename;
        }
    }

    location ~ ^/app2(/.*)$ {
        alias /home/app2/html/public/;
        try_files $uri /index.php$is_args$args;

        location ~ ^/index\.php(/|$) {
                fastcgi_pass   php_stream;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include        fastcgi_params_;
                fastcgi_param  SCRIPT_FILENAME $realpath_root$request_filename;
                fastcgi_param  DOCUMENT_ROOT $realpath_root;
        }
    }

    location / {
        root /var/www/;
    }
}

If I put a static files in app's folder, it works but not with php files. it look after php files in the root folder /var/www/index.php instead of public app folder. It's like the alias directive is not taking into account.

Anyone has an idea on how to fix this ?

Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
  • Doesn't the "try_files" restart looking for a matching location, so "/index.php" is caught by "location /" ? – Gerard H. Pille Aug 03 '20 at 11:41
  • I have tried to delete the / (try_files $uri index.php$is_args$args) and still get an error (not the same) : open() "/etc/nginx/htmlindex.php" failed (2: No such file or directory). the folder /etc/nginx/html is the default one,not present in the config file – Guillaume MARIE Aug 03 '20 at 12:33
  • Please don't add `Solved` in the title of your question. On this site questions are marked as `Solved` when you accept an answer. It's perfectly fine to accept your own answer. – Gerald Schneider Aug 07 '20 at 06:41

2 Answers2

0

" If alias is used inside a location defined with a regular expression then such regular expression should contain captures and alias should refer to these captures (0.7.40), for example:

location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ {
    alias /data/w3/images/$1;
}

" See http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

Gerard H. Pille
  • 2,569
  • 1
  • 13
  • 11
0

Its seem that my problem is du to a bug in nginx (regarding the use of alias and try_file in the same block). this bug exist for more than 6 years and can't be corrected to avoid side effect (if I understand well). Here is a workaround that actually works :

upstream app1.fake {
    server 127.0.0.1;
}

upstream app2.fake {
    server 127.0.0.1;
}

upstream app1_stream {
    server unix:/var/run/php-fpm-app1.sock1 weight=100 max_fails=5 fail_timeout=5;
    server unix:/var/run/php-fpm-app1.sock2 weight=100 max_fails=5 fail_timeout=5;
}

upstream app2_stream {
    server unix:/var/run/php-fpm-app2.sock1 weight=100 max_fails=5 fail_timeout=5;
    server unix:/var/run/php-fpm-app2.sock2 weight=100 max_fails=5 fail_timeout=5;
}

server {
    server_name mydomain;

    listen 80;

    root /var/www/;
    index index.html;

    location /app1 {
        proxy_pass http://app1.fake;
    }

    location /app2 {
        proxy_pass http://app2.fake;
    }
}

server {
    server_name app1.fake;
    root /home/app1/html/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }
        location ~ ^/index\.php(/|$) {
                fastcgi_pass app1_stream;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT $realpath_root;
        }
}

server {
    server_name app2.fake;
    root /home/app2/html/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }
        location ~ ^/index\.php(/|$) {
                fastcgi_pass app2_stream;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT $realpath_root;
        }
}