F
F
fredrsf2014-01-16 12:14:46
Nginx
fredrsf, 2014-01-16 12:14:46

How to change nginx root directory depending on cookies?

Good afternoon, it is necessary to replace the root directory for root requests depending on cookies. Something like

root /path/to/rails/app/public;
if ($http_cookie ~* "dev_access") {
  root /path/to/rails/app2/public;
}

Is it somehow possible to implement this? Depending on what else can you change the directory?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolai Vasilchuk, 2014-01-16
@fredrsf

Reading the documentation :

syntax: root path;
default: roothtml;
context: http, server, location, if in location
That's how it works.
root /var/www/default;
location / {
    if ($http_cookie ~ 'test') {
        root /var/www/test;
    }
    try_files $uri $uri/ /index.html;
}

K
kashtanoff, 2014-04-17
@kashtanoff

There is such a code, in both directories the same 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;
                }

                *********
        }
}

When the cookie is set, only the main page works, the rest does not work.
The site in the second directory, when linked to a separate domain, works fully.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question