Linux, Nginx study notes

A, Nginx Overview

1.1, Nginx Profile

Nginx (engine x) is a high-performance HTTP server and reverse proxy web server, but also provides IMAP / POP3 / SMTP services. Nginx by Igor Saisuoyefu of Russia visited the second Rambler.ru site (Russian: Рамблер) developed the first public version 0.1.0 was released in 2004 October 4.

1.2 Proxy

Here Insert Picture Description

1.2.1, forward proxy

The client (browser) to configure a proxy server to access the Internet through a proxy server performed.
Here Insert Picture Description

1.2.2 reverse proxy

Here Insert Picture Description

1.3, load balancing

Distribute requests to each server by a single reverse proxy server.
Here Insert Picture Description

1.4, static and dynamic separation

Will be deployed separately from dynamic resource and static resources
Here Insert Picture Description

Second, install Nginx configuration Introduction of

2.1, nginx installation

2.1.1 Preparations

# 1.Linux的编译需要gcc包,如果没有需要安装一下
yum install gcc-c++ 
# 2.PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
# 3.zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
# 4.OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel

2.1.2, download nginx

nginx Download
Here Insert Picture Description
the downloaded file on Linux, and extract to the current directory.
Then enter the directory nginx

# 告诉安装的文件要放在哪里。(也可以不设置,直接 ./configure)
./configure --prefix=/usr/local/nginx 
# 编译并安装
make && make install

2.1.3 start nginx

By nginx startup directory entry to, the following command to start nginx

./nginx

Here Insert Picture Description
Here Insert Picture Description

2.2, nginx commonly used commands

Use nginx operation command prerequisite is to enter into ngix the / usr / local / nginx under / sbin directory.
Note: If you do not want every time to this directory to perform, you can execute the following command. Add soft

ln -s /usr/local/nginx/sbin/nginx    /usr/bin/nginx
command DEFINITIONS
./nginx -v View the current version number
./nginx -s stop Stop process
./nginx Start the process
./nginx -s reload Reload configuration file
systemctl status nginx.service View the process status

2.3, nginx configuration files

Path 2.3.1, nginx configuration files

/usr/local/nginx/conf
Here Insert Picture Description

Composition 2.3.2, nginx configuration files

nginx profile consists of three parts
1, global block
starts from the configuration file to the content blocks between the events, the overall operation of some of the major effects provided nginx server configuration instructions.
work_processes 1; configured value, the larger the carrying amount of concurrency. But subject to hardware limitations.
2, events blocks
Effect nginx server and the user's network connection.
For example: worker_connections 1024; supports the maximum number of connections;
3, http block
nginx server portion, http block configuration most frequently comprise blocks and global http server block.

Three, Nginx practical operation case

3.1 Reverse Proxy

To achieve the effect: After opening the browser, enter the access address so that tomcat jump to the main page.

3.1.1 Preparations

1, nginx install and start
2, launch applications on Linux, and uses port 8080.
3. It should be particularly noted that the firewall port. You need to open port 8080.

3.1.2, visits analysis

Here Insert Picture Description

3.1.3, the direction of the proxy configuration

Here Insert Picture Description
After you configure reload configuration.

3.1.4 Test

Here Insert Picture Description

3.2, the reverse proxy - Example 2

Jump different access routes different ports.
Here Insert Picture Description

3.2.1 Preparations

Prepare two tomcat server, a 8080 port, a 8081 port.

3.2.2, concrete placement

Nginx.conf modify configuration files
Here Insert Picture Description

3.2.3 to see if the need for open ports open

# 查看已开放的端口
firewall-cmd --list-port

If you do not open, you need to open the appropriate ports.

3.3, load balancing

3.3.1, achieve results

Over the same browser to access, load balancing. That will let nginx access request to distribute an average of 8080 and 8081 two applications.

3.3.2, configure load balancing

Here Insert Picture Description
Here Insert Picture Description

3.3.3, nginx server allocation strategy

3.3.3.1, polling (default)

Here Insert Picture Description

3.3.3.2, weight (weight)

Representative weight weight of 1, the higher weight is assigned to give the client more.
Here Insert Picture Description

3.3.3.3、ip_hash

Here Insert Picture Description

3.3.3.4, fair (third party)

Here Insert Picture Description

3.3, static and dynamic separation

Here Insert Picture Description
Here Insert Picture Description

3.3.1 ready

Prepare some static resources in the Linux system, provide resources for later testing.
Here Insert Picture Description

3.3.2 configuration

Here Insert Picture Description

3.3.3 Test

This is should be configured in the configuration file autoindex on;
Here Insert Picture Description
Here Insert Picture Description

Fourth, high availability clustering

4.1, the concept of

Here Insert Picture Description
Here Insert Picture Description

4.2, ready to work

Here Insert Picture Description

# 安装keepalived
yum install keepalived -y

4.3, configuration work

4.3.1, kepalived.conf profile

Here Insert Picture Description
Here Insert Picture Description
FIG modifications required configuration from the server
Here Insert Picture Description

4.3.2, nginx_check.sh profile

Here Insert Picture Description

4.4 Test

4.4.1, and start ginx keepalived on both servers

Here Insert Picture Description

4.4.2 Test Access

Use virtual IP access, you can access to find keepalived previous instructions for configuring nginx no problem.
Here Insert Picture Description
Check the following command line can be seen 50 has been tied to the ens33 the NIC.
Here Insert Picture Description

4.4.3 Stop Master Test Access

# 停止主服务器的keepalived
systemctl stop keepalived.service
# 停止nginx
systemctl stop nginx.service

Refresh the page, you can see or have access to
Here Insert Picture Description

4.5, high-availability configuration file Detailed

Here Insert Picture Description
In the / etc / hosts file needs to be configured as in FIG.
Here Insert Picture Description

Five, nginx principle

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Published 76 original articles · won praise 16 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_38657051/article/details/104907392