我也說說Nginx解決前端跨域問題,正確的Nginx跨域配置(后端Nginx CORS跨域配置、CORS設(shè)置,后端允許跨域請(qǐng)求)

最近連續(xù)兩個(gè)朋友問我跨域相關(guān)問題,我猜想可能不少朋友也遇到類似問題,我打算寫個(gè)博客聊一下我實(shí)際使用的配置,

先說明一下,我并不太了解這配置,沒精力去了解太多,但我覺得其中有一些關(guān)鍵的小注意點(diǎn),可能有些初學(xué)者不太注意到,導(dǎo)致配置有問題,本文章可能只對(duì)新手有點(diǎn)幫助,如果你有好配置,歡迎評(píng)論回復(fù),讓大家學(xué)習(xí)!

Nginx的CORS配置,網(wǎng)上太多這配置了,但大家更多的復(fù)制粘貼、轉(zhuǎn)發(fā),幾乎都是類似下面這三兩行:


add_header Access-Control-Allow-Origin *;

add_header Access-Control-Allow-Headers X-Requested-With;

add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

這樣有用么?有用,我以前這樣使用也正常過,但后來還是遇到問題了,發(fā)現(xiàn)有些項(xiàng)目請(qǐng)求就不成功,也遇到有些瀏覽器成功,有些瀏覽器不成功;

我也在網(wǎng)上查找各種資料和調(diào)整寫法,最后我調(diào)整好的寫法,基本的使用沒問題,我在項(xiàng)目中也一直使用著!

下面發(fā)一段我實(shí)際項(xiàng)目中的部分配置:


location /aoda-web {

    add_header'Access-Control-Allow-Origin'$http_origin;

    add_header'Access-Control-Allow-Credentials''true';

    add_header'Access-Control-Allow-Methods''GET, POST, OPTIONS';

    add_header'Access-Control-Allow-Headers''DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X- Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';

   add_header'Access-Control-Expose-Headers''Content-Length,Content-Range';

    if($request_method='OPTIONS') {

        add_header'Access-Control-Max-Age'1728000;

        add_header'Content-Type''text/plain; charset=utf-8';

        add_header'Content-Length'0;

        return204;

    }

     roothtml;

      indexindex.html index.htm;

        proxy_passhttp://127.0.0.1:8080;

        proxy_set_headerHost$host;

        proxy_set_headerX-Real-IP$remote_addr;

        proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;

        proxy_set_headerX-Forwarded-Proto$scheme;

        proxy_connect_timeout5;

}

下面簡(jiǎn)單講解一下,以便大家配置成功!

1、Access-Control-Allow-Origin,這里使用變量 $http_origin取得當(dāng)前來源域,大家說用“*”代表允許所有,我實(shí)際使用并不成功,原因未知;

2、Access-Control-Allow-Credentials,為 true 的時(shí)候指請(qǐng)求時(shí)可帶上Cookie,自己按情況配置吧;

3、Access-Control-Allow-Methods,OPTIONS一定要有的,另外一般也就GET和POST,如果你有其它的也可加進(jìn)去;

4、Access-Control-Allow-Headers,這個(gè)要注意,里面一定要包含自定義的http頭字段(就是說前端請(qǐng)求接口時(shí),如果在http頭里加了自定義的字段,這里配置一定要寫上相應(yīng)的字段),從上面可看到我寫的比較長(zhǎng),我在網(wǎng)上搜索一些常用的寫進(jìn)去了,里面有“web-token”和“app-token”,這個(gè)是我項(xiàng)目里前端請(qǐng)求時(shí)設(shè)置的,所以我在這里要寫上;

5、Access-Control-Expose-Headers,可不設(shè)置,看網(wǎng)上大致意思是默認(rèn)只能獲返回頭的6個(gè)基本字段,要獲取其它額外的,先在這設(shè)置才能獲取它;

6、語句“ if ($request_method = 'OPTIONS') { ”,因?yàn)闉g覽器判斷是否允許跨域時(shí)會(huì)先往后端發(fā)一個(gè) options 請(qǐng)求,然后根據(jù)返回的結(jié)果判斷是否允許跨域請(qǐng)求,所以這里單獨(dú)判斷這個(gè)請(qǐng)求,然后直接返回;

好了,按我上面配置基本都可使用(有些朋友確定只百度復(fù)制了兩行,然后說配置好了,跟前端朋友互扯),

下面發(fā)一個(gè)實(shí)際配置供參考,我做了少量更改,如下:


server{

        listen80;

        server_namexxx.com;

        location/xxx-web/papi {

        add_header'Access-Control-Allow-Origin'$http_origin;

        add_header'Access-Control-Allow-Credentials''true';

        add_header'Access-Control-Allow-Methods''GET, POST, OPTIONS';

        add_header'Access-Control-Allow-Headers''DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';

        add_header'Access-Control-Expose-Headers''Content-Length,Content-Range';

        if($request_method='OPTIONS') {

            add_header'Access-Control-Max-Age'1728000;

            add_header'Content-Type''text/plain; charset=utf-8';

            add_header'Content-Length'0;

            return204;

        }

       roothtml;

        indexindex.html index.htm;

        proxy_passhttp://127.0.0.1:7071;

        proxy_set_headerHost$host;

        proxy_set_headerX-Real-IP$remote_addr;

        proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;

        proxy_set_headerX-Forwarded-Proto$scheme;

        proxy_connect_timeout5;

    }

    location/xxx-web {

        add_header'Access-Control-Allow-Origin'$http_origin;

        add_header'Access-Control-Allow-Credentials''true';

        add_header'Access-Control-Allow-Methods''GET, POST, OPTIONS';

        add_header'Access-Control-Allow-Headers''DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';

        add_header'Access-Control-Expose-Headers''Content-Length,Content-Range';

        if($request_method='OPTIONS') {

                add_header'Access-Control-Max-Age'1728000;

                add_header'Content-Type''text/plain; charset=utf-8';

                add_header'Content-Length'0;

                return204;

    }

      roothtml;

      indexindex.html index.htm;

      proxy_passhttp://127.0.0.1:8080;

       proxy_set_headerHost$host;

        proxy_set_headerX-Real-IP$remote_addr;

        proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;

        proxy_set_headerX-Forwarded-Proto$scheme;

        proxy_connect_timeout5;

    }

    location/ {

        root/var/www/xxx/wechat/webroot;

        indexindex.html index.htm;

    }

    error_page500502503504/50x.html;

    location= /50x.html {

    roothtml;

    }

}

來源于:https://blog.csdn.net/envon123/article/details/83270277

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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