1

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"
Adam Birds
  • 187
  • 2
  • 3
  • 13

1 Answers1

1

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.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
  • That would be okay but I want it to work for if the request was either: /autodiscover/autodiscover.xml /Autodiscover/autodiscover.xml /autodiscover/Autodiscover.xml /Autodiscover/Autodiscover.xml This is due to email clients asking for different things. – Adam Birds Jan 25 '17 at 22:20
  • I changed the `location` statement to accept either lower or upper case in the first letter of first path component. – Tero Kilkanen Jan 25 '17 at 22:23
  • Thats working now except it is downloading the .php file instead of executing it, any ideas? – Adam Birds Jan 25 '17 at 22:49
  • Thats fine, sorted that bit. – Adam Birds Jan 25 '17 at 23:07
  • 1
    You have to escape the extension dot otherwise it will match any character. Also, why not simply use `location ~*` which does a case insensitive match? –  Jun 27 '17 at 12:55
  • @AdamBirds how did you fix the php file download? – Maxxer Jul 05 '18 at 12:10