
Ich versuche, ein CORS-Problem zu debuggen. Hier ist meine Konfiguration, die ich verwendehttp://www.test-cors.org/um meine Nginx-Regeln zu testen. Ich erhalte die folgende Meldung in der Konsole meines Browsers, wenn ich die Methode OPTIONS verwende. Aber ich habe trotzdem die Daten erhalten, was sehr seltsam ist
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.
Ich erhalte die folgende Meldung, wenn ich die Methode GET verwende. Ich erhalte auch die Daten
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.
Hier ist meine aktualisierte Nginx-Konfiguration, 3. Update, und ich habe sie in eine neue Datei eingefügt.
❯ 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 ..."}';
}
}
}
Ich habe es so angefangen
sudo nginx -c /usr/local/etc/nginx/nginx-mini.conf
ps ax | grep nginx zeigt den Prozess
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 zeigt den TCP-Port an, der mit meinem Nginx verknüpft ist
❯ netstat -na|grep 8009
tcp4 0 0 *.8009 *.* LISTEN
Die IP-Adresse ist korrekt
❯ 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
Ich habe sichergestellt, dass ich mich mit curl mit meinem eigenen lokal laufenden Nginx-Server verbinde
❯ curl http://www.example.com:8009/testcors
{"code": 200, "reason": "Testing CORS ..."}%
Und die Ergebnisse sind immer noch dieselben (Screenshots der Chrome-Entwicklertools)https://i.stack.imgur.com/CB67D.jpg
Antwort1
Das Problem besteht darin, dass Sie in Ihrem keine CORS-Header senden location /testcors
.
server
Diese verschicken Sie für alle anderen Standorte ausschließlich im Block.
Der Grund hierfür ist, dass add_header
Anweisungen in Blöcken auf niedrigerer Ebenekomplett außer Kraft setzenadd_header
die in Blöcken höherer Ebene. Da Sie in Ihrem verwendet haben location
, müssen Sie auch alle anderen add_header
Anweisungen erneut einschließen.
Um Ihre Konfiguration DRY zu halten, sollten Sie Folgendes berücksichtigen:Erstellen einer include
Dateiwelches die gemeinsamen add_header
Anweisungen enthält, und include
diese dann an jedem relevanten Punkt in der Konfiguration ein.
Antwort2
JUHUU!!! Endlich habe ich es zum Laufen gebracht!!! Ich habe gelernt, dass wir nginx sagen müssen, was wir tun wollen, wenn es nicht mit dem Ursprung übereinstimmt. Mein erster Eindruck von Nginx ist, dass es Domänen automatisch blockiert, wenn sie unterschiedlich sind. 99 % der Beispiele, die ich gefunden habe, sagen das nicht.
# 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!";
}
}
Hier schlägt test-cors.org fehl :)https://i.stack.imgur.com/3kKXY.png Früher war es möglich, Daten abzurufen, auch wenn CORS-Fehler auftraten.