给 PHP 接口弄了个站点配置,代码已经设置好跨域
如果是在 PC 端访问 https://domain.com/demo/ 能访问,正常
但如果前端 用 ajax 请求 https://domain.com/demo/ 报跨域错误:
Access to XMLHttpRequest at 'https://domain.com/demo/' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
但奇怪的是代码不变,把接口地址改成具体的 php 文件地址如: https://domain.com/demo/index.php ,就正常了
站点配置:
server {
listen 80;
server_name _;
root /var/www;
index index.php index.html;
client_max_body_size 50m;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php:9000;
try_files $uri =404;
}
}