Nginx (download and install, common commands, reverse proxy, load balancing)

Official website:

https://nginx.org/

Nginx is a lightweight web server/reverse proxy server and e-mail (IMAP/POP3) proxy server, which is characterized by less memory and strong concurrency.

download and install

download

On the download page of Nginx's official website ( http://nginx.org/en/download.html ), the current Nginx version is displayed and a download link is provided.

Install

Install dependencies

Since nginx is developed based on C language, it is necessary to install a C language compilation environment and third-party dependent libraries such as regular expression libraries.

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

Download the Nginx installation package (the wget command is used to download files from the specified URL)

yum install wget 
wget https://nginx.org/download/nginx-1.16.1.tar.gz

Unzip the nginx compressed package

tar -zxvf nginx-1.16.1.tar.gz

Configure the Nginx compilation environment (the directory specified by --prefix is ​​the directory where Nginx is installed)

cd nginx-1.16.1
./configure --prefix=/usr/local/nginx

compile & install

make & make install

Enter the installation directory /usr/local/nginx, you can see some important directories and files.

directory/file

illustrate

Remark

conf

The storage directory of the configuration file

conf/nginx.conf

Nginx core configuration file

There are many nginx configuration files under conf, but the core configuration file is mainly operated

html

Store static resources (html, css)

Static resources deployed to Nginx can be placed in the html directory

logs

Store nginx logs (access logs, error logs, etc.)

sbin/nginx

Binary file for starting and stopping Nginx service

Nginx common commands

In Nginx, our binary executable file (nginx) is stored in the sbin directory. When executing the following instructions, it needs to be executed in the /usr/local/nginx/sbin/ directory.

view version

./nginx -v

check configuration file

After modifying the nginx.conf core configuration file, before starting the Nginx service, you can check whether there is any error in the configuration of the conf/nginx.conf file. The command is as follows:

./nginx -t

start up

./nginx

Use the ps -ef | grep nginx command to check whether the nginx process exists.

After startup, we can directly access port 80 of Nginx, http://ip address

In order to access Nginx normally, you need to close the firewall or open the specified port number. The command to execute is as follows:

A. Turn off the firewall

systemctl stop firewalld

B. Open port 80

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

stop

./nginx -s stop

Reload

After modifying the Nginx configuration file, it needs to be reloaded to take effect. You can use the following command to reload the configuration file:

./nginx -s reload

When using the nginx command to start, stop, and reload the service, you need to use a command nginx, and this command is in the nginx/sbin directory. Every time you use this command, you need to switch to the sbin directory. Relatively cumbersome to use. Then, by configuring the environment variables of nginx, you can execute this command in any directory to operate nginx.

1. Open the /etc/profile file through the vim editor, and add the sbin directory of nginx to the PATH environment variable.

2. After modifying the configuration file, execute source /etc/profile to make the file take effect.

Nginx application

Configuration file structure

The nginx configuration file (conf/nginx.conf) is generally divided into three parts: global block, events block, and http block.

global block

Configure global configuration related to nginx operation

events block

Configuration related to network connection

http block

Configure proxy, cache, logging, virtual host and other configurations

In the global block, events block and http block, we often configure the http block, which can contain multiple server blocks, and each server block can be configured with multiple location blocks.

Static resource deployment

Nginx can be used as a static web server to deploy static resources, including common html pages, css files, js files, pictures, videos and other resources. Compared with Tomcat, Nginx is more efficient in handling static resources, so in a production environment, static resources are generally deployed to Nginx.

Deploying static resources to Nginx 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;    #指定默认首页
    }
}

reverse proxy

Regarding the forward proxy, it is a server located between the client and the original server (origin server). In order to obtain content from the original server, the client sends a request to the proxy and specifies the target (origin server), and then the proxy forwards the content to the original server. Request and return the obtained content to the client. A typical use of a forward proxy is to provide a way for LAN clients inside the firewall to access the Internet. Forward proxy generally sets up a proxy server on the client side , forwards the request through the proxy server, and finally accesses the target server.

The reverse proxy server is located between the user and the target server, but for the user, the reverse proxy server is equivalent to the target server, that is, the user can directly access the reverse proxy server to obtain the resources of the target server, and the reverse proxy server is responsible for Forward 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. For the user, accessing the reverse proxy server is completely unaware.

In nginx, the reverse proxy can be configured in nginx.conf:

server {
    listen 82; #监听的端口
    server_name localhost;
    location / {
        proxy_pass http://ip地址:端口;   #反向代理配置,将请求转发到指定服务
    }
}

For example: when accessing port 82 of nginx, according to the reverse proxy configuration, the request will be forwarded to the service corresponding to http://ip address:port.

load balancing

An application cluster composed of multiple servers can perform horizontal expansion of performance and avoid single point of failure.

Application cluster : Deploy the same application to 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 to a server in the application cluster for processing according to the corresponding load balancing algorithm.

Nginx's load balancing is based on reverse proxy, but the proxy server is not one, but multiple.

Configure load balancing in nginx (after the configuration is complete, reload the nginx configuration file)

#upstream指令可以定义一组服务器
upstream targetserver{    
    server 主机1的ip地址;
    server 主机2的ip地址;
}

server {
    listen       8080;
    server_name  localhost;
    location / {
        proxy_pass http://targetserver;
    }
}

The load balancing strategy adopts the round robin strategy by default, and other load balancing strategies are also provided in Nginx.

polling

default method

weight

weight method

Distribute requests according to the weight, and the probability of assigning a large weight to the request is high

ip_hash

According to the ip allocation method

Calculate the hash value according to the IP address requested by the client, and distribute the request according to the hash value. Requests initiated by the same IP will be forwarded to the same server

least_conn

According to the least connection method

Which server currently handles fewer connections, the request is forwarded to this server first

url_hash

According to the url distribution method

Distribute the request according to the hash value of the url requested by the client. The same url request will be forwarded to the same server

fair

By response time method

Prioritize the distribution of requests to servers with short processing time

Configuration of weights

#upstream指令可以定义一组服务器
upstream targetserver{    
    server IP地址:端口 weight=2;
    server IP地址:端口 weight=1;
}

Guess you like

Origin blog.csdn.net/crazy_xieyi/article/details/129149264