Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I set up my custom 404 page for most cases in Nginx using global error_page settings in server block, however, if I do the following inside a location block:

if ($uri ~* ...) {return 404;}

then the default Nginx 404 page is returned.

Is there a way to return the custom page in this case?

Updated: currently I have something like this

server {
    ...
    error_page    404    /html/error/404.html;
    ...
    root           /home/www-data/mysite/static;

    location ^~ /blah/blah/ {
        if ($uri ~* ...) { return 404; }
        ...
    }

    location ^~ /html/error/ {
        internal;
    }
}
share|improve this question
    
I think it has nothing to do with your location match or doc root. I went through the same scenario. Nginx does not use the custom 404 page when you do "return 404" for a particular location or request. Though I could not fix the problem. –  user228338 Jun 27 at 22:03

2 Answers 2

"/html/error" shouldn't be in both the root (absolute) and error_path (relative) paths.

Where is the document root? Is it really '/home/www-data/mysite/static' when there's an 'html/' subdirectory of that? Are you sure root shouldn't be '/home/www-data/mysite/static/html' ?

What's the pattern on the right side of the ~* match? You might have a regex error that's causing it to match everything.

Or, the root directory might be wrong, and nginx might be returning 404 because it can't find the file, and not because that "if" pattern matches.

By default, the nginx error log will log the actual filesystem path nginx attempted to access for a request that returned 404 because of a missing file. See the nginx docs for log_not_found and error_log if those errors are not showing up in the error log.

share|improve this answer
    
thanks for pointing that out. it was a typo in my original question, I have updated the question. However, with this setup, my problem still persists. –  MLister Apr 16 '13 at 15:03

Instead of returning 404 directly rather use:

error_page 404 /404.html;
location / {
            try_files $uri =404;
}

This would produce 404 http status code with custom 404 error page. Hope this helps.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.