之前和前端配合解決跨域問題時(shí)nginx大體是這樣配置的允粤,一直也沒問題:
server {
listen 80;
server_name xxxx;
root /data/www/zp/web/;
access_log /var/log/nginx/master.log;
error_log /var/log/nginx/master_error.log;
location / {
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://unix:/usr/gunicorn_sock/zp.sock;
uwsgi_send_timeout 5;
include uwsgi_params;
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'OPTIONS';
add_header Access-Control-Allow-Credentials true;
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;
}
}
}
在對應(yīng)的‘location’中加入OPTIONS允許配置厦坛。
一直這樣配置就沒問題了的。
現(xiàn)在換了工作換了服務(wù)器換了配合的前端同事
結(jié)果出問題了。
No 'Access-Control-Allow-Origin'又出現(xiàn)了
前端說ta用的axios框架挠铲,我之前同事用的fetch框架婴谱,我不懂不知道和這有沒有關(guān)哈蟹但,可能吧
也可能nginx版本問題,畢竟之前服務(wù)器兩年沒更新版本
最后查了很久才解決谭羔,還是我來改的nginx配置
最終nginx 配置
http
首先在 nginx.conf 根配置文件中华糖,http模塊里面加入這幾行配置:
add_header Access-Control-Allow-Origin *;
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;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS,PATCH;
我感覺主要就是卡在這了,一直沒在網(wǎng)上查到說在這配置的瘟裸。
server
server 也要添加
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS,PATCH;
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;
location
location添加 request_method 判斷 options請求客叉,{}中內(nèi)容和前面一樣
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS,PATCH;
add_header Access-Control-Allow-Credentials true;
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;
}
server 配置
server {
listen 80;
server_name ***;
index index.html index.htm index.php;
root /data/www/zp/web/;
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS,PATCH;
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;
location / {
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://unix:/usr/gunicorn_sock/zp.sock;
uwsgi_send_timeout 5;
include uwsgi_params;
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS,PATCH;
add_header Access-Control-Allow-Credentials true;
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;
}
}
access_log /var/log/nginx/zp/access_log.log;
error_log /var/log/nginx/zp/error_log.log;
}
我嘗試去刪掉server中配置,發(fā)現(xiàn)會報(bào)錯(cuò)景描∈欤看來三處是必須要加了。
在這做個(gè)備忘超棺,希望可以節(jié)省遇到同樣問題的朋友時(shí)間向族。