I have an Ubuntu web server with NGINX. I want to add CORS support for the following WebDAV methods: PUT, GET, OPTIONS, MKCOL, PROPFIND. I read couple of articles how to add the CORS support and I have put the following lines in /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;

But even after restart the nginx server the client is complaining that the CORS is not enabled. I got error messages that "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.". The interesting part is that I don't get such errors when the file is present, only if it is not there, but on the other hand I am testing the same on another server and on this server when the file is not present I got only HTTP status code 404.

And this is my nginx version and installed modules:

$ 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 

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.