N
N
Nday0012020-06-25 11:47:17
Nginx
Nday001, 2020-06-25 11:47:17

nginx. How to use different php for different location?

There is NGINX used as a web server for example.com/site1 and as a reverse proxy for example.com/site2.
How to use the "built-in" php-fpm for site1, and proxy requests to site2?

I am attaching a conditional example of a part of the config.

location /site1 {
root /var/www/site1/;
}
location /site2/ {
proxy_pass https://site2/;
}

When added to the config, php stops working for the proxied site2. Trying to request https://example.com/site2/index.php gives 404 Not Found.
location ~* \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; # подключаем сокет php-fpm
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2020-06-25
@Nday001

nginx.org/ru/docs/http/ngx_http_core_module.html#l...

If the matched prefix location of the maximum length has the “^~” modifier, then the regular expressions are not checked.

Need to location /site2/add And add ^~
more insidelocation /site1 {location ~* \.php$
location /site1 {
    root /var/www/site1;

    location ~* \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; # подключаем сокет php-fpm
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

location ^~ /site2/ {
    proxy_pass https://site2/;
}

A
Alexey Dmitriev, 2020-06-25
@SignFinder

I suspect you need to elevate the location /site2/ block above the location ~* \.php$ block in the file.
They go in order of coincidence, if your php-fpm block is higher - all php, regardless of what is described below, goes to it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question