NGinX Configuration with basic auth & custom error messages
If for some reason you need the following things out of an NGinX server:
- Multiple virtual blocks
- Basic authentication (with IP whitelist of approved IPs
- Customized error messages
there are some things you should probably be aware of, as the virtual block configuration is not obvious.
First off, custom error messages are straightforward if and only if you do not have basic auth enabled.
If you do, you need to ensure two things. Your error_page
flag needs to reside above the location block of that specific virtual host.
One thing I wasn’t aware of with NGinX virtual blocks is that the root
should be (at least in this configuration) inside the location
block. If you put the basic authentication at the server
level, it will apply to all locations inside that configuration.
Here’s a consolidated example of how all the pieces go together.
The key in the above configuration is this one line proxy_intercept_errors on
. This combined with containing the root
inside the location block, will allow NGinX to return your custom error page.
Anyway, I recently burned some time with this, thought I’d share my findings, it was not easy to find a working combination of flags.