A
A
ArmiT2013-11-05 16:44:28
Nginx
ArmiT, 2013-11-05 16:44:28

nginx: multiple hosts per index.php

There are several directories like /var/www/site_n/ that the virtual host looks at and contains user files and logs. There is a /usr/lib/cms/

directory with the CMS engine and an index.php file, which should be launched with a different environment depending on the host settings in nginx. I'm trying to configure with this config using 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;        
            fastcgi_pass   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 {
            fastcgi_pass 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 I try to request site1.com, I get a 403 error. Nginx writes in the logs that it is looking for the file in the site directory, and not in the engine folder.
Please help me set it up correctly. And in general, how correct is this approach?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Nikolai Vasilchuk, 2013-11-05
@Anonym

I don't like your config.
Try like this:

        location / {
            index index.php index.html index.htm;
            try_files $uri $uri/ @php;
        }

        location ~ \.php$ {
            fastcgi_pass   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 {
            fastcgi_pass 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;
           }

S
Sergey, 2013-11-05
Protko @Fesor

I understand the meaning of this action - saving on matches in the form of the absence of duplication of the code of the cms itself. At least you should not do this, because you cannot guarantee 100% that the cms version for each of the sites will be the same. As a last resort, make a symlink.

R
rozhik, 2013-11-05
@rozhik

try_files $uri $uri/ $app_root/index.php ?$args The file has no parameters - that's why. But no one bothers to change this to try_files $uri php .

D
Diam0n, 2013-11-05
@Diam0n

just link to index.php try

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question