proxy if cookie + map

Валентин Бартенев vbart at nginx.com
Thu Aug 28 16:27:33 UTC 2014


On Thursday 28 August 2014 10:36:36 sejo412 wrote:
> Спасибо, решение уже где-то рядом )
> На боевом сервере за все отвечает php-скрипт.
> Сделал так:
>         location / {
>           if ($http_cookie ~ 'newsitetest') {

Зачем так усложнять.
В nginx есть специальные переменные для работы с куками: $cookie_*.

К изучению: http://nginx.org/ru/docs/http/ngx_http_core_module.html#variables


>                 rewrite ^ /newsite/$uri last;
>           }
>                 rewrite / /not_found.php?query_uri=/&$args;
>           }
> 
>         location ~* ^/(.+)$ {

Вам не нужен этот location с регуляркой.


>           if ($http_cookie ~ 'newsitetest') {
>                 rewrite ^ /newsite/$uri last;
>           }
>
>           try_files $uri $uri/ /not_found.php?query_uri=/$1&$args;
>           fastcgi_pass   127.0.0.1:9000;
>           fastcgi_index  index.html;
>           include        fastcgi_params;
>           fastcgi_param  SCRIPT_FILENAME
> $document_root$fastcgi_script_name;
>           fastcgi_param  QWERTY   $document_root$fastcgi_script_name;
>         }
> 
>         location /newsite/ {
>                 proxy_pass http://192.168.2.146;

Есть большая разница между:

  proxy_pass http://192.168.2.146;

и

  proxy_pass http://192.168.2.146/;

К изучению: http://nginx.org/r/proxy_pass/ru

>                 proxy_http_version 1.1;
>                 proxy_set_header Connection "";
>                 proxy_set_header Host $host;
>                 proxy_set_header X-Real-IP $remote_addr;
>                 proxy_set_header X-Forwarded-For
> $proxy_add_x_forwarded_for;
>         }
> 
> После установки куки браузер выдает 500 ошибку, в логах nginx (который
> проксирует) ругань на циклический редирект
> 2014/08/28 18:21:51 [error] 19193#0: *454 rewrite or internal redirection
> cycle while processing
> 
"/newsite//newsite//newsite//newsite//newsite//newsite//newsite//newsite//newsite//newsite//not_found.php"
> 

Ваш конфиг целиком должен выглядеть так:

    location / {

        if ($cookie_newsitetest) {
            rewrite ^ /newsite/$uri last;
        }

        try_files $uri $uri/ /not_found.php?query_uri=$uri&$args;

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.html;

        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  QWERTY   $document_root$fastcgi_script_name;
    }

    location /newsite/ {
        internal;

        proxy_pass http://192.168.2.146/;
        proxy_http_version 1.1;

        proxy_set_header Connection "";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

--
Валентин Бартенев


Подробная информация о списке рассылки nginx-ru