How to configure nginx for handling cookies?

The 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;
 location 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, returns for any query :
siter.u/404/404/404/404/404/404/404/404/404/404/40...

in a cookie or get parameter q is the recorded address of the site.

I need the following:
if set to cook q to take with her address and proxying a request there.
if the cookie is not present to validate the get parameter q to take with him the address and proxy the request there.
if there is no cookie get no option to give 404.
How to implement it?
June 5th 19 at 23:01
1 answer
June 5th 19 at 23:03
Solution
Is done through standard map (written in the http section!):
map "$arg_q:$cookie_q" $my_upstream {
 default 404;
 "~^:(?<cookie>.+)$" $cookie;
 "~^(?<get>.+):$" $get;
 }</get></cookie>

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

Good description is here, see the section "upstream Choice". Under the above-mentioned case.
Note that in the original specification considered the option of "eat and cook and get parameter". What then? In the above config I will be 404.
thank you! - rose.Lindgren commented on June 5th 19 at 23:06

Find more questions by tags HTTP CookiesNginx