nginx 解決跨域問(wèn)題 No 'Access-Control-Allow-Origin'

之前和前端配合解決跨域問(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í)間。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容