I want to redirect all my 404s to another URI. The problem is that my other URI has a response code of 404. So when I use error_page 404 URI, the redirection occurs in a loop. Example server snippet is shown below
if ($request_uri != URI) {
proxy_intercept_errors on;
error_page 404 URI;
}
Direct 404 Errors to the Custom 404 Page
Use the error_page directive so that when a 404 error occurs (when a requested file is not found), the custom page you created is served. 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 (not requestable directly by clients):
error_page 404 /custom_404.html;
location = /custom_404.html {
root /usr/share/nginx/html;
internal;
}
and at custom_404.html file
use any redirect like
<meta http-equiv="refresh" content="0;URL='http://thetudors.example.com/'" />
Related
I want to rewrite 403 response that already has document from app. And use my document or just plain text response instead of detailed application data.
I tried doing :
error_page 403 /40x.html; location = /40x.html { }
but since document is already served by app, error_page doesn't work.
I was lacking the:
proxy_intercept_errors on;
option in server config.
After adding it and modifying default_headers it started working as desired
I have this nginx block that redirects all 401,403 and 404 to a single custom error page
error_page 401 #errorPage;
error_page 403 #errorPage;
error_page 404 #errorPage;
location #errorPage {
rewrite ^ /error.php?error=$status last;
}
Is it possible to deny the access to the error.php if called directly? Thanks
Consider storing error pages outside of the main web folder and set a new root or alias:
location #errorPage {
root /var/www/error-pages;
...
}
I need to set these two Urls redirect to a custom 404 page:
https://example.com/frontend/#/,
https://example.com/frontend/
the Nginx config below works fine:
server{
listen 80;
server_name example.com;
error_page 404 /404.html;
location = /404.html {
root /opt/app/nginx/html;
internal;
}
location = /frontend/ {
return 404;
}
}
However this setting makes URLs with the same prefixes go to 404 as well, which is a pain in the ass!
https://example.com/frontend/#/abc,
https://example.com/frontend/#/123
all redirect to 404.
I use exact matching (= modifier) for the 404 settings,
why does it still affect others and how can I fix it?
Thank you for any help! :D
Edited
I found the reason thanks to the answers given down there! So the # sign is processed by the browser only, the browser never passes # to the server, that's why Nginx treats https://example.com/frontend/#/abc,
https://example.com/frontend/#/123 all as https://example.com/frontend/.
In this case, if I wanna set https://example.com/frontend/#/ to the 404 page, what I need to do is to create an index page under /frontend folder in the project, and put 404 content inside the index page, There's nothing Nginx could do with redirecting URL contains hash sign.
You can't do that because the hash part isn't sent to the server. It's for client side processing only.
Try using two location without the "/" in each.
location = /frontend {
return 404; }
location = /frontend/# {
return 404; }
I have a NGINX server running, now I have the following directive and location block for a custom 503 error page:
error_page 503 #maintenance;
location #maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
When a user is redirected to the custom 503 error page, the url in his browser changes to the following:
https://www.example.com/maintenance
I would like the user to be redirected to the maintenance but keep the root domain in the address bar:
https://www.example.com/
How can I achieve that?
Thanks in adavnce!
I'm trying to use nginx error_page directive (link) to show error pages.
This is my code so far:
error_page 404 /site/404.html;
location /site/404.html {
try_files $uri =404;
break;
}
But what I really want is show this 404.html page only if the url is www.example.com/example. And not show this 404.html error page if the url is only www.example.com. Is it possible to achieve this behavior using error_page directive?