PHP / Symfony API Nelmio的CORS问题

时间:2019-04-07 17:46:50

标签: php symfony nginx cors nelmiocorsbundle

从我的React应用程序(在端口3100上运行)到我的API(在同一台机器上,但是我假设使用不同的端口,无论Symfony,Nelmio,PHP的默认端口是什么)的所有调用都会导致以下结果:

Failed to load https://beta-api.fitchek.com/v1/oauth/login: 
Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the 
requested resource. Origin 'https://beta-wellness.fitchek.com' is 
therefore not allowed access. If an opaque response serves your 
needs, set the request's mode to 'no-cors' to fetch the resource 
with CORS disabled.

我已经检查了Stack Overflow和其他站点的配置问题,但是找不到任何有用的信息。我什至尝试将一些开放式CORS配置添加到我的nginx conf中,结果没有差异。

我的配置如下:

config.yml(对于Symfony和Nelmio):

nelmio_cors:
    defaults:
        allow_credentials: true
        allow_origin: []
        allow_headers: []
        allow_methods: []
        expose_headers: []
        max_age: 0
        hosts: []
        origin_regex: false
    paths:
        '^/':
            allow_credentials: true
            allow_origin: ['*']
            allow_headers: ['content-type', 'authorization']
            allow_methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']
            max_age: 3600

parameters.yml(用于环境):

    allow_origin_list:
        - 'https://beta-app.fitchek.com'
        - 'https://beta-marketplace.fitchek.com'
        - 'https://beta-canfitpro.fitchek.com'
        - 'https://beta-sweateq.fitchek.com'
        - 'https://beta-payments.fitchek.com'
        - 'https://beta-wellness.fitchek.com'

我的nginx配置

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##
        ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers   on;
        ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DHE+AES128:!ADH:!AECDH:!MD5;
        ssl_session_cache shared:SSL:20m;
        ssl_session_timeout 180m;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Max file upload
        ##

        client_max_body_size 10m;
        client_body_timeout 60s;
        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

已编辑

我在Nginx日志或php日志中看不到任何错误。我不知道我还会在哪里寻找相关的错误,PHP API的开发或生产日志中也没有任何内容。

(当我使用Postman从本地主机向beta服务器进行调用时,不会出现CORS错误,并且调用可以正常工作!但是,当从beta服务器向beta服务器进行调用时,CORS错误为上面提到的出现)。

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,我在我的API nginx配置文件中找到了答案。由于某些原因,注释了将标头添加到OPTIONS调用的行。取消注释会使OPTIONS调用成功,并且可以解决CORS问题。