In-depth understanding of Nginx: load balancing and reverse proxy

I. Introduction

Before understanding load balancing and reverse proxy, let us first understand what nginx is and its characteristics.

1.What is nginx

It is a high-performance open source web server. It is not only a simple web server, but also a powerful load balancer and reverse proxy tool. Nginx's load balancing and reverse proxy functions play a key role when building large-scale, high-traffic network architectures.

2.Features

  • High performance: Nginx is known for its excellent performance and can efficiently handle a large number of concurrent connections, making it suitable for building high-performance web servers and reverse proxies.

  • Event-driven: Nginx adopts an event-driven architecture and processes requests in an asynchronous and non-blocking manner, improving the system's throughput and response speed.

  • Lightweight: One of the design goals of Nginx is to keep it simple and lightweight, occupying less system resources, and is suitable for environments with limited resources.

  • Load balancing: Nginx provides a load balancing function that can distribute requests to multiple servers to ensure system stability and performance.

  • Reverse proxy: As a reverse proxy server, Nginx can receive client requests and forward these requests to the back-end server, hiding the information of the back-end server and improving the security of the system.

  • Modularity: The modular design of Nginx allows users to selectively enable or disable functions according to needs, providing flexibility and customizability.

2. Reverse proxy

1. Implementation of reverse proxy

A load balancing device (ie, reverse proxy server) is required to distribute user requests and distribute user requests to the backend to truly provide services.

on the service server. The server returns its services to the load balancing device. The load balancing device returns the server's services to the user. opposite

The real server is hidden in the proxy process. The client does not know who is actually providing the service. All the services requested by the client are proxied.

Server processing. The reverse proxy acts on the responder, that is, the server; when we request www.baidu.com this

www.baidu.com is a reverse proxy server. There are many servers that actually provide services. The reverse proxy server will transfer our

The request is forwarded to each server that actually provides the service. Nginx is a reverse proxy server with very good performance, used for load balancing.

balance.

2. Reverse proxy configuration

The reverse proxy module is ngx_http_proxy_module
We need to prepare two nginx servers, one as an application server, which is the real server of the website, and the other as a proxy server. In the process, we mainly configure the proxy server to achieve the purpose of the proxy.

Configure proxy server

#vim /etc/nginx/conf.d/default.conf
server {
    server {
    listen       80;
    server_name  localhost;

    location / {
    proxy_pass http://192.168.91.5;
    proxy_redirect default;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_connect_timeout 30;
    proxy_send_timeout 60;
    proxy_read_timeout 60;
    }
}
nginx -s reload     #重启nginx服务

Reverse proxy specific configuration

proxy_pass :真实后端服务器的地址,可以是ip也可以是域名和url地址
proxy_redirect :如果真实服务器使用的是的真实IP:非默认端口。则改成IP:默认端口。
proxy_set_header:重新定义或者添加发往后端服务器的请求头
proxy_set_header X-Real-IP $remote_addr;#只记录连接服务器的上一个ip地址信息。
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #通过这个选项可以记录真正客户端机器的ip地址

proxy_connect_timeout::后端服务器连接的超时时间发起三次握手等候响应超时时间
proxy_send_timeout:后端服务器数据回传时间,就是在规定时间之内后端服务器必须传完所有的数据
proxy_read_timeout :nginx接收upstream(上游/真实) server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭。像长连接

After the configuration is completed, we can access through the IP address or domain name of the proxy server. All access requests are forwarded by the proxy server to the real website server, thus hiding the real website server.

3. Load balancing

When the proxy server forwards the request to the real website server, if there is only one website, the pressure on the server will be too great and it is extremely easy to go down, causing the website to crash. Therefore, we can add a few more servers to the backend, achieve load balancing through the upstream module, and schedule it by the algorithm.

1. Load balancing algorithm

upstream 支持4种负载均衡调度算法

1、轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器;

2、ip_hash:每个请求按访问IP的hash结果分配,同一个IP客户端固定访问一个后端服务器。可以保证来自同一ip的请求被打到固定的机器上,可以解决session问题。

3、url_hash:按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器。

4、fair:这是比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx本身是不支持 fair的,如果需要使用这种调度算法,必须下载Nginx的 upstream_fair模块。

2. Load balancing configuration

upstream testapp { 
      server 192.168.91.5:8080;
      server 192.168.91.6:8080;
    }
 server {
        ....
        location / {         
           proxy_pass  http://testapp;  #请求转向 testapp 定义的服务器列表         
        }

4. Conclusion

In this article, we take a deep dive into the key concepts of Nginx load balancing and reverse proxy, their benefits, and how to configure and use them to improve the performance and availability of your web services. Nginx's flexibility and high customizability make it ideal for handling high traffic and complex network architectures.

Load balancing ensures the smooth operation of the system and improves overall performance by distributing traffic to multiple servers. Reverse proxy improves the security of the system by hiding the real address of the back-end server. The combination of the two provides powerful tools for building reliable and efficient network infrastructure.

However, Nginx does much more than that. By in-depth study of Nginx's documentation and configuration options, you can discover more advanced features to meet various complex needs. Whether you're dealing with high-traffic online services or building secure and reliable applications, Nginx is always a powerful and reliable partner.

I hope this blog can help you better understand and use Nginx load balancing and reverse proxy to support your network architecture and application performance.

Thanks for reading, and if you have any questions or suggestions, please feel free to leave a message. May your network services always be efficient, secure and reliable!

Guess you like

Origin blog.csdn.net/XX_HK/article/details/134830093