Linux, Nginx learning
- A, Nginx Overview
- Second, install Nginx configuration Introduction of
- Three, Nginx practical operation case
- 3.1 Reverse Proxy
- 3.1.1 Preparations
- 3.1.2, visits analysis
- 3.1.3, the direction of the proxy configuration
- 3.1.4 Test
- 3.2, the reverse proxy - Example 2
- 3.3, load balancing
- 3.3, static and dynamic separation
- Fourth, high availability clustering
- 4.1, the concept of
- 4.2, ready to work
- 4.3, configuration work
- 4.4 Test
- 4.5, high-availability configuration file Detailed
- Five, nginx principle
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
1.2.1, forward proxy
The client (browser) to configure a proxy server to access the Internet through a proxy server performed.
1.2.2 reverse proxy
1.3, load balancing
Distribute requests to each server by a single reverse proxy server.
1.4, static and dynamic separation
Will be deployed separately from dynamic resource and static resources
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
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
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
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
3.1.3, the direction of the proxy configuration
After you configure reload configuration.
3.1.4 Test
3.2, the reverse proxy - Example 2
Jump different access routes different ports.
3.2.1 Preparations
Prepare two tomcat server, a 8080 port, a 8081 port.
3.2.2, concrete placement
Nginx.conf modify configuration files
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
3.3.3, nginx server allocation strategy
3.3.3.1, polling (default)
3.3.3.2, weight (weight)
Representative weight weight of 1, the higher weight is assigned to give the client more.
3.3.3.3、ip_hash
3.3.3.4, fair (third party)
3.3, static and dynamic separation
3.3.1 ready
Prepare some static resources in the Linux system, provide resources for later testing.
3.3.2 configuration
3.3.3 Test
This is should be configured in the configuration file autoindex on;
Fourth, high availability clustering
4.1, the concept of
4.2, ready to work
# 安装keepalived
yum install keepalived -y
4.3, configuration work
4.3.1, kepalived.conf profile
FIG modifications required configuration from the server
4.3.2, nginx_check.sh profile
4.4 Test
4.4.1, and start ginx keepalived on both servers
4.4.2 Test Access
Use virtual IP access, you can access to find keepalived previous instructions for configuring nginx no problem.
Check the following command line can be seen 50 has been tied to the ens33 the NIC.
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
4.5, high-availability configuration file Detailed
In the / etc / hosts file needs to be configured as in FIG.
Five, nginx principle