CORS NGINX配置问题

CORS NGINX配置问题

我有一个带有 NGINX 的 Ubuntu Web 服务器。我想添加对以下 WebDAV 方法的 CORS 支持:PUT, GET, OPTIONS, MKCOL, PROPFIND。我阅读了几篇如何添加 CORS 支持的文章,并将以下几行放入/etc/nginx/sites-enabled/default

server {
        listen 80 default_server;
        listen [::]:80 default_server;
#       return         301 https://$server_name$request_uri;


        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        location /test {
                add_header "Access-Control-Allow-Origin"  *;
                add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS, PUT, DELETE, MKCOL, PROPFIND';
                add_header  "Access-Control-Allow-Credentials" "true";
                add_header  "Access-Control-Allow-Headers" "Authorization, origin, accept";
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
                dav_methods  PUT DELETE MKCOL;
                dav_ext_methods   PROPFIND OPTIONS;
                autoindex     on;
                create_full_put_path on;
                limit_except  GET HEAD {
                allow XXX.XXX.XXX.XXX/32;
                deny  all;
                }
       }
}

但即使重新启动 nginx 服务器后,客户端也会抱怨 CORS 未启用。我收到错误消息"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'XXXX.com' is therefore not allowed access. The response had HTTP status code 404."。有趣的是,当文件存在时,只有当文件不存在时,我才不会收到此类错误,但另一方面,当文件不存在时,我在另一台服务器上和此服务器上测试相同的错误仅有的HTTP status code 404

这是我的 nginx 版本和安装的模块:

$ nginx -V
nginx version: nginx/1.10.3 (Ubuntu)
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --add-module=/build/nginx-Q158zN/nginx-1.10.3/debian/modules/nginx-auth-pam --add-module=/build/nginx-Q158zN/nginx-1.10.3/debian/modules/nginx-dav-ext-module --add-module=/build/nginx-Q158zN/nginx-1.10.3/debian/modules/nginx-echo --add-module=/build/nginx-Q158zN/nginx-1.10.3/debian/modules/nginx-upstream-fair --add-module=/build/nginx-Q158zN/nginx-1.10.3/debian/modules/ngx_http_substitutions_filter_module 

[编辑]

这是curl的输出

$ curl --head http://ip_address/test:
HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Tue, 10 Oct 2017 10:02:23 GMT
Content-Type: text/html
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, OPTIONS, PUT, DELETE, MKCOL, PROPFIND
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization, origin, accept
Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range

所以看起来一切都很好,但我仍然在开发者控制台中遇到此错误。

相关内容