Nginx: setting the error page to a static file - nginx

I have the following nginx configuration:
server {
listen 80;
server_name default;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080/;
}
}
When the internal service at http://127.0.0.1:8080/ is not responding nginx return a 502 Bad Gatway error.
My question: how can I configure nginx to returning a static html file, e.g. /var/www/error.html, when such error occurs?
What I tried
Taking a cue from here I tried this:
server {
listen 80;
server_name default;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080/;
}
error_page 404 502 /error.html;
location = /error.html {
internal;
root /var/www;
}
}
It's working fine if the service (on the port 8080) is down, but when the service is up and I try to access the url /error.html nginx match the last location (returning me the static file) and I would like to avoid it.

Well, if you really don't want to expose error page, you could use named location.
server {
listen 80;
server_name default;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080/;
# probably you also need this
# proxy_intercept_errors on;
}
error_page 404 502 #error;
location #error {
root /var/www;
try_files /error.html /error.html;
}
}

Related

how to change nginx error page?

I use AWS ec2 ami and nginx and node.js express
how to change nginx err page html or ejs??
like express error handler...
my nginx server block
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3000;
proxy_redirect off;
}
error_page 404 http://example.com/error;
error_page 500 502 503 504 http://example.com/error;
To have a custom error page for 404 and 50x errors :
First save your custom error pages in the /usr/share/nginx/html directory which is Nginx's default document root.
Create a page for 404 errors called customerror_404.html and one for 50x errors say customerror_50x.html.
echo " This is not the page you looking for!! Error 404 " | sudo tee /usr/share/nginx/html/customerror_404.html
similarly for customerror_50x.html file.
Nginx defaults must be aware of to use these custom files
so open nginx default,
vi /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server ;
server_name localhost;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
. . .
error_page 404 /customerror_404.html;
proxy_intercept_errors on;
location = /customerror_404.html {
root /usr/share/nginx/html;
internal;
}
error_page 500 502 503 504 /customerror_50x.html;
location = /customerror_50x.html {
root /usr/share/nginx/html;
internal;
}
}
save the file and restart nginx
test the changes :
http://server_hostname_or_IP/anynonexistingfilename
You are good to go.

Kibana dashboard couldn't connect with Nginx

Hi i'm trying to use Nginx as a reverse proxy for accessing a Kibana 4 dashboard. The location of the dashboard is not available in the latest kibana but it can be accessed using a URL.
Kibana and Nginx are running both locally and installed on a windows machine installed in C:\
Kibana is running on localhost:5601.
I installed NGinx and configured it to run on port 80. My config file of Nginx looks like this.
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name 127.0.0.1:5601;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ {
proxy_pass http://127.0.0.1:5601;
#proxy_redirect https://kibana/;
}
}
But when i enter localhost in my browser i see,
"Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx."
Kibana is working fine with : localhost:5601.
Do i need to make any changes to the Kibana config file also? I want to access the kibana dashboard by localhost:80 through NGinx.
Thanks
Change "server_name 127.0.0.1:5601;" to "server_name localhost:80;"
Add this upstream above "server {" :
upstream kibana {
server localhost:5601;
}
and then replace "location ~" with :
location /kibana/ {
proxy_pass http://kibana/;
}
Use http://localhost/kibana to access Kibana
I have configured my nginx to reverse proxy the kibana-4 dashboard. The following nginx config does the job for me:
server {
listen 80;
#You can add your fqdn, say example.com, if you want to in the next parameter
server_name localhost;
auth_basic off;
location / {
proxy_pass http://localhost:5601;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
here is how you can proxy to kibana through nginx kibana and ES on a remote server with https using letencrypt
server {
listen [some_port] ssl http2;
server_name [server_name];
root /your/root/directoty;
location /app {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/conf.d/yyyyyyyyy.passwd;
proxy_pass http://example.com:5601;
}
location /bundles {
proxy_pass http://example.com:5601/bundles;
}
location /elasticsearch {
proxy_pass [http://elasticsearch_server:9200;]
}
location /status {
proxy_pass http://example.com:5601/status;
}
location /api {
proxy_pass http://example.com:5601/api;
}
location /plugins {
proxy_pass http://example.com:5601/plugins;
}
location /ui {
proxy_pass http://example.com:5601/ui;
}
location /es_admin {
proxy_pass http://example.com:5601/es_admin;
}
location /built_assets {
proxy_pass http://example.com:5601/built_assets;
}
location /node_modules {
proxy_pass http://example.com:5601/node_modules;
}
location /translations {
proxy_pass http://example.com:5601/translations;
}
location /internal {
proxy_pass http://example.com:5601/internal;
}
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/yourdomain.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;
access_log /var/log/nginx/xxxx.access.log;
error_log /var/log/nginx/xxxxx.error.log;
passenger_enabled on;
passenger_min_instances 1;
client_max_body_size 10m;
}

Nginx giving error 403 only on location /

I am trying to serve static html files via nginx. In my nginx config, I have:
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /home/django/django_project/static/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin;
}
location /static {
alias /home/django/django_project/static;
}
location /minion {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
# this is what im talking about
location /html {
alias /home/django/django_project/static/html;
}
location / {
alias /home/django/django_project/static/html;
}
}
(note how the two last definitions are identical aside from the location path). Now, if I go to mywebsite.tld, I get a 403 error for some weird reason, while going to mywebsite.tld/html works exactly as it is supposed to be.
What is the explanation for this behaviour? What mistake did I make so it does this? Obviously, the file perms are ok, the index and files all exist (since /html works), it has to do with the location /, but I dont have a clue why it would... Please help me out here.

How to redirect requests on IP to domain

Every once in a while someone is trying to access our website through the public ip instead of one of our domains (yes we have multiple country specific domains .dk, .it, .es etc. however we also have .com as the "general").
Now I would like to redirect those requests coming in on the IP-adress to our www.domain.com domain instead. How would I do that in nginx without touching anything BUT requests going directly to the IP?
Here is my nginx.conf
upstream unicorn {
server unix:/tmp/unicorn.mysite.sock fail_timeout=0;
}
server {
listen 80 default deferred;
root /home/deployer/apps/mysite/current/public;
proxy_set_header X-Request-Start "t=${msec}";
if (-f $document_root/system/maintenance.html) {
return 503;
}
error_page 503 #maintenance;
location #maintenance {
rewrite ^(.*)$ /system/maintenance.html last;
break;
}
location ~ ^/assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
So the question would be, how do I modify this to redirect e.g. http://123.123.123.123/some_page to http://www.mysite.com/some_page but leave all other like http://www.mysite.dk/some_pageunchanged?
You could setup a catchall server with the following directives:
listen 80 default_server;
server_name _;
If you have multiple server stanzas, you can use default_server to specify the configuration that is used if the hostname doesn't match any of the other server_name entries. Using an ip number fits this scenario. The server_name _; line acts as a null server_name entry.
It's also useful if one of your servers has multiple domain names.

nginx and monit web interface - how to have monit on domain.com/monit versus domain.com

I am using monit on my ec2 instance and I am new to nginx. Below is my nginx config file:
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass 127.0.0.1:2812;
proxy_set_header Host $host;
}
}
So..if I go to domain.com I see monit. How do I modify above code where I can see monit on domain.com/monit?
Thanks
Please, try this:
server {
listen 80;
server_name 127.0.0.1;
location /monit/ {
proxy_pass http://127.0.0.1:2812;
proxy_set_header Host $host;
}
}
Please, read more here about how directive location works in nginx
There's an article in Monit's wiki how to configure it with Nginx.
Here's my /etc/nginx/conf.d/monit.conf:
server {
listen 80;
server_name my.server.name;
location /monit/ {
allow 127.0.0.1;
allow 192.0.0.0/8;
deny all;
proxy_pass http://127.0.0.1:2812;
proxy_set_header Host $host;
rewrite ^/monit/(.*) /$1 break;
proxy_ignore_client_abort on;
}
}
Sergei already correctly answered your direct question. I think it's also worth noting that this may be cleaner to just use a subdomain:
server {
listen 80;
server_name monit.domain.com;
location / {
proxy_pass 127.0.0.1:2812;
proxy_set_header Host $host;
}
}

Resources