这让我很沮丧。我得到了那句经典台词:
请求的资源上不存在“Access-Control-Allow-Origin”标头。
我在 Ubuntu 16.04 上运行最新的稳定 Nginx,配置了 Swoole 服务器以处理 PHP 7.2 中的 Laravel PHP 请求。据我所知,一切正常。
Laravel api 在一个子域上,前端是另一个子域上的 angular。到目前为止一切正常,所以我一直在尝试在后端站点上配置 CORS。
尽管进行了配置,但它并没有意识到它的存在。是的,我每次更新配置时都会重新启动 nginx。出于开发目的,缓存目前已关闭。Nginx 配置缩短如下:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
root /var/www/sub.example.com/;
index index.php;
server_name sub.example.com www.sub.example.com;
[redirect 301 part]
[php location]
}
## https://example.com redirects to https://www.example.com
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.sub.example.com;
[SSL certs]
[redirect 301 to sub.example.com]
}
## Serves https://sub.example.com
server {
server_name lhb.luminuxlab.com;
listen 443 ssl http2 ;
listen [::]:443 ssl http2;
[SSL certs]
root /var/www/sub.example.com;
index index.php;
location / {
try_files $uri $uri/ /index.php?@swoole;
}
[php stuff]
location @swoole {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow_Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Ra$
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# IF https
proxy_set_header HTTPS "on";
proxy_pass http://127.0.0.1:1215$query_string;
}
}
我尝试将标头转移到一堆不同的地方,但我没有运气改变响应,用 curl ping 服务器
curl -H "Access-Control-Request-Method: GET" -H "Origin: http://front.example.com" --head http://back.example.com/
没有提供任何有用的信息,并且似乎没有包含 CORS 标头。我尝试了 CORS 标头命令的不同变体和配置,但没有响应。
有人知道可能出了什么问题吗?