Nginx If cookie exist different location
Asked Answered
K

1

8

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 .

Knitting answered 3/5, 2018 at 7:47 Comment(1)
I don't know how to serve conditional data using if directive in nginx configuration file, It seems that nginx if has a weird result.Knitting
K
5

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)

Knitting answered 3/5, 2018 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.