Home > Article > Operation and Maintenance > How to configure a highly available reverse proxy (such as Nginx) on Linux
How to configure a highly available reverse proxy (such as Nginx) on Linux
Abstract:
Reverse proxy is a technology commonly used in network architecture to improve the load balancing capabilities of services and fault tolerance. This article will introduce how to configure a highly available reverse proxy on a Linux system, taking Nginx as an example.
Installing Nginx
First, we need to install Nginx on the Linux system. The installation can be completed with the following command:
sudo apt-get update sudo apt-get install nginx
Configuring the reverse proxy
Configuring the reverse proxy in Nginx is very simple. Edit the Nginx configuration file /etc/nginx/nginx.conf
and add the following content in the http block:
http { upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } }
In the above configuration, we defined a file named backend
's upstream block, which lists the backend servers that require proxies. The server
block specifies the listening port and domain name and forwards the request to backend
.
a. IP Hash
upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; server backend3.example.com; }
The IP Hash policy distributes the client's requests to a specific server based on its IP address. end server, so that all requests from the same client can be sent to the same server.
b. Least Connections
upstream backend { least_conn; server backend1.example.com; server backend2.example.com; server backend3.example.com; }
The Least Connections policy will send requests to the server with the smallest number of current connections to achieve load balancing.
Reload Nginx configuration
After completing the configuration, you need to reload the Nginx configuration file for the changes to take effect. The operation can be completed through the following command:
sudo nginx -s reload
High availability configuration
In order to achieve high availability, multiple Nginx nodes can be formed into a cluster and use tools such as Keepalived to achieve failover . The following is a simple example, assuming there are two nodes in the cluster:
sudo apt-get install keepalived
On each node, you need to edit the Keepalived configuration file, usually located at /etc/keepalived/keepalived.conf
. The following is an example configuration:
global_defs { router_id LB_NODE1 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 virtual_ipaddress { 192.168.1.100 } }
where state
specifies the node's status (MASTER or BACKUP), virtual_router_id
specifies the virtual router ID, virtual_ipaddress
The virtual IP address is specified.
Finally, restart the Keepalived service to make the configuration take effect:
sudo service keepalived restart
Summary:
This article introduces the steps to configure a highly available reverse proxy on a Linux system , and related code examples. Through these configurations, the load balancing capability and fault tolerance of the service can be improved, thereby achieving higher availability and stability. At the same time, it also introduces how to use tools such as Keepalived to implement reverse proxy failover. I hope this content will be helpful to friends who want to build a highly available reverse proxy system.
The above is the detailed content of How to configure a highly available reverse proxy (such as Nginx) on Linux. For more information, please follow other related articles on the PHP Chinese website!