I'm using nginx as proxy server for my project. In case my app is offline for maintenance I'd like to show a fallback page. This works fine so far. The only problem is, that the server response with a 502 error code - which makes sense. How do I change it to a 503 route in my fallback though?
server {
listen 80;
error_page 500 502 503 504 #fallback;
location / {
proxy_pass http://0.0.0.0:3000;
}
location #fallback {
// I need this to answer with a status of 503 instead of a 502
root /srv/my-project/static;
try_files /fallback.html;
}
}
you can set a error page nginx error page
and set somethig like
error_page 502 =503 /maintenance.html
or something like
location / {
error_page 502 =503 /maintenance.html;
include proxy_params;
proxy_pass http://unix:/var/run/my.sock;
}
location /maintenance.html {
return 503;
}
source : How can I make Nginx return HTTP 503 when my proxied app server is down?
Thanks #shalbafzadeh for that answer, that solved it for me.
In my very particular situation the solution looks like this:
server {
listen 80;
error_page 502 =503 #fallback; <-- THIS
location / {
proxy_pass http://0.0.0.0:3000;
}
location #fallback {
root /srv/my-project/static;
try_files /fallback.html;
}
}
Related
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;
}
}
I'm currently working on a JS Project, that uses the url path. Now if I go on my website with example.com/, the JavaScript won't work, because I actually need example.com/index.html.
I'm already using an reverse proxy to proxy pass to two different docker containers. So my idea was to pass the request to example.com/index.html when example.com/ is called. But I can't figure out the regex stuff to achieve this goal.
My old config:
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location / {
proxy_pass http://structure.example:80;
}
location /cdn {
proxy_pass http://content.example:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Stuff I tried:
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location / {
proxy_pass http://structure.nocms:80/index.html;
}
location ~* \S+ {
proxy_pass http://structure.nocms:80;
}
location /cdn {
proxy_pass http://content.nocms:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
The accepted answer has one disadvantage: going to example.com explicitly redirects to example.com/index.html (that is, returns 301 Moved permanently), which is not always desired.
Instead, I suggest to prepend location / with another directive, location = /, which is designed to the root URL only:
location = / {
proxy_pass http://structure.nocms:80/index.html;
}
location / {
proxy_pass http://structure.nocms:80;
}
The above instructs nginx to pass requests to example.com directly to http://structure.nocms:80/index.html, while requesting any other URLs in example.com/* would pass the request to the corresponding URL in the downstream.
Below config should work for you
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location = / {
rewrite ^ /index.html permanent;
}
location / {
proxy_pass http://structure.example:80;
}
location /cdn {
proxy_pass http://content.example:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I have an Nginx working as a upstream
upstream img_servers {
server localhost:8000;
server localhost:8001 backup;
}
I'm expecting the server listening on 80 would try the 2 servers in the upstream. When the file is not found, it should try the other one.
What I'm doing is
location ~ (jpg|jpeg|png|gif)$ {
proxy_pass http://img_servers;
proxy_next_upstream http_404;
}
However, the upstream server used the image_filter module, and when some image is not found, it always returns 415.
server {
listen 8000;
access_log logs/access_8000.log main;
error_page 415 =404 /empty.gif;
location ~ (sku_\d+(\d)(\d)(\d)_\d+)_(thumb|small)\.(jpg|png|jpeg|gif)$ {
alias /srv/http/media/$2/$3/$4/$1.$6;
image_filter resize 150 -;
image_filter_buffer 2M;
}
}
So when the code runs into the location, and the file doesn't exist, it returns 415 to the proxy. I'm expecting to return 404 so that proxy knows to try the next upstream server. According to the document of error_page I'm expecting to achieve this by:
error_page 415 =404 /empty.gif;
I don't know why this doesn't work for me. Any suggestions?
Finally I found the solution. A little trick would resolve this issue:
error_page 404 =404 /empty.gif;
It seems like although nginx returned 415 to user, while internally it still thinks it's 404. As I'm using the modified version of image filter, it might be the patch which leads to this issue. I'll edit my answer once I made sure.
location ~ (sku_\d+(\d)(\d)(\d)_\d+)_(thumb|small)\.(jpg|png|jpeg|gif)$ {
error_page 415 = /empty;
alias /srv/http/media/$2/$3/$4/$1.$6;
image_filter resize 150 -;
image_filter_buffer 2M;
}
location = /empty
{
return 404;
}
Im using an nginx proxy, and am trying to replace the base url with my server url. For example I would like to proxy www.google.com through my localhost:8000, and replace all base url instances (www.google.com) with (localhost:8000).
My nginx.conf so far:
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://www.google.com/;
rewrite ^/.google.com /localhost:8080 last;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Just remove rewrite directive in 'location /' block. And add break;
location / {
proxy_pass http://www.google.com/;
break;
}
I have the following vhost entry
server {
listen 80;
server_name example.com www.example.com;
#access_log /var/log/nginx/nginx-access.log;
location /media/ {
root /home/luke/django/solentcms;
}
location /admin/media/ {
root /home/luke/virts/django1.25/lib/python2.7/site-packages/django/contrib/admin/media;
}
location / {
proxy_pass http://127.0.0.1:8001;
}
error_page 404 /404.html;
location = /404.html {
root /home/luke/django/solentcms/404;
allow all;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/luke/django/solentcms/404;
allow all;
}
}
However, 404's and 50x errors are still be re-directed to the horrible nginx default pages.Any ideas as to why? This syntax works on one of my other servers.
Cheers.
Are the errors coming from your backend? You may need to add proxy_intercept_errors on; alongside your proxy_pass.