nginx two different error_pages for same error code - nginx

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.

Related

Nginx redirect to 404 page if there is a trailing slash at the end of the URL

I got a ask of doing a redirect to 404 page if there is a trailing slash at the end of the URL.
Eg:
https://www.example.com/test/
If the user enters a trailing slash at the end means we need to redirect it to 404 page. Previously I have redirected the same URL by removing the trailing slash, Now the requirement has changed to redirect to the 404 page.
Here is the nginx conf file for it. Please let me know what change I have to make here to achieve the scenario.
nginx.conf
server {
listen 80;
rewrite ^/(.*)/$ /$1 permanent;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri.html $uri/index.html $uri/index =404;
error_page 404 /404.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Updated Conf:
Default Nginx 404 error page is displayed after this config. What's the update required here to display the custom 404 page?.
server {
listen 80;
location ~ ^/(.*)/$ {
return 404;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri.html $uri/index.html $uri/index =404;
error_page 404 404 /404.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Remove the rewrite ^/(.*)/$ /$1 permanent; statement and replace it with:
location ~ ^/(.*)/$ { return 404; }

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;
}

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;
}
}

Nginx change document root directory

I'm using the Windows setup file from Nginx For Windows
But It doesn't allow to change the install location, so it defaults to C:\nginx Is there a way to update the config file to change the root directory to D:\blabla?
Sample code from nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Use the -p parameter when you start nginx:
nginx -p "D:\blabla"
root d:/path/whereever;
will work without issues, but do use a proper windows version like the Butterfly or Caterpillar release and not those other crippled versions.
even a root '//192.168.2.5/mount/webdrive';
will work!
I don't know howit was two years ago but with an current Version (1.13.9) you only need to set the root of your Server
server {
listen 80;
server_name localhost;
location / {
root D:\blabla;
index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root D:\blabla;
}
}

Resources