NGINX custom 404 page without redirect - nginx

By default, when NGINX returns a 404 it renders some default HTML. For example, something like:
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.0 (Ubuntu)</center>
</body>
</html>
I'd like to customize this HTML but leave the URL unchanged. I've tried setting the error page for 404s:
error_page 404 /404.html
But this first redirects the browser to /404.html.
Is there a way to customize the 404 HTML without redirecting?

may be you can do it as follow by using the error_page and proxy_intercept_errors directive of nginx
detail config
server {
## other config
## here the proxy_intercept_errors directive is the key
proxy_intercept_errors on;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
## your 50x.html file parent directory
root /var/www;
}
error_page 404 /404.html;
location = /404.html {
## your 404.html file parent directory
root /var/www;
}
}
document on nginx official site:
proxy_intercept_errors and error_page

This configuration worked for me
error_page 404 /404.html;
location = /404.html {
internal;
}
http://nginx.org/en/docs/http/ngx_http_core_module.html#internal

Related

nginx two different error_pages for same error code

I want to configure a maintenance page for Nginx.
I wanted to differentiate the 503 maintenance page with other 503 pages.
server {
...
location / {
if (-f /www/maintenance_on.html) {
return 503;
}
...
}
# Error pages.
error_page 503 /maintenance_on.html;
location = /maintenance_on.html {
root /www/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
...
}
I want to serve maintenance_on.html only when it is present. For all the 503s where maintenance_on.html is not there, I want to serve 50x.html.
Use a separate error_page directive for handling 503. You can point it at a named location block with a try_files directive.
For example:
error_page 503 #error503;
error_page 500 502 504 /50x.html;
location = /50x.html {
root html;
}
location #error503 {
root html;
try_files /maintenance_on.html /50x.html =404;
}
The #error503 block will first check for the existence of the file maintenance_on.html and if it does not exist, the file 50x.html instead. The =404 term is not reached.

How to provide custom nginx error_pages

Need to provide custom error pages on nginx. Currently config looks like below:
error_page 404 /404.html;
error_page 500 /500.html;
error_page 502 /502.html;
location ~ ^/(404.html|500.html|502.html){
root /etc/nginx/error-pages;
}
It works for urls like https://example.com/404, but it doesn't for https://example.com/404/404
How to make it work ? Thanks in advance.
We will create a location block for the file, where we are able to ensure that the root matches our file system location and that the file is only accessible through internal Nginx redirects:
error_page 404 /custom_404.html;
location = /custom_404.html {
root /etc/nginx/error-pages;
internal;
}
error_page 500 502 503 504 /custom_50x.html;
location = /custom_50x.html {
root /etc/nginx/error-pages;
internal;
}

switch off html wrapping of errors in nginx

I use nginx on Ubuntu to forward requests to a Spring Boot-driven API server. Sometimes the API returns a body with 4xx and 5xx errors, that the client application consumes. E.g. in the API code I might return:
{
"message": "blah"
}
However nginx seems to wrap errors up into some HTML and embeds the response within them. Is there any way to disable this behavior? And is there a way to do it en masse (i.e. for all 4xx and 5xx errors, without explicitly writing them all out with error_page for example)
Similar question and accepted answer here, however that seems to be a bit of hack. I'm sure there's a better way of doing this...
EDIT: Config looks like this:
server {
listen 80;
server_name my.domain.com;
location / {
proxy_pass http://127.0.0.1:9001;
}
}
You just need to add a error_page directive inside the location
server {
listen 80;
server_name my.domain.com;
location / {
error_page 404 = 404;
proxy_pass http://127.0.0.1:9001;
}
}
What this would do is cancel error_page from previous directives set at the http block. And pass the result back to the client as it is. You don't want the codes for which JSON should be sent back to the client in this error_page directive.
You can try update your nginx conf and add:
error_page 403 /error403.html;
location = /error403.html {
root html;
}
or for group of erros:
error_page 500 502 504 /50x.html;
location = /50x.html {
root html;
}
And here you can replace html files to json so nginx will return json respnse, like:
error_page 404 /404.json;
location = /404.json {
root html;
}
and don't forget put 404.json (and all appropriate files) into root which is specified in config.
In your config it will look like:
server {
listen 80;
server_name my.domain.com;
location / {
proxy_pass http://127.0.0.1:9001;
}
error_page 404 /404.json;
location = /404.json {
root html;
}
error_page 500 502 504 /50x.json;
location = /50x.json {
root html;
}
}

NgINX error_page

I have some virtual host configurated on my server.
I Need to set a common error_page for all them.
host1.conf
server {
listen 80;
server_name host1.com
root host1/path;
location / {
proxy_pass http://localhost:xxxx;
}
}
host2.conf
server {
listen 80;
server_name host2.com
root host2/path;
location / {
proxy_pass http://localhost:xxxx;
}
}
and so on.
1) Could I set the error_page in every .conf file?
Could I set the error_page in every .conf file? Or I'm able to set it only in a default .conf?
2) Problem with error redirect
I have a 404.html page and 50x.html page too on my default server.
So I added the follow instructions to all my host .conf files
error_page 404 /404.html;
location = /404.html {
root /path/to/default;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /path/to/default;
}
I see rightly that page but... they have some css, webfonts and images inclusion. That inclusions are not resolved, it seems they are looked on host root instead of default root.
There is a solution for have a single error page setted on a default site configuration and reused in all others conf?

nginx to serve specific case invariant subfolder

I want to have a subdomain my.domain.com. I want my.domain.com to serve an error page, and my.domain.com/one to serve app one and my.domain.com/two to serve app two. The location of one is /var/www/domain.com/One and two is likewise /var/www/domain.com/Two. I have tried (and (and (and tried)) yet nothing works. Here's what I've got.
server {
server_name my.domain.com;
root /var/www/domain.com;
index index.html;
location /one/ {
root /var/www/domain.com/One;
}
location /two/ {
root /var/www/domain.com/Two;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Resources