PHP / Symfony API Nelmio的CORS问题

问题描述 投票:0回答:1

从我的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和其他网站以查找配置问题,但找不到任何有用的信息。我甚至尝试在我的nginx conf中添加一些开放的CORS配置,结果没有区别。

我的配置如下:

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(用于env):

    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中的dev或prod日志中也没有出现任何内容。

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

php symfony nginx cors nelmiocorsbundle
1个回答
0
投票

感谢您的帮助,我在API nginx配置文件中找到了答案。由于某种原因,将标题添加到OPTIONS调用的行被注释掉了。取消注释它会使OPTIONS调用成功并处理CORS问题。

© www.soinside.com 2019 - 2024. All rights reserved.