Nginx CORS对于Lumen API无法正常工作

问题描述

嗨,我曾尝试在default.conf文件以及lumen API中启用cors,但由于某些原因,当我尝试在生产chrome中运行angular时,出现CORS错误,这是我的Nginx配置,有人可以告诉我此配置是否是错误的,或者我将CORS add_header放置在错误的位置

server {
        listen 80;
        listen 443;
        server_name example.com;
        client_max_body_size 50M;
        index index.PHP;

        access_log /dev/stdout combined buffer=256k flush=10s;
        error_log /dev/stderr;

        keepalive_timeout 65;
        root /var/www/html/api/public/;

        # BLOCKS ACCESS TO . FILES (.env,.htaccess,...)
        location ~ /\. {
            deny  all;
        }

        # Commenting out for Now until we enforce https/ssl everywhere
        #if ($http_x_forwarded_proto = http) {
        #    return 301 https://$host$request_uri;
        #}

        location / {
            # Rewrite rules and other criterias can go here
            # Remember to avoid using if() where possible (http://wiki.Nginx.org/IfIsEvil)
            index index.PHP index.html index.htm;
            try_files $uri $uri/ /index.PHP$is_args$args;
                 if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,authorization';
            #
            # Custom headers and headers varIoUs browsers *should* be OK with but aren't
            #
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
         }
         if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET,Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
         }
         if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET,Content-Range';
         }
        }

        # This block will catch static file requests,such as images,css,js
        # The ?: prefix is a 'non-capturing' mark,meaning we do not require
        # the pattern to be captured into $1 which should help improve performance
        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
          # Some basic cache-control for static files to be sent to the browser                                                                                                                                                                        expires max;
        add_header Pragma public;
        add_header Cache-Control "public,must-revalidate,proxy-revalidate";
        add_header x-frame-options "SAMEORIGIN" always;
        add_header X-Content-Type-Options nosniff always;
        add_header X-XSS-Protection "1;mode=block" always;
    }

    location ~ [^/]\.PHP(/|$) {

        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }
        fastcgi_param HTTP_PROXY "";
        fastcgi_pass unix:/run/PHP/PHP7.2-fpm.sock;
        fastcgi_index index.PHP;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    }

}

我不确定100%是否是我在此处发布此问题的地方,所以如果不能,可以有人通知

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)