CORS NGINX 配置问题

CORS NGINX 配置问题

我有一个装有 NGINX 的 Ubuntu Web 服务器。我想为以下 WebDAV 方法添加 CORS 支持:,,,,,PUT。我读了几篇关于如何添加 CORS 支持的文章,并在其中添加了以下几GET行:OPTIONSMKCOLPROPFIND/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 

相关内容