A
A
Artyom Alexandrov2018-05-09 15:34:54
Nginx
Artyom Alexandrov, 2018-05-09 15:34:54

How to configure nginx to handle cookies?

Existing config
server {
        error_page 404 = $document_root/404.html;
        set $cats $cookie_q;

        if ($cats = 0) {
                set $cats $args_q;
        }
        if ($cats = 0) {
                return 404;
        }

        listen 80;
        server_name site.ru www.site.ru;
        root /path/to/site/public_html;
        rewrite ^([^.\?]*[^/])$ $1/ permanent;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        location = /wp-login.php {
                deny all;
                return 404;
        }

        location ~* \.(git|htaccess|htpasswd)$ {
                deny all;
                return 404;
        }

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location ~ /\.ht {
                deny all;
                return 404;
        }

        location ~* \.(gif|jpeg|jpg|txt|png|tif|tiff|ico|jng|bmp|doc|pdf|rtf|xls|xlsx|ppt|rar|rpm|swf|zip|bin|deb|cur|ttf|woff|woff2|svg|xml|json|eot)$ {
                expires 180m;
        }

        location ~* \.(css|js)$ {
                expires 180m;
        }

        location / {
                fastcgi_intercept_errors on;
                try_files $uri $uri/index.php /index.php 404;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

At the moment it returns to any request:
siter.u/404/404/404/404/404/404/404/404/404/404/40...
in the cookie or get parameter q the website address is written.
I need the following:
if the q cookie is set, take the address from it and proxy the request there.
if there is no cookie, check the get parameter q to take the address from it and proxy the request there.
if there is no cookie or get parameter, issue 404.
How to implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Sundukov, 2018-05-10
@kradwhite

It is done standardly via map (written in the http section!):

map "$arg_q:$cookie_q" $my_upstream {
        default 404;
        "~^:(?<cookie>.+)$" $cookie;
        "~^(?<get>.+):$" $get;
    }

Then in the desired server you can use it like this:
server {
    if ($my_upstream = 404) {
        return 404;
    }
    ...
    proxy_pass http://$my_upstream;

There is a good description here, see the " Upstream Selection " section . Just for this case.
I draw your attention to the fact that in the original TOR the option "there is both a cookie and a get parameter" is not considered. What then? In the config I provided, it will be 404.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question