server { listen 14011; server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /;
index index.html index.htm;
}
location /upload {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
return 204;
}
auth_request /auth;
root /;
}
location /auth {
internal;
proxy_set_header Host $host;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_pass http://127.0.0.1:14010/auth;
}
}
====================================================================== 以上是我的配置,因为需要访问静态资源要鉴权,所以需要携带请求头发请求,但是发送 OPTIONS 请求的时候跨域了,加上了网上说的配置还是不行,求助。
1
Keppel OP QAQ
|
2
ch2 2021-03-19 11:03:53 +08:00
把 if ($request_method = 'OPTIONS') 去掉
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; 这条就够了,外面不用再套个 if |
4
NjcyNzMzNDQ3 2021-03-19 15:34:10 +08:00
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; # !!!!重要 普通 Ajax 请求直接发起请求不用添这个设置。 # Vuejs 会先发起 option 请求,而 add_header 支持的 http 状态码有限,必须添加 http_code 返回 204 if ($request_method = 'OPTIONS') { return 204; } |
5
NjcyNzMzNDQ3 2021-03-19 15:34:28 +08:00
试试我这个,我之前就这么配置的
|
6
Keppel OP @NjcyNzMzNDQ3 我能说这个我也试过吗 =。=
|
7
Keppel OP add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods *; add_header Access-Control-Allow-Headers *; add_header Access-Control-Allow-Credentials: true; if ($request_method = 'OPTIONS') { return 204; } 最后改成了这样解决的,和#4 差不多,但是全通配了。 |