Redundancy failover with Nginx
What is Nginx?
Nginx is a powerful, open-source web and reverse proxy server known for its performance and scalability. In addition to the open source version, there is Nginx Plus, a commercial solution with extended features such as security functions and support. Both are used for caching, load balancing and the provision of modern web applications.
There are several ways to set up a failover for OPC router redundancy with Nginx. The setup of a failover with Nginx Plus and Nginx is described below.
Failover with Nginx Plus
This is an example of an Nginx configuration that enables failover for an OPC router redundancy pair.
events { }
http {
upstream opcrouter-service {
zone opcrouter-service 64k;
# Defines the servers to which a request can be forwarded.
server opcrouter-primary:8080;
server opcrouter-secondary:8080;
}
server {
listen 80;
location /api {
# Replaces /api/ with /services/MyRestServer/api/
rewrite ^/api/(.*) /services/MyRestServer/api/$1 break;
# Forward the request to one of the endpoints defined in opcrouter-service.
proxy_pass http://opcrouter-service;
# Check the health of the endpoints
health_check
# Interval in seconds at which the health of the endpoints is checked.
interval=5
# Defines how many consecutive times the request may fail before the endpoint is considered "Unhealthy".
fails=1
# Defines how many times the request must be successful in succession before the endpoint is considered "Healthy".
passes=1
# The url used to determine the health status.
uri=/health/runtime/ready;
}
}
}
Failover with Nginx
Active health checks with health_check
are not available in the free version. Therefore, a passive health check is used in the following example.
events { }
http {
upstream opcrouter-service {
# Defines the servers to which a request can be forwarded.
server opcrouter-primary:8080 max_fails=2 fail_timeout=5s;
server opcrouter-secondary:8080 max_fails=2 fail_timeout=5s;
# max_fails is the number of failures that may occur within the fail_timeout before the server is temporarily (for fail_timeout) considered "Unhealthy".
}
server {
listen 80;
location /api {
rewrite ^/api/(.*) /services/MyRestServer/api/$1 break;
proxy_pass http://opcrouter-service;
# Specifies in which cases the request should be repeated with another server. In this case for errors, timeouts, and http codes 404 and 503
proxy_next_upstream error timeout http_404 http_503;
}
}
}