之前和前端配合解決跨域問(wèn)題時(shí)nginx大體是這樣配置的,一直也沒(méi)問(wèn)題:
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;
}
}
}
在對(duì)應(yīng)的‘location’中加入OPTIONS允許配置。
一直這樣配置就沒(méi)問(wèn)題了的。
現(xiàn)在換了工作換了服務(wù)器換了配合的前端同事
結(jié)果出問(wèn)題了。
No 'Access-Control-Allow-Origin'又出現(xiàn)了
前端說(shuō)ta用的axios框架,我之前同事用的fetch框架,我不懂不知道和這有沒(méi)有關(guān)哈,可能吧
也可能nginx版本問(wèn)題,畢竟之前服務(wù)器兩年沒(méi)更新版本
最后查了很久才解決,還是我來(lái)改的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;
我感覺(jué)主要就是卡在這了,一直沒(méi)在網(wǎng)上查到說(shuō)在這配置的。
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請(qǐng)求,{}中內(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)會(huì)報(bào)錯(cuò)??磥?lái)三處是必須要加了。
在這做個(gè)備忘,希望可以節(jié)省遇到同樣問(wèn)題的朋友時(shí)間。