Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx If cookie exist different location

I have a nginx config file which is using prerender service , I want to check if a cookie names access_token exist then serve local angular , and if cookie is not exist (user is not logged in ) then proxy pass request to prerender service ,

and here is my nginx config file,

proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=prerendercloud_cache:10m max_size=100m inactive=60m use_temp_path=off;

server {
    set \$root /var/www/;
    resolver 8.8.8.8;
    set \$prerendercloud 'service.prerender.cloud';

    listen 80;
    server_name _; 

    location = / {
        if (\$http_cookie ~* "access_token" ) {
            root \$root;
            try_files \$uri \$uri/ /index.html;
            index index.html;
        }

        root \$root;
        try_files \$uri @prerendercloud;
    }

    location / {
        root \$root;
        try_files \$uri \$uri/ /index.html;
        index index.html;
    }



    location @prerendercloud {
        proxy_set_header Accept-Encoding "gzip";
        gunzip on;
        root \$root;
        proxy_set_header X-Prerender-Token 'my_token';
        proxy_cache prerendercloud_cache;
        proxy_cache_valid 200 5m;
        proxy_intercept_errors on;
        error_page 429 500 502 503 504 =200 /index.html;
        proxy_cache_use_stale updating;
        set \$prerender 0;
        if (\$uri ~ "^(([^.]|\.html?)+)\$") {
            set \$prerender "EXTENSION";
        }
        set \$prerender "\${prerender}-USER_AGENT";
        if (\$http_x_prerendered ~ true) {
            set \$prerender 0;
        }
        if (\$http_user_agent ~ "prerendercloud") {
            set \$prerender 0;
        }
        proxy_cache_key "\$prerender\$host\$uri";

        if (\$prerender = "EXTENSION-USER_AGENT") {
            rewrite .* /\$scheme://\$host\$request_uri? break;
            proxy_pass http://\$prerendercloud\$uri;
            break;
        }
        rewrite .* /index.html break;
    }

}

and here is error which nginx gives me :

nginx: [emerg] "try_files" directive is not allowed here in /etc/nginx/conf.d/default.conf:30

I should add that dollar signs are escaped because this file is inside my docker entrypoint .

like image 833
Majid Abdolhosseini Avatar asked May 03 '18 07:05

Majid Abdolhosseini


1 Answers

I found the solution and here is my config file which works :

proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=prerendercloud_cache:10m max_size=100m inactive=60m use_temp_path=off;

server {
    set $root /var/www/;
    resolver 8.8.8.8;
    set $prerendercloud 'service.prerender.cloud';
    listen 80;
    server_name _;

    root $root;

    location = / {
        try_files $uri @prerendercloud;
    }


    location / {
        try_files $uri $uri/index.html $uri/ /index.html;
        index index.html;
    }

    location @prerendercloud {
        proxy_set_header Accept-Encoding "gzip";
        gunzip on;
        proxy_set_header X-Prerender-Token 'my_token';
        proxy_cache prerendercloud_cache;
        proxy_cache_valid 200 5m;
        proxy_intercept_errors on;
        error_page 429 500 502 503 504 =200 /index.html;
        proxy_cache_use_stale updating;
        set $prerender 0;
        if ($uri ~ "^(([^.]|\.html?)+)$") {
            set $prerender "EXTENSION";
        }
        set $prerender "${prerender}-USER_AGENT";
        if ($http_x_prerendered ~ true) {
            set $prerender 0;
        }

        if ($http_user_agent ~ "prerendercloud") {
            set $prerender 0;
        }

        if ($http_cookie ~* "access_token" ) {
            set $prerender 0;
        }

        proxy_cache_key "$prerender$host$uri";



        if ($prerender = "EXTENSION-USER_AGENT") {

            rewrite .* /$scheme://$host$request_uri? break;
            proxy_pass http://$prerendercloud$uri;
            break;
        }

        rewrite .* /index.html break;
    }

}

now I know that nginx rewrite is not just about redirecting request with 301 or 302, It also can server content related to other location. so the most important thing I found out is rewrite != redirect (301, 302)

like image 111
Majid Abdolhosseini Avatar answered Sep 24 '22 23:09

Majid Abdolhosseini