【问题标题】:What's the config for CORS on a Nginx Swoole proxy?Nginx Swoole 代理上的 CORS 配置是什么?
【发布时间】:2018-12-16 18:44:08
【问题描述】:

这让我很沮丧。我一直在听那句经典台词:

请求中没有“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 标头命令的不同变体和配置,但没有响应。

有人知道可能出了什么问题吗?

【问题讨论】:

    标签: php laravel-5 proxy cors swoole


    【解决方案1】:

    好的,问题解决了。经过更多的实验,我发现访问控制标头的正确位置是在代理重定向到 swoole 之前的最终 https 重定向中。像这样:

    ## Serves https://sub.example.com
    server {
            server_name sub.example.com;
            listen 443 ssl http2 ;
            listen [::]:443 ssl http2;
    
            [ssl stuff]
    
    
            add_header 'Access-Control-Allow-Origin' 'https://front.example.com';
            add_header 'Access-Control-Allow_Credentials' 'true';
            add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,Content-Type,Content-Range,Range';
            add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
    
            root /var/www/sub.example.com/;
            ....
    
           location @swoole {
            ....
    
            proxy_pass http://127.0.0.1:1215$query_string;
        }
    }
    

    希望这对未来的人们有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-08-06
      • 1970-01-01
      • 2019-11-19
      • 2017-11-21
      • 2021-10-28
      • 2020-04-26
      • 2021-03-29
      • 2011-08-28
      • 2021-03-24
      相关资源
      最近更新 更多