我目前正在尝试使用 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') {
# …
Run Code Online (Sandbox Code Playgroud)