代码之家  ›  专栏  ›  技术社区  ›  Mugluck

nginx swoole代理上cors的配置是什么?

  •  0
  • Mugluck  · 技术社区  · 6 年前

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

    请求的上不存在“access control allow origin”头 资源。

    我正在ubuntu 16.04上运行最新的稳定nginx,配置了一个swoole服务器来处理php 7.2中的laravel php请求。据我所知一切都在起作用。

    laravel api在一个子域上,前端是另一个子域上的一个角。到目前为止一切正常,所以我一直在尝试在后端站点上配置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头命令的不同变体和配置,但没有响应。

    有人知道会出什么事吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Mugluck    6 年前

    好的问题解决了。经过更多的实验,我发现访问控制头的正确位置是在代理重定向到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;
        }
    }
    

    希望这对将来的人有帮助。