0

See the below nginx configuration file.
Virtual name based hosting (example.com), passing requests for example.com and www.example com to the mono fastcgi-process on port 9000 (127.0.0.1:9000)

I've added the file google-site-verification, but the problem is, the asp.net fastcgi-application is a MVC application, so it doesn't find the controller called "google79af7e588a34905e0.html".

I'm trying to overwrite the location in the virtual host config file, so it doesn't forward the request for google79af7e588a34905e0.html to the fastcgi-application, but it doesn't work.

I've restarted nginx, but it doesn't make a difference...
What am I doing wrong ? I've tried removing the equal-sign in "location = /" but that doesn't work, and I've tried moving the location overwrite before the main location, but that doesn't make a difference either. I've also changed the hostname forth-and-back, just to see whether I'm in the right configuration file, and i am, when I change the domain name, i start getting 404.

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##


 server {
         listen   80;
         server_name www.example.com example.com;
         access_log   /var/log/nginx/your.domain1.xyz.access.log;




         location / {
                 root /home/example/www/homepage;
                 #index index.html index.htm default.aspx Default.aspx;
                 #fastcgi_index Default.aspx;
                 fastcgi_pass 127.0.0.1:9000;
                 include /etc/nginx/fastcgi_params;
         }



    #http://serverfault.com/questions/477103/how-do-i-verify-site-ownership-on-google-webmaster-tools-through-nginx-conf
    location = /google79af7e588a34905e0.html {
            rewrite ^/(.*)  $1;
            return 200 "google-site-verification: $uri";
    }




    location /doc {
        root /usr/share;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location /shared_images {
        root /usr/share;
        autoindex off;
    }

    error_page 404 /CustomErrors/404.htm;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #   root /usr/share/nginx/www;
    #}

}
Quandary
  • 974
  • 4
  • 18
  • 34

2 Answers2

2

A location above your root definition could do the trick

location /google79af7e588a34905e0.html {
    alias /your/local/path/to/google79af7e588a34905e0.html;
}

without any rewrite.

deagh
  • 1,969
  • 5
  • 18
  • 16
  • Already got that idea on my own - doesn't work, which I find strange... – Quandary Sep 23 '14 at 02:31
  • 1
    @quandary that should work 100%. Even though you have not set a root on the server level, an alias shall work. Untested, but from the looks of it, the accepted answer had the side effect that it routes all 404 to the fast CGI application, even in /doc. –  Sep 23 '14 at 17:18
  • The original sample in the question should already work 100%! I would guess that there's something else within the config that's not being presented here that is messing up the configuration; or perhaps the config has no effect for the host/port in question? – cnst Nov 08 '15 at 08:56
1

I would go for this kind of setup:

server {
    listen 80;
    server_name www.example.com example.com;
    access_log   /var/log/nginx/your.domain1.xyz.access.log;

    root /home/example/www/homepage;

    try_files $uri $uri/ @mvc;

    location @mvc {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
    }

    location /doc {
        alias /usr/share/doc;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location /shared_images {
        alias /usr/share/shared_images;
        autoindex off;
    }

    error_page 404 /CustomErrors/404.htm;
}

Here we use nginx try_files directive to check if the files exist physically in the filesystem. If files do not exist, then the request is passed to the MVC backend.

I have also replaced root directives inside locations with alias directives, because that makes the configuration more intuitive, at least I see it like that.

This setup also makes nginx server all static assets, instead of making the MVC backend process those requests too. You might need to adjust your main server block root directive.

This setup needs the actual file on the filesystem though.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58