Nginx 如果 cookie 存在不同的位置
Nginx If cookie exist different location
我有一个使用预呈现服务的 nginx 配置文件,
我想检查 cookie 名称 access_token 是否存在然后服务于本地 angular ,
如果 cookie 不存在(用户未登录),则代理将请求传递给预渲染服务,
这是我的 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 入口点内。
我找到了解决方案
这是我的有效配置文件:
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 rewrite不仅仅是用301或302重定向请求,它还可以服务器与其他位置相关的内容。
所以我发现最重要的是重写 != redirect (301, 302)
我有一个使用预呈现服务的 nginx 配置文件, 我想检查 cookie 名称 access_token 是否存在然后服务于本地 angular , 如果 cookie 不存在(用户未登录),则代理将请求传递给预渲染服务,
这是我的 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 入口点内。
我找到了解决方案 这是我的有效配置文件:
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 rewrite不仅仅是用301或302重定向请求,它还可以服务器与其他位置相关的内容。 所以我发现最重要的是重写 != redirect (301, 302)