我也說說Nginx解決前端跨域問題,正確的Nginx跨域配置(後端Nginx CORS跨域配置、CORS設定,後端允許跨域請求)
最近連續兩個朋友問我跨域相關問題,我猜想可能不少朋友也遇到類似問題,我打算寫個部落格聊一下我實際使用的配置,
先說明一下,我並不太瞭解這配置,沒精力去了解太多,但我覺得其中有一些關鍵的小注意點,可能有些初學者不太注意到,導致配置有問題,本文章可能只對新手有點幫助,如果你有好配置,歡迎評論回覆,讓大家學習!
Nginx的CORS配置,網上太多這配置了,但大家更多的複製貼上、轉發,幾乎都是類似下面這三兩行:
add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
這樣有用麼?有用,我以前這樣使用也正常過,但後來還是遇到問題了,發現有些專案請求就不成功,也遇到有些瀏覽器成功,有些瀏覽器不成功;
我也在網上查詢各種資料和調整寫法,最後我調整好的寫法,基本的使用沒問題,我在專案中也一直使用著!
下面發一段我實際專案中的部分配置:
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; return 204; } root html; index index.html index.htm; proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 5; }
跨域相關的配置,主要是下面這部分:
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; return 204; }
下面簡單講解一下,以便大家配置成功!
1、Access-Control-Allow-Origin,這裡使用變數 $http_origin取得當前來源域,大家說用“*”代表允許所有,我實際使用並不成功,原因未知;
2、Access-Control-Allow-Credentials,為 true 的時候指請求時可帶上Cookie,自己按情況配置吧;
3、Access-Control-Allow-Methods,OPTIONS一定要有的,另外一般也就GET和POST,如果你有其它的也可加進去;
4、Access-Control-Allow-Headers,這個要注意,裡面一定要包含自定義的http頭欄位(就是說前端請求介面時,如果在http頭裡加了自定義的欄位,這裡配置一定要寫上相應的欄位),從上面可看到我寫的比較長,我在網上搜索一些常用的寫進去了,裡面有“web-token”和“app-token”,這個是我專案裡前端請求時設定的,所以我在這裡要寫上;
5、Access-Control-Expose-Headers,可不設定,看網上大致意思是預設只能獲返回頭的6個基本欄位,要獲取其它額外的,先在這設定才能獲取它;
6、語句“ if ($request_method = 'OPTIONS') { ”,因為瀏覽器判斷是否允許跨域時會先往後端發一個 options 請求,然後根據返回的結果判斷是否允許跨域請求,所以這裡單獨判斷這個請求,然後直接返回;
好了,按我上面配置基本都可使用(有些朋友確定只百度複製了兩行,然後說配置好了,跟前端朋友互扯),
下面發一個實際配置供參考,我做了少量更改,如下:
server {
listen 80;
server_name xxx.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;
return 204;
}
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:7071;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5;
}
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;
return 204;
}
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5;
}
location / {
root /var/www/xxx/wechat/webroot;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
(完)