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 return403
, 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 ?