how to change nginx error page? - nginx

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.

Related

Reverse proxy with Cockpit uses HTTP2 when its server block doesn't, but main server block does

I have a main server block:
conf.d/mydomain.conf
server {
listen 80;
listen [::]:80;
server_name mydomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name mydomain.com;
root /var/www/mydomain.com;
index index.html index.php;
include modules/ssl.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location / {
try_files $uri $uri/ =404; autoindex on;
}
}
As well as a server block for a Cockpit reverse proxy:
conf.d/system.mydomain.com.conf
server {
listen 80;
listen 443 ssl;
server_name system.mydomain.com;
location / {
# Required to proxy the connection to Cockpit
proxy_pass https://127.0.0.1:9090;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
# Required for web sockets to function
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Pass ETag header from Cockpit to clients.
# See: https://github.com/cockpit-project/cockpit/issues/5239
gzip off;
}
}
I also have a CNAME record from system.mydomain.com to mydomain.com.
This works well unless I want to make the main server block use HTTP2:
listen 443 ssl http2;
listen [::]:443 ssl http2;
Then, logging into Cockpit at system.mydomain.com returns a page that only says protocol-error and the connection to system.mydomain.com returns a status code 500.
It there any way I can configure nginx to handle the Cockpit requests using HTTP 1.1 and all other traffic on HTTP2?
Unfortunately, you cannot run HTTP/1.1 and h2 on the same port (443). If you are able to choose a different port, you can of course work around the problem.
If you make one server block http2, all other blocks with the same port implicitly also run on h2.
I am of course only referring to Nginx here. I don't know how it is with Apache or HAProxy.

How to set up Nginx correctly when 403 Forbidden on CentOS 7?

On CentOS 7
/etc/hosts:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.1 app1
Installed Nginx from package:
yum install nginx
In /etc/nginx/nginx.conf:
# ...
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# ...
Created a new file under /etc/nginx/sites-available/ named myapp:
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:///home/deploy/myapp/tmp/sockets/unicorn.sock;
}
server {
listen 192.168.0.1:80;
server_name app1;
# Application root, as defined previously
root /home/deploy/myapp/public;
try_files $uri/index.html $uri #app;
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Link it to /etc/nginx/sites-enabled/:
cd /etc/nginx/sites-enabled/
ln -s ../sites-available/myapp
Restart nginx:
service nginx restart
Then try to access url:
curl 192.168.0.1
Got error:
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>
I removed default index.html file under /usr/share/nginx/html path, so it got 403 Forbidden.
Nginx error log /var/log/nginx/error.log:
2017/07/25 03:35:59 [error] 8200#0: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 192.168.0.2, server: _, request: "GET / HTTP/1.1", host: "192.168.0.1"
Why it accessed default /usr/share/nginx/html/ path, but not new added myapp under /etc/nginx/sites-enabled/ directory?
The real problem is, the OS distribution version and package version, makes software different.
Attention: It's CentOS 7.3!
The method I used to install nginx was:
yum update
yum install epel-release
yum install nginx
Then, the nginx version maybe a little different from others like package on Ubuntu. So the usage is not the same, too.
Its directory is:
/etc/nginx/nginx.conf
/etc/nginx/conf.d/
# Notice, there aren't these directories exist!
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/
So the usage is different and the following is necessary!
First, command out the default setting in /etc/nginx/nginx.conf:
# server {
# listen 80 default_server;
# listen [::]:80 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
Second, create the new config for application under /etc/nginx/conf.d/:
# File Name: rails.conf
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:/home/deploy/myapp/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 192.168.0.1:80;
server_name app1;
# Application root, as defined previously
root /home/deploy/myapp/public;
try_files $uri/index.html $uri #app;
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-FORWARDED_PROTO https;
proxy_pass http://app;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
If exist default.conf under /etc/nginx/conf.d/, remove it.
Third, check syntax and restart nginx:
nginx -t
service nginx restart
It will access the path pointed to /home/deploy/myapp/public when run curl 192.168.0.1.
The error your getting is saying that nginx is not able to access the index folder of /usr/share/nginx/html/ this is happening when it hot the tryfile #app directive in the app.conf.the reason for that is that by default nginx have autoindex off; that mean if you request a / path it will not allowed on a try_file.
see:
autoindex
in your case you need to add autoindex on; directive in the server before the try_file directive.

Nginx Unkown File when restarting after changing default

I want to set my blog to point towards yourdomainanme/blog and to host a static page on the default yourdomainname.com. I changed the paths but It does not find the html file for some reason.
Error
nginx: [emerg] unknown directive "home" in /etc/nginx/sites-enabled/default:14
nginx: configuration file /etc/nginx/nginx.conf test failed
My Default File
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name yourdomain.com; # Replace with your domain
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 10G;
location / {
root var/www/;
home home.html;
}
location /blog {
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}
I believe you are confusing the non-existent home keyword with the index keyword, which tells NGINX the 'default' or 'index' file in the root directory.
location / {
root var/www/;
home home.html;
}
should be
location / {
root var/www/;
index home.html;
}

Nginx: setting the error page to a static file

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

nginx not stripping www using rewrite

I want to strip the www from my url in my nginx configuration and looking around the documentation and stack overflow posts I wrote the following configuration but it doesn't seem to be working.
server_name {
server_name www.subdomain.domain.com;
rewrite ^(.*) https://subdomain.domain.com/$1 permanent
}
server_name {
server_name subdomain.domain.com;
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /file;
index index.html index.htm app.js;
location /{
proxy_pass https://subdomain.domain.com:443/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /*/ {
proxy_pass https://subdomain.domain.com:443/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
error_page 404 /404.html;
}
But it doesn't seem to be working at all. However whenever I try to go to the site using www.subdomain.domain.com i can't access the site but doing https://subdomain.domain.com works fine. Any advice on this would be great thanks.
You have typo in nginx config (server_name instead server), and try return instead rewrite as more proper way:
server {
server_name www.subdomain.domain.com;
return 301 $scheme://subdomain.domain.com$request_uri;
}

Resources