Nginx如果cookie存在不同的位置

时间:2018-05-03 07:47:22

标签: nginx cookies proxypass httpcookie

我有一个使用prerender服务的nginx配置文件, 我想检查cookie名称access_token是否存在然后提供本地角度, 如果cookie不存在(用户未登录),则代理将请求传递给prerender服务,

这是我的nginx配置文件,

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;
    }

}

这是nginx给我的错误:

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

我应该补充说美元符号已转义,因为此文件位于我的docker入口点内。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案 这是我的配置文件:

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;
    }

}

现在我知道nginx重写不只是用301或302重定向请求,它还可以服务器内容与其他位置相关。 所以我发现最重要的是重写!=重定向(301,302)