
我正在嘗試調試 CORS 問題。這是我正在使用的配置http://www.test-cors.org/測試我的 Nginx 規則。當我使用的方法是 OPTIONS 時,我在瀏覽器控制台中收到以下訊息。但我仍然收到了很奇怪的數據
Failed to load http://www.example.com:8009/testcors: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.test-cors.org' is therefore not allowed access.
如果我使用的方法是 GET,我會收到以下訊息。我也拿到數據了
Failed to load http://www.example.com:8009/testcors: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.test-cors.org' is therefore not allowed access.
這是我更新的 nginx 配置,第三次更新,我將其放在一個新檔案中。
❯ cat /usr/local/etc/nginx/nginx-mini.conf
worker_processes 1;
worker_rlimit_nofile 15000;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
events {
worker_connections 5000;
accept_mutex off;
}
http {
include mime.types;
default_type application/octet-stream;
proxy_cookie_path / "/; HTTPOnly; Secure";
types_hash_max_size 4096;
access_log off;
sendfile off;
sendfile_max_chunk 512k;
tcp_nopush off;
tcp_nodelay on;
output_buffers 1 3m;
open_file_cache max=10000 inactive=5m;
open_file_cache_valid 2m;
open_file_cache_min_uses 1;
open_file_cache_errors on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_http_version 1.1;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
application/atom+xml
application/javascript
application/json
application/rss+xml
application/vnd.ms-fontobject
application/x-font-ttf
application/x-javascript
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
application/xml+rss
font/opentype
image/svg+xml
image/x-icon
text/css
text/javascript
text/js
text/plain
text/x-component
text/xml;
# CORS
map $http_origin $allow_origin {
default "";
~example.com "$http_origin";
}
server {
listen 8009;
server_name www.example.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log debug;
location /testcors {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $allow_origin;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Max-Age' 60;
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' $allow_origin;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' $allow_origin;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
add_header GETMETHOD accessed;
add_header Content-Type "application/json; charset=utf-8";
}
add_header Content-Type "application/json; charset=utf-8";
return 200 '{"code": 200, "reason": "Testing CORS ..."}';
}
}
}
我是這樣開始的
sudo nginx -c /usr/local/etc/nginx/nginx-mini.conf
ps 斧頭 | grep nginx 顯示進程
31528 ?? Ss 0:00.00 nginx: master process nginx -c /usr/local/etc/nginx/nginx-mini.conf
31529 ?? S 0:00.00 nginx: worker process
31787 s003 R+ 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn nginx
netstat 顯示與我的 nginx 關聯的 tcp 連接埠
❯ netstat -na|grep 8009
tcp4 0 0 *.8009 *.* LISTEN
IP位址正確
❯ ping www.example.com
PING airborne.gogoinflight.com (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.042 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.067 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.132 ms
我確保使用curl 連接到我自己的本地運行的nginx 伺服器
❯ curl http://www.example.com:8009/testcors
{"code": 200, "reason": "Testing CORS ..."}%
結果還是一樣(chrome開發工具截圖)https://i.stack.imgur.com/CB67D.jpg
答案1
問題是您沒有在location /testcors
.
您只需將這些內容傳送到server
任何其他位置的區塊中。
這樣做的原因是add_header
較低層級區塊中的指令完全覆蓋那些在更高級別的塊中。因此,因為您已經add_header
在 中使用了location
,所以您也必須再次包含所有其他add_header
指令。
為了保持你的配置乾燥,你應該考慮製作include
文件其中包含通用add_header
指令,然後include
它位於配置中的每個相關點。
答案2
哇哦!我終於開始工作了!我了解到,如果它與來源不匹配,我們必須告訴 nginx 我們想要做什麼。我對 Nginx 的第一印像是,如果網域不同,它會自動阻止網域。我找到的99%的例子都沒有提到這一點。
# CORS
map $http_origin $allow_origin {
default "blocked";
~example.com "allowed";
}
map $allow_origin $origin_is_allowed {
allowed $http_origin;
}
location ~ /getapi/?(?<capture>.*) {
if ($allow_origin = 'allowed') {
add_header 'Access-Control-Allow-Origin' "$origin_is_allowed";
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
add_header Content-Type "application/json; charset=utf-8";
proxy_pass http://localhost:7777/api/$capture;
}
if ($allow_origin = 'blocked') {
add_header Content-Type "text/plain";
return 403 "Your domain is not allowed!";
}
}
這就是 test-cors.org 失敗的地方:)https://i.stack.imgur.com/3kKXY.png 之前即使出現cors錯誤也能夠取得數據