Nginx server redirect to Error Server not working
January 27, 2020 01:05PM
Hi Team and Techies,

I need your help.
I have been searching for a solution to this problem. Here is my setup.
Server A: 192.168.1.99
Nginx Server Latest version: 01-2020 version. Serves as Reverse proxy for all sites served by Server B.
Apache Server ( Serves two purposes): 1. Serve sites, 2. Serve as Error server if "Server B being down"
Server B: 192.168.1.100
Apache Server: Main Site Server.
Requirement:
When Server B is down, Server A should serve as an "Error page" with same domain name. For example:
if Server B serves: example.com, and if it is down, then Server A will serve the error page as below:
example.com/error404.html if the error is 502 error, it should serve example.com/error502.html from Server A.
error.html can be manipulated using JavaScript to display the appropriate error.
Is this possible using load balancer ? or regular setup. Here is the configuration file:

Server A: (Nginx Server) /etc/nginx/conf.d/example.com.conf

server {
listen 80;
if ($host = www.example.com ) {
return 301 https://example.com$request_uri;
# return 301 https://$host$request_uri;
} # managed by Certbot

if ($host = example.com ) {
return 301 https://example.com$request_uri;
# return 301 https://$host$request_uri;
} # managed by Certbot

}

server {
listen 443 ssl;

server_name www.example.com;
root /var/www/example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;

location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
send_timeout 300;
proxy_intercept_errors on;
error_page 404 500 502 503 504 = @fallback;

proxy_pass https://192.168.1.100:9443/;
proxy_read_timeout 90;
#rewrite http://www.example.com/ https://www.example.com/ redirect;
proxy_redirect https://192.168.1.100:9443/ https://example.com;
}

index index.php index.html index.htm index.nginx-debian.html;

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}

location ~ /\.ht {
deny all;
}

location @fallback {
root /var/www/example.com;
rewrite ^/(.*) https://example.com;
#proxy_pass https://192.168.1.99:6443/;
#proxy_redirect https://192.168.1.99:6443/ https://example.com;
#rewrite ^/(.*) https://192.168.1.99:6443/ permanent;
}


}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 116
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready