Score:0

Does NGINX handle some error events / codes without them being set up in the config by default?

nl flag

Question: Are there some error events / codes that NGINX handles by default?

Background: The following portion of my config stops hotlinking and returns a 403 error.

    location ~* \.(jpg|png|svg|webp|ico)$ {
        valid_referers none blocked server_names ~\.bing\. ~\.duckduckgo\. ~\.facebook\. ~\.google\. ~\.instagram\. ~\.twitter\. ~\.yahoo\.;
        if ($invalid_referer) {
            return 403;
        }
    }

This section blocks unwanted HTTP methods and returns a 405 error.

if ($request_method !~ ^(GET|HEAD|POST)$) {
    return 405;
}

This got me thinking, are there any errors that I don't have to set up in NGINX?

For example, a 400 Bad Request error. Does NGINX know what a bad request is without adding an if statement and some logic to the config?

Alternately do I need to set up every error in the config that I plan to use and if I don't it can't be triggered? I always assumed this was the case but when I see how many different 400 and 500 errors there are I wonder if I'm not fully understanding this concept.

Score:2
co flag

Nginx will handle everything except application level errors. A few examples would be

400 if the request breaks RFC specified format.
403 if nginx cannot read a file due to permission.
404 if the file isn't found.

And so on so forth, the only thing nginx doesn't handle is non-RFC requirements like your anti-hotlinking and things outside its scope such as fastcgi/uwsgi/http backends.

Basically don't worry about anything not related to your specific business logic.

myNewAccount avatar
nl flag
Awesome! On the 400 error example do I still need to include a location in my config so that when NGINX sees the error it knows what to send? For example, `error_page 400 /html/400.html;`
Martin Fjordvald avatar
co flag
It has a simple default error page, see here: https://www.google.com/search?q=nginx+400+example&tbm=isch You can customise it to look nicer yes, but it's not required.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.