How do I force nginx to serve a custom 404 page while responding with a 404 header, without changing the address of the browser so the user can easily retype?
set $allowed 0; #(updated after comments)
error_page 404 /404page.html; #(updated after first answer, forgot to mention)
location = /authreq.html {
if ($allowed = 0){
# return 307 $scheme://$host/404page.html ; #works but should be 404
# return 404 "shows this message"; #does not redirect when inserting url instead of message
# rewrite ^ /404page.html break; #serves 404page.html and doesn't change browseraddress which is good, but sends a response header 200
# return 404 # invokes error_page directive with header 200
}
}
The whole configuration:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name www.fellowshipmedia.eu;
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html index.htm;
error_page 404 = /404page.html;
set $allowed 0;
location = /authreq.html {
if ($allowed = 0){
# return 307 $scheme://$host/404page.html ; #works but should be 404
# return 404 "shows this message"; #does not redirect when inserting url instead of message
return 404; #
# rewrite ^ /404page.html break; #serves 404page.html and doesn't change browseraddress which is good, but sends a response header 200
}
}
}
Wouldn't the error_page directive do this for you you?
For example something like this:
Simply doing the following will not return a 404 header but a 200:
To invoke a 404 header and serve a custom 404 page without changing the browsers' addressbar address you should add '404' in the error_page directive like so: