1. 程式人生 > 實用技巧 >nginx實現跨域訪問並支援(GET, POST,PUT,DELETE, OPTIONS)

nginx實現跨域訪問並支援(GET, POST,PUT,DELETE, OPTIONS)

最近有同事提出在使用客戶端跨域訪問的時候,發現伺服器對option請求返回了403,後來查看了網路添加了一段配置,發現option服務返回204了,但是後續的put操作也直接返回了204導致無法使用圖片上傳功能,經過一番查詢才發現,原來put等請求也需要定義,不然會直接使用option那段配置的請求

#首先nginx需要支援dav_module模組

./configure --prefix=/home/zqlx/apps/usr/webserver/nginx-1.12.0 --with-http_stub_status_module --with-http_ssl_module --user=zqlx --group=zqlx --with-pcre --with-pcre-jit --add-module=/tmp/nginx_upstream_check_module-master --with-stream --with-http_dav_module

#配置檔案加上以下配置


location /aaa {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
#if ($request_method = "OPTIONS") {
# add_header 'Access-Control-Allow-Origin' "*";
# add_header 'Access-Control-Allow-Credentials' "true";
# add_header 'Access-Control-Max-Age
' 86400; # add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE'; # add_header 'Access-Control-Allow-Headers' 'reqid, nid, host, x-real-ip, x-forwarded-ip, event-type, event-id, accept, content-type'; # add_header 'Content-Length' 0; # add_header 'Content-Type' 'application/json, charset=utf-8
'; # return 204; #} dav_methods PUT DELETE; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT,DELETE,OPTIONS'; add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; add_header 'Access-Control-Max-Age' 3600; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT,DELETE,OPTIONS'; add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } if ($request_method = 'GET') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST,DELETE,PUT,OPTIONS'; add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } if ($request_method = 'PUT') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT,DELETE,OPTIONS'; add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } if ($request_method = 'DELETE') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST,DELETE,PUT,OPTIONS'; add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } proxy_pass http://aaa/aaa; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }

我這裡是允許所有,也可以指定域名