Disable direct access to nginx custom error page - nginx

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

Related

NGINX - redirect to a custom maintenance page without changing url

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!

NGINX Ingress 404 redirect to URI

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/'" />

return custom error pages in nginx, flask

I tried other answers in SO, like :
Return custom 403 error page with nginx
nginx + uwsgi + flask - disabling custom error pages
nginx not serving my error_page
and
references:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors
http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page
but still not able to return custom error pages - nginx keeps to return its own pages.
Working in local env, flask returns custom html pages.
When published in nginx, no more.
This is what I want to have:
if user hit /js/, route will return 403, while allowing to internal files /js/myJS.js
If file /js/myJS.js does not exist, return 404
If something goes awry, say an API in flask that break (it shouldn't :) ) > return 500
My flask configuration is like:
#application.errorhandler(404)
def error_404(e):
application.logger.error('Page Not Found: %s', (request.path))
#return render_template('404.html'), 404
return render_template("404.html", error = str(e)), 404
// other errors are handled similarly
and the 404.html template, is within var/www/mysite/templates folder.
The error page are static files, there are no assets served from flask but the html template.
Now, in my nginx configuration I am handling errors like this:
root var/www/mysite;
# update: tried to handle custom errors with proxy_intercept_errors on;
# still no success
proxy_intercept_errors on;
# static is the directory that serve the subdirectory /js
# HOW COULD I RETURN 403 if /js is hit directly, but not /js/myfile.js ?
# I tried to add deny all and allow only my server up, but did not worked out
location /static {
expires 1M;
alias /var/www/mysite/static;
access_log off;
add_header Cache-Control "public";
allow #myserverip;
deny all;
}
error_page 403 /403.html;
location = /403.html {
root /var/www/mysite/templates/;
internal;
}
error_page 404 /404.html;
location = /404.html {
root /var/www/mysite/templates/;
# internal;
allow all;
}
# also with the following no success: nginx white internal page is still displayed
error_page 404 http://example.com/404.html;
That I try to set internal or comment it out; that I try to set allow all (as in answer in above SO answer) , no matter what, nginx will return its custom page 404 - not from my templates folder.
I also tried to set up a folder for fallback, like:
## Fallback Directory
location #error {
root /var/www/error;
}
but still no success - always the white 404 page from nginx.
What is wrong in my configuration ?
I would like to better understand how flask and nginx dialogue for errors.
I understood that once published, nginx will handle the error - but did not understood how the raised error are handled between flask and nginx: what happen when user hit route of a "wrong" resource ? is it nginx that intercept it? is it flask? is it uwsgi ? how do they pass the ball?
Also, please share a tip:
how could I query error pages in nginx, e.g. for testing bad requests 403 and 500 ?

How do I return generic error pages based on different urls using nginx

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?

Multiple 404 error pages in nginx

I am running nginx server. I want to serve a custom error page for a particular request only. For-example for request
http://localhost/abc1 & http://localhost/abc2
if these pages are not there I want to serve a custom error page. This custom error page should appear only for above two mentioned links, rest of the page errors can show the default error page. I have tried different configuration but nothing seems to work. Thoughts
Regards,
Farrukh Arshad.
Your answer is quite correct, but as you said, you only defined them for the html's, remove the extension and it should work for the whole directory, no need to repeat the root, just define it in the server block scope
server {
listen 80;
root /var/www/nginx-default;
location /abc1 {
error_page 404 /special_error.html;
}
location /abc2 {
error_page 404 /special_error2.html;
}
}
Ok, I found the answer. The trick is you have to define error_page explicitly for all those special locations. Here is the configuration which worked for me.
location / {
root /var/www/nginx-default;
index index.html index.htm;
error_page 404 /404.html;
}
location /abc1.html {
root /var/www/nginx-default;
error_page 404 /special_error.html;
}
location /abc2.html {
root /var/www/nginx-default;
error_page 404 /special_error2.html;
}
I am not good with nginx, but I have noticed it depends on the search pattern you give in "location" tag. I have tried different things and those failed . Forexample the above rules will ONLY work for
http://localhost/abc1.html
and fail for
http://localhost/abc1
so your "location" search pattern should be good if you want to cover second case. Probably some nginx guru can shed some more light on this. Thanks.

Resources