nginx: multiple hosts for single index.php

There are several directories of type /var/www/site_n/which looks up the virtual host and which are user files, and logs.

There is a directory /usr/lib/cms/ CMS engine and file index.php that should run with different environment depending on the settings of the host in nginx.

Trying to set this config on the example of one domain:
server {

 server_name site1.com; 
 root /var/www/site1.com/public_html/;
 set $app_root /usr/lib/cms/;
 set $app_key 454647;

 access_log /var/www/site1.com/logs/access.log;
 error_log /var/www/site1.com/logs/error.log;

 location / {
 index index.php index.html index.htm;
 try_files $uri $uri/ $app_root/index.php?$args;
}

 location ~ \.php$ {
 try_files $uri @php; 
 location php5-fpm-sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $app_root$fastcgi_script_name;
 fastcgi_param app-key $app_key;
 include fastcgi_params;
 } 

 location @php {
 location php5-fpm-sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $app_root/index.php;
 fastcgi_param SCRIPT_NAME /index.php;
 fastcgi_param QUERY_STRING q=$uri&$args;
 include fastcgi_params;
}

}

When you try to query addresses site1.com get a 403 error. Nginx writes logs that searches for the file in the website directory and not in the folder of the engine.
Please help to configure correctly. And in General, how is this approach correct?
October 3rd 19 at 05:18
4 answers
October 3rd 19 at 05:20
Something I don't like your config.
Try this:
 location / {
 index index.php index.html index.htm;
 try_files $uri $uri/ @php;
}

 location ~ \.php$ {
 location php5-fpm-sock;
 fastcgi_index /;
 fastcgi_param SCRIPT_NAME index.php;
 fastcgi_param SCRIPT_FILENAME $app_root/index.php;
 fastcgi_param app-key $app_key;
 include fastcgi_params;
 } 

 location @php {
 location php5-fpm-sock;
 fastcgi_index /;
 fastcgi_param SCRIPT_NAME index.php;
 fastcgi_param SCRIPT_FILENAME $app_root/index.php;
 fastcgi_param QUERY_STRING q=$uri&$args;
 include fastcgi_params;
}
Another would be to remove the standardization from the configs: nginx.org/en/docs/faq/variables_in_config.html - Idella_Simon commented on October 3rd 19 at 05:23
Gathered for your advice. Address: site.com/index.php work out right, but from the root ( site.com ) does not work. I.e. the server does not understand that to run index.php need directory cms not website.
Probably stupid, but to understand what the problem is, I couldn't.
Here is a piece of config:
 location / {
 root /var/www/site.com/public_html;
 index index.php index.html index.htm;
 try_files $uri $uri/ @php;
}

 location ~ \.php$ {
 location php5-fpm-sock;
 root /usr/lib/cms;
 fastcgi_index /index.php;
 include fastcgi_params;
 fastcgi_param SCRIPT_NAME index.php;
 fastcgi_param SCRIPT_FILENAME /usr/lib/cms/index.php;
 fastcgi_param app-key 4567890;
}

 location @php {
 root /usr/lib/cms;
 location php5-fpm-sock;
 fastcgi_index /index.php;
 include fastcgi_params;
 fastcgi_param SCRIPT_NAME index.php;
 fastcgi_param SCRIPT_FILENAME /usr/lib/cms/index.php;
 fastcgi_param QUERY_STRING q=$uri&$args;
}
- hiram52 commented on October 3rd 19 at 05:26
Enable debug log in nginx and see where it is forwarded. - Kristina_Cart commented on October 3rd 19 at 05:29
October 3rd 19 at 05:22
I understand the meaning of this action — extreme savings in the absence of code duplication cms. At least don't do that, because you can't 100% guarantee that the version of the cms for each site will be the same. In the extreme case, make a symlink.
October 3rd 19 at 05:24
try_files $uri $uri/ $app_root/index.php?$args the file has no settings for this. But no one bothers to change these things on try_files $uri php.
October 3rd 19 at 05:26
just a link to index.php try

Find more questions by tags Nginx