Nginx – How to serve Autodiscover.xml using Nginx

autodiscovernginxPHPrewritexml

I am trying to serve an Autodiscover.xml file using Nginx:

Below is my config:

upstream autodiscoverexamplecoukbackend {
        server unix:/var/run/php-fcgi-autodiscoverexamplecouk.sock;
}

server {
        listen 80;
        listen 443 ssl;

        ssl_certificate /etc/letsencrypt/live/autodiscover.example.co.uk/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/autodiscover.example.co.uk/privkey.pem;

        server_name autodiscover.example.co.uk;
        root  /var/www/vhosts/autodiscover.example.co.uk/htdocs;

        index index.html;

        error_log /var/www/vhosts/autodiscover.example.co.uk/error.log;
        access_log /var/www/vhosts/autodiscover.example.co.uk/access.log combined;

        #location ^~ /autodiscover/ {
                #index autodiscover.php;
                #rewrite ^/.*$ /autodiscover.php last;
        #}

        location ~* /autodiscover/ {
                rewrite ^/autodiscover/autodiscover\.xml$ /autodiscover/autodiscover.php last;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location ~ \.php$ {
                try_files $uri =404;
                include /etc/nginx/fastcgi_params;
                fastcgi_pass autodiscoverexamplecoukbackend;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                fastcgi_intercept_errors on;
        }
}

Problem is that it isn't able to serve the Autodiscover.xml file when requested with the uppercase A

If anybody can assist that would be great.

When Autodiscover.xml is requested it should reweite it to autodiscover.php which will serve .autodisocver.xml back. It does this so it can serve different domains.

The file is located at /autodiscover/autodiscover.php

Nginx error log:

2017/01/25 21:34:12 [error] 29385#29385: *93 open() "/var/www/vhosts/autodiscover.example.co.uk/htdocs/Autodiscover/Autodiscover.xml" failed (2: No such file or directory), client: 13.67.59.89, server: autodiscover.example.co.uk, request: "POST /Autodiscover/Autodiscover.xml HTTP/1.1", host: "autodiscover.example.co.uk"
2017/01/25 21:34:13 [error] 29385#29385: *94 open() "/var/www/vhosts/autodiscover.example.co.uk/htdocs/Autodiscover/Autodiscover.xml" failed (2: No such file or directory), client: 13.67.59.89, server: autodiscover.example.co.uk, request: "POST /Autodiscover/Autodiscover.xml HTTP/1.1", host: "autodiscover.example.co.uk"
2017/01/25 21:45:05 [error] 29385#29385: *108 stat() "/var/www/vhosts/autodiscover.example.co.uk/htdocs/Autodiscover/Autodiscover.xml" failed (2: No such file or directory), client: 13.67.59.89, server: autodiscover.example.co.uk, request: "POST /Autodiscover/Autodiscover.xml HTTP/1.1", host: "autodiscover.example.co.uk"
2017/01/25 21:45:05 [error] 29385#29385: *109 open() "/var/www/vhosts/autodiscover.example.co.uk/htdocs/Autodiscover/Autodiscover.xml" failed (2: No such file or directory), client: 13.67.59.89, server: autodiscover.example.co.uk, request: "POST /Autodiscover/Autodiscover.xml HTTP/1.1", host: "autodiscover.example.co.uk"
2017/01/25 21:56:15 [error] 29485#29485: *121 open() "/var/www/vhosts/autodiscover.example.co.uk/htdocs/Autodiscover/Autodiscover.xml" failed (2: No such file or directory), client: 13.67.59.89, server: autodiscover.example.co.uk, request: "POST /Autodiscover/Autodiscover.xml HTTP/1.1", host: "autodiscover.example.co.uk"
2017/01/25 21:56:16 [error] 29485#29485: *122 open() "/var/www/vhosts/autodiscover.example.co.uk/htdocs/Autodiscover/Autodiscover.xml" failed (2: No such file or directory), client: 13.67.59.89, server: autodiscover.example.co.uk, request: "POST /Autodiscover/Autodiscover.xml HTTP/1.1", host: "autodiscover.example.co.uk"

Best Answer

If your objective is to send all requests for https://autodiscover.example.co.uk/autodiscover/Autodiscover.xml to /var/www/vhosts/autodiscover.example.co.uk/htdocs/autodiscover/autodiscover.php, you can use this location block:

location ~ /(?:a|A)utodiscover/Autodiscover.xml {
    try_files /autodiscover/autodiscover.php =404;
}

The problem in your configuration is that you are using lower-case version in the rewrite statement, and therefore there is no match when a request with a capital first letter comes in.