Nginx feature application and loading

        Nginx is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server. Its characteristic is that it occupies less memory and has strong concurrency capability. In fact, the concurrency capability of nginx is better than other web servers of the same type. Websites using nginx in mainland China include: NetEase, Tencent, Ali, etc.
        Nginx was developed by Igor Sesuyev for the second most visited Rambler.ru site in Russia (Russian: Panfnep). The first public version was released on October 4, 2004.
Official website:  nginx news icon-default.png?t=N6B9https://nginx.org/

feature application

        The application of three major features of Nginx: dynamic and static separation, reverse proxy, load balancing .

        These three functions are very commonly used.

        (1) Nginx can be used as a static web server to deploy static resources . For example, static resources such as common html pages, css and js files, pictures and videos. Compared with Tomcat, Nginx is more efficient and fast in handling static resources, so in a production environment, static resources are generally deployed to Nginx. Deployment is very simple, just copy the files to the html directory under the Nginx installation directory.

server { 
    listen 80;  # 监听端口 
    server_name localhost;  # 服务器名称 
    location / {  # 匹配客户端请求url 
        root html;  # 指定静态资源根目录  
        index index.html;  # 指定默认首页
    }
}

        (2) Nginx has the most "reverse proxy", that is, the proxy server.
The reverse proxy server is located between the user and the target server. For the user, the reverse proxy server is equivalent to the target server. The user can directly access the reverse proxy server, and the reverse proxy server is responsible for forwarding the request to the target server. The user does not need to know the address of the target server, and does not need to make any settings on the client side .

server {
    listen 82;
    server_name localhost;
    location / {
        proxy_pass http://127.0.0.1:8080;  # 反向代理配置,将请求转发到指定服务
    }
}

        (3) "Load balancing" is also used a lot

The early website traffic and business functions were relatively simple, and a single server could meet the basic needs. As the business traffic increased, the business logic became more and more complex, and the performance and single point of failure of a single server became prominent. Therefore, it is necessary to Multiple servers form an application cluster to scale performance horizontally and avoid single point failures. ·
Application cluster - the same application is deployed on multiple machines to form an application cluster, receive requests distributed by the load balancer, perform business processing and return response data; load balancer - distribute
user requests according to the corresponding load balancing algorithm Processed by a server in the application cluster.

# 配置负载均衡:
upstream targetserver {  # upstream指令可以定义一组服务器
    server 192.168.138.101:8080;
    server 192.168.138.101:8081;  # 甚至还可以配权重weight=xx,比如一个设为1,一个设为2,默认轮询
}
server {
    listen 8080;
    server_name localhost;    
    location / {
        proxy_pass http://targetserver;
    }
}

to load

Official website download page: nginx: download icon-default.png?t=N6B9https://nginx.org/en/download.html

The Stable version is more recommended.

Linux installation steps:

# 安装依赖包 
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
# 下载Nginx安装包 (可以在Linux系统上下载刚刚上面的安装包)
wget https://nginx.org/download/nginx-1.20.2.tar.gz 
# 解压
tar -zxvf nginx-1.20.2.tar.gz
#  
cd nginx-*
# 创建目录并配置
mkdir -p /usr/local/nginx
./configure --prefix=/usr/local/nginx 
# 安装
make && make install

 Start the Nginx service using the following command: ./nginx

To stop the Nginx service use the following command: ./nginx -s stop

After the startup is complete, you can view the Nginx process: ps -ef | grep nginx

 

Guess you like

Origin blog.csdn.net/lxd_max/article/details/132059204