How to solve CORS error?
I found this artical:
"Enabling CORS for one or more containers in docker-letsencrypt-nginx-proxy-companion"
url:https://stackoverflow.com/questions/63159423/enabling-cors-for-one-or-more-containers-in-docker-letsencrypt-nginx-proxy-compa
but I can't solve my problem, it still shows the CORS error
Please help me to solve this problem, many thanks
testserver_include.conf:
location ~ ^/Crontab/.*$ {
allow 127.0.0.1;
allow "myip";
deny all;
break;
include /home/bryant/nginx/cors-setting.conf;
}
location ~ [^/].php(/|$) {
# limit_req zone=one burst=10;
include /home/bryant/nginx/cors-setting.conf;
fastcgi_connect_timeout 1800;
fastcgi_send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_split_path_info ^(.+?.php)(/.*)$;
try_files $uri $uri/ 404;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include /etc/nginx/fastcgi.conf;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location /Images/ {
expires 1y;
add_header Pragma public;
break;
include /home/bryant/nginx/cors-setting.conf;
}
location /Files/ {
if ($args) {
add_header Content-disposition "attachment; filename=$1";
}
include /home/bryant/nginx/cors-setting.conf;
}
location /HCTLabelImages/ {
include /home/bryant/nginx/cors-setting.conf;
}
location /Excel/ {
include /home/bryant/nginx/cors-setting.conf;
}
location / {
include /home/bryant/nginx/cors-setting.conf;
proxy_redirect off;
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_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://127.0.0.1:3336;
expires 1y;
}
location ~* .(jpg|jpeg|png|gif|ico|css|js|map)$ {
break;
}
location ~* .(eot|ttf|woff|woff2)$ {
add_header Access-Control-Allow-Origin *;
}
cors-setting.conf
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
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-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
}
1 Reply
Another user here may have more hands-on experience with CORS policies, but I did want to provide a response to get you started. First, did you restart your web server after making these changes? A restart is usually necessary to put any configuration changes into effect. You'll also want to ensure that your web server knows where to look for the CORS policies. I looked at the StackOverflow post you linked here, and it looks like your configuration file lines up with their recommendations. Could you provide the exact CORS error you're seeing here? That error should help to narrow down troubleshooting further. Reviewing this sample Nginx CORS config file may be helpful as well.