Redundancy and 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 advanced features such as security functions and support. Both are used for caching, load balancing, and deploying modern web applications.
There are several ways to set up failover for an OPC router redundancy pair using Nginx. The following sections show example configurations using Nginx Plus and the open-source version of Nginx.
These examples assume that internal OPC router redundancy is already set up. Nginx does not handle the redundancy decision here; instead, it simply forwards requests to the currently active endpoint.
Failover with Nginx Plus
This example shows an Nginx configuration that enables failover for a pair of redundant OPC routers.
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;
# Forwards the request to one of the endpoints defined in opcrouter-service.
proxy_pass http://opcrouter-service;
# Checks the availability of the endpoints.
health_check
# Interval in seconds at which the endpoints are checked.
interval=5
# Specifies how many consecutive requests may fail before an endpoint is considered "Unhealthy."
fails=1
# Specifies how many consecutive successful requests are required before the endpoint is considered "Healthy."
passes=1
# URL used to determine the health status.
uri=/health/runtime/ready;
}
}
}
Failover with Nginx
Active health checks using 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 fail_timeout before the server is considered "unhealthy" for fail_timeout.
}
server {
listen 80;
location /api {
rewrite ^/api/(.*) /services/MyRestServer/api/$1 break;
proxy_pass http://opcrouter-service;
# Specifies the cases in which the request should be retried with another server—in this case, for errors, timeouts, and HTTP status codes 404 and 503.
proxy_next_upstream error timeout http_404 http_503;
}
}
}