Ste*_*ing 5 nginx conditional cors
我目前正在尝试使用 Nginx 有条件地返回一组 CORS 标头。起初,这似乎是一个简单的任务,因为我已经有了这个工作配置:
upstream api-app {
server unix:/tmp/api-app.sock fail_timeout=0;
}
server {
listen 80;
# [other server stuff...]
# CORS headers added to all ALL responses of this server (needed for all requests)
more_set_headers 'Access-Control-Allow-Methods: GET,POST,PATCH,PUT,DELETE,OPTIONS';
more_set_headers 'Access-Control-Allow-Origin: *';
more_set_headers 'Access-Control-Allow-Credentials: true';
more_set_headers 'Access-Control-Request-Method: GET,POST,PATCH,PUT,DELETE,OPTIONS';
more_set_headers 'Access-Control-Request-Headers: Content-Type';
more_set_headers 'Access-Control-Allow-Headers: Origin,X-Requested-With,Content-Type,Accept,Session-Id,Role-Id,Visitor-Id,X-Window-Location';
more_set_headers 'Access-Control-Expose-Headers: X-Total-Entries,X-Total-Pages,X-Page,X-Per-Page,X-Folder-Hierarchy';
location / {
# For OPTIONS request only return above headers and no content (also allow caching them)
if ($request_method = 'OPTIONS') {
# cache above (Access-Control) headers
add_header 'Access-Control-Max-Age' 600;
# set content length and type just for good measure:
add_header 'Content-Length' 0;
add_header 'Content-Type' 'text/plain charset=UTF-8';
# return 204 - no content
return 204;
}
# Forward requests to actual app (if all above conditions did not match)
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_read_timeout 35;
proxy_send_timeout 35;
proxy_pass http://api-app;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我只是想将 CORS 标头部分更改为 s.th. 像这样
# ...
set $cors '';
if ($http_origin ~ '^https?://(some.domain|some.other.domain|other-allows.domain)') {
set $cors 'true';
}
if ($cors = 'true') {
more_set_headers 'Access-Control-Allow-Methods: GET,POST,PATCH,PUT,DELETE,OPTIONS';
more_set_headers 'Access-Control-Allow-Origin: $http_origin';
more_set_headers 'Access-Control-Allow-Credentials: true';
more_set_headers 'Access-Control-Request-Method: GET,POST,PATCH,PUT,DELETE,OPTIONS';
more_set_headers 'Access-Control-Request-Headers: Content-Type';
more_set_headers 'Access-Control-Allow-Headers: Origin,X-Requested-With,Content-Type,Accept,Session-Id,Role-Id,Visitor-Id,X-Window-Location';
more_set_headers 'Access-Control-Expose-Headers: X-Total-Entries,X-Total-Pages,X-Page,X-Per-Page,X-Folder-Hierarchy';
}
location / {
# ...
Run Code Online (Sandbox Code Playgroud)
然而nginx -t
很快告诉我more_set_headers
不能在 if 语句中使用。add_header 也不能。
我发现在该location
部分它会起作用。但我已经知道这不是一个好的解决方案,因为我们都知道nginx 位置部分是否是邪恶的。我是对的,因为 OPTIONS 请求的 CORS 标头将会丢失,我已经有一个 if 情况(一个 OK 的情况,因为它返回)。
我还遇到了使用 Nginxs地图功能的选项。但这仅在我想更改标头值时有效,而不是在我想添加整个标头块时有效。
所以我的问题如下: 有没有办法有条件地(不使用地图)和位置块之外添加标头?理想情况下,允许包含 CORS 部分,这样我就可以在多个服务器(即 nginx 站点)中使用它。
我不知道是否more_set_headers
接受空字符串。如果它接受,那么您可以定义多个map
语句:
map $http_origin $cors_methods {
default "";
~^https?://(some.domain|some.other.domain|other-allows.domain) Access-Control-Allow-Methods: GET,POST,PATCH,PUT,DELETE,OPTIONS;
}
map $http_origin $cors_origin {
default "";
~^https?://(some.domain|some.other.domain|other-allows.domain) Access-Control-Allow-Origin: $http_origin;
}
Run Code Online (Sandbox Code Playgroud)
然后使用这个:
more_set_headers $cors_methods;
more_set_headers $cors_origin;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2136 次 |
最近记录: |