【问题标题】:Nginx configuration for enabling CORS用于启用 CORS 的 Nginx 配置
【发布时间】:2015-06-15 05:19:09
【问题描述】:

我正在关注参考试图让我的 nginx 接受 CORS。 http://enable-cors.org/server_nginx.html

但无论我如何配置我的 /etc/nginx/site-enable/default 如下。 它只是行不通。我的配置有什么问题吗?

谢谢。

> >

服务器{ 听 80 default_server; 听 [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;
    #add_header Access-Control-Allow-Origin *; # < this is the needed header

    location / { 
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            #try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules

                 if ($request_method = 'OPTIONS') {
                    add_header 'Access-Control-Allow-Origin' '*';
                    #   
                    # Om nom nom cookies
                    #   
                    add_header 'Access-Control-Allow-Credentials' 'true';
                    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                    #   
                    # Custom headers and headers various browsers *should* be OK with but aren't
                    #   
                    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                    #   
                    # 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-Credentials' 'true';
                    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                 }   
                 if ($request_method = 'GET') {
                    add_header 'Access-Control-Allow-Origin' '*';
                    add_header 'Access-Control-Allow-Credentials' 'true';
                    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                 }   
    }    }

【问题讨论】:

  • 试试这个:stackoverflow.com/questions/50771746/…>

标签: nginx


【解决方案1】:

仅当您在请求中发送的所有标头都在 allowed-headers 字段中传播时,才会触发 Nginx CORS 过滤器。一旦您只发送了一个本节未提及的标头,CORS-Filter 将什么也不做。你检查你的请求头了吗?

此外,您的配置不适用于 PUT 请求。您发送哪种类型的请求?您检查了响应标头吗?

尝试设置ipv6only=off;

【讨论】:

  • 我正在尝试重现这些步骤:$ curl -I "petstore.swagger.io/v2/swagger.json" 在下面的文档中:github.com/swagger-api/swagger-ui/blob/master/… 但我只有 curl -I "ebc-vm:8080/swagger.json" HTTP/1.1 200 OK Server : nginx/1.4.6 (Ubuntu) 日期: 2015 年 6 月 15 日星期一 05:30:16 GMT 内容类型: application/json 内容长度: 8875 上次修改时间: 2015 年 6 月 15 日星期一 03:36:29 GMT 连接: 保持活力
  • 由于使用了这个API,所以必须在Access-Control-Allowed-Headers中添加:Content-Type, api_key, Authorization
  • 感谢您的提示。我已经更新了我的配置,但仍然无法让 swagger-ui 工作。
  • # curl -I "petstore.swagger.io/v2/swagger.json" HTTP/1.1 200 OK Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, DELETE, PUT Access-Control- Allow-Headers: Content-Type, api_key, Authorization Content-Type: application/json Content-Length: 0 Connection: close Server: Jetty(9.2.7.v20150116)
  • # curl -I "ebc-vm/swagger.json" HTTP/1.1 200 OK 服务器:nginx/1.4.6 (Ubuntu) 日期:星期一,2015 年 6 月 15 日 05:45:54 GMT 内容类型:应用程序/json 连接:保持活动访问控制允许来源:* 访问控制允许方法:POST、GET、OPTIONS、DELETE、PUT 访问控制允许标题:来源、授权、接受、内容类型, api_key Access-Control-Allow-Credentials: true Accept-Ranges: bytes
猜你喜欢
  • 2019-06-16
  • 1970-01-01
  • 2019-11-19
  • 2019-06-27
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
相关资源
最近更新 更多