How to configure a highly available reverse proxy (such as Nginx) on Linux

PHPz
Release: 2023-07-05 08:57:18
Original
1546 people have browsed it

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.

  1. 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
    Copy after login
  2. 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;
         }
     }
    }
    Copy after login

    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.

  3. Configuring load balancing strategy
    In the above configuration, the round-robin load balancing strategy is used by default. That is, each request is distributed to the backend server in sequence. If a more complex load balancing strategy is required, it can be configured as needed.
    The following are some common load balancing policy examples:

a. IP Hash

upstream backend {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;
}
Copy after login

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;
}
Copy after login

The Least Connections policy will send requests to the server with the smallest number of current connections to achieve load balancing.

  1. 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
    Copy after login
  2. 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
    Copy after login

    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
     }
    }
    Copy after login

    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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template