Score:0

Custom error page not shown when custom error gets returned

cn flag

I'm trying to serve a custom error page for a 403 error that gets triggered when a non HTTPS request is made. The 403 error gets returned fine, but the default nginx error page get shown instead of the custom error page.

Changing the order (defining the error_page above the if statement) doesn't seems to have any impact.

server {
    listen 80;
    server_name example.com;
    root "/var/www/example.com";

    index "index.html";

    [..]

    if ($scheme != "https") {
        return 403;
    }

    error_page 403 /error_pages/403_forbidden.html;
}

Am I missing something? Why doesn't the custom error page get shown in this case?

sv flag
Welcome to ServerFault. Please post the full config. Btw, I don't see `root` directive. Did you define it?
Kaspar avatar
cn flag
@PothiKalimuthu, thank you. I initially left the `root` directive out of the example thinking it wasn't relevant. I've added it to the example.
Richard Smith avatar
jp flag
The error page is `http://example.com/error_pages/403_forbidden.html` which is also being prevented by the `if ... return` statement.
Kaspar avatar
cn flag
@RichardSmith, thank you, thats interesting. Would that mean that there is no way to show/use the custom error page if the `if ... return` statement is valid?
Richard Smith avatar
jp flag
You would need to restructure your configuration to allow the error pages to be delivered. At the moment, access to those pages is denied. For example, use two `server` blocks instead of `if ($scheme != "https")`.
Score:0
cn flag

Thanks to comments on my question I've found that using a location block was needed to solve my issue. That way the error page still works as intended.

location / {
    if ($scheme != "https") {
        return 403;
    }
}
I sit in a Tesla and translated this thread with Ai:

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.