K
K
kashtanoff2014-04-17 10:51:49
PHP
kashtanoff, 2014-04-17 10:51:49

Nginx: different root depending on cookies, partially working, problem with PHP - how to fix?

There is such a code, in both directories (root1 and root2) there are identical copies of the site (Yii)

server {

        root /root1;

        index  index.html index.htm index.php;

        server_name domain.ru;

        location / {
                if ($cookie_VAR = "1") {
                        root /root2;
                }

                try_files $uri $uri/ /index.php?$args;
        }

        #error_page 500 502 503 504 /50x.html;

        location ~ \.php$ {
                if ($cookie_VAR = "1") {
                    root /root2;
                }

                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;

                fastcgi_index index.php;
                include fastcgi_params;

                fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;

        }
}

When the cookie is set, only the main page of the second site works, the rest does not work.
CNC is configured on the sites, in the absence of cookies, everything works correctly, and requests like http://domain.ru/some_path go to index.php.
If there is a cookie, everything except domain.ru returns 404. Pictures, etc. are returned normally.
The site in the second directory, when linked to a separate domain, works fully.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2014-04-17
@kashtanoff

In general, the root can only be set once. Try something like this, I checked, it seems to work.

server {
        index  index.html index.htm index.php;

        server_name test.loc;
        
        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        #error_page 500 502 503 504 /50x.html;

        location ~ \.php$ {
            if ($cookie_VAR = "1") {
                root /Users/evgenij/projects/www/1;
            }
        
            if ($cookie_VAR = "2") {
                root /Users/evgenij/projects/www/2;
            }

            root /Users/evgenij/projects/www/3;
            
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;

            fastcgi_index index.php;
            include fastcgi_params;

            fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_script_name;
        }
}

Tested like this
curl --cookie "VAR=1" http://test.loc
curl --cookie "VAR=2" http://test.loc
curl http://test.loc

E
Eugene, 2014-04-17
@Nc_Soft

Why do you need 2 identical applications depending on the cookie to run?

K
kashtanoff, 2014-04-17
@kashtanoff

In fact, they will be slightly different, but at the moment for the purposes of testing the functionality, they are the same.
You need to make sure that everything works identically.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question