Skip to main content
Version: 5.6

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 advanced 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 failover for OPC Router redundancy with Nginx. The following section covers setting up failover with Nginx Plus and Nginx.

Failover with Nginx Plus

This is an example of an Nginx configuration that enables failover for an OPC Router redundancy pair.

nginx.conf
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
# Determines how many consecutive requests may fail before the endpoint is considered "unhealthy."
fails=1
# Determines how many consecutive requests must be successful 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.

nginx.conf
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;
}
}
}