Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451
Overview
This article will introduce the very popular Nginx server. We will understand the concepts of reverse proxy and load balancing, and then master the installation and configuration of Nginx. You can use Nginx+Vsftp configuration file to upload the server and realize the configuration of load balancing.
1. Introduction to Nginx
Nginx is a lightweight, high performance, high stability, and concurrency HTTP and reverse proxy server. At present, it is widely used in Internet companies, such as Baidu, JD, Sina, Netease, Tencent, Taobao, etc.
1.1 What can Nginx do
The main functions of Nginx are:
-
http server
Nginx is generally used to deploy static resources and deploy them separately from servers (such as Tomcat) that deploy dynamic resources to achieve separation of dynamics and statics and maximize server performance.
-
Reverse proxy
Proxy background server, flexible routing through configuration
-
Load balancing
A large number of user requests are evenly distributed to multiple servers to improve the load capacity of the system
1.2 Forward/reverse proxy
An important role of Nginx is a reverse proxy, so we first need to figure out what a reverse proxy is.
The difference between forward proxy and reverse proxy is:
-
Forward proxy, the proxy is the client
For example, users cannot directly access the internal server of the enterprise, but can access indirectly through the VPN proxy server, which is a kind of forward proxy
-
Reverse proxy, the proxy is the server
For example: Taobao's business is deployed on different servers, such as commodity servers, merchant servers, order servers, etc. When users access Taobao, requests are forwarded to different back-end servers through Nginx
2. Installation of Nginx
2.1 Install Nginx in Linux
1) Install dependent libraries
yum install -y gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
2) Download Nginx
cd /usr/local
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
3) Unzip
tar -zxvf nginx-1.12.0.tar.gz
4) Perform default configuration
cd nginx-1.12.0
./configure
5) Compile and install
make
make install
6) Start nginx
After compiling and installing, the nginx directory will appear
cd /usr/local/nginx/sbin/
./nginx
Other commands:
停止
./nginx -s stop
退出
./nginx -s quit
重启
./nginx -s reload
7) Open port 80
firewall-cmd --zone=public --add-port=80/tcp --permanent
Open the browser, enter the IP address, the Nginx page appears
2.2 Install Nginx in Windows
1) Go to the official website to download Nginx
http://nginx.org/en/download.html
2) Unzip and double click nginx.exe
3. Nginx+Vsftp build file server
Nginx as an http server has a very high performance for deploying static resources. The Vsftp component in Linux can realize file transfer. Nginx+Vsftp can be combined to build a high-performance file server to realize the function of project file upload.
3.1 Install vsftp
1) Install vsftp component
yum -y install vsftpd
2) Add ftp user
useradd ftpuser
The default path after login is /home/ftpuser.
3) Add password to ftp user
passwd ftpuser
4) Modify the configuration
The configuration file is in /etc/vsftpd/vsftpd.conf
vi /etc/vsftpd/vsftpd.conf
Change setting
Turn off anonymous access
Add port range
5) Restart vsftp
service vsftpd restart
6) Set boot up
chkconfig vsftpd on
7) Modify user permissions
chown ftpuser /home/ftpuser
chmod 777 -R /home/ftpuser
3.2 Nginx configuration Vsftp
1) Create a new directory for saving pictures
cd /home/ftpuser/
mkdir www
cd www
mkdir images
cd images
pwd
/home/ftpuser/www/images
2) Configure Nginx
The configuration file of nginx is nginx/conf/nginx.conf
Add location to server
location /images {
root /home/ftpuser/www/;
autoindex on;
}
3) Restart nginx
./nginx -s reload
Enter http://server address/images in the browser, you can see the list of files in the upload directory
4. Nginx load balancing
4.1 Load Balancing Concept
Each server has an upper limit on the number of requests that can be processed. If this upper limit is exceeded, it may hang. Can deploying a few more servers solve this problem?
Not necessarily. Assuming that all requests are made to one server, they will still hang.
Load balancing is a computer technology that can distribute user requests to each server in the cluster according to a certain load balancing algorithm, so as to avoid overloading a single server and improve the processing capacity of the server cluster. Response speed, to achieve the optimal configuration of server resources.
Load balancing can be achieved through client, server and hardware. Nginx is a server-side load balancing technology.
4.2 Load balancing configuration
1) Server settings
For the convenience of testing, you can start several SpringBoot projects on one computer and simulate multiple servers by port distinction
RestController
public class HelloController
{
@GetMapping("/hello")
public String hello(){
return "Hello 8081";
}
}
2) Modify Nginx configuration
Add upstream in http, configure the server list, myservers is the name of the list, and each server inside is the IP and port of the server
upstream myservers {
server 192.168.0.113:8081;
server 192.168.0.113:8082;
server 192.168.0.113:8083;
}
Add server, configure the agent to send requests to myservers for processing
server {
listen 8088;
server_name localhost;
charset utf-8;
location / {
proxy_pass http://myservers;
}
}
3) Restart Nginx
./nginx -s reload
4) Test
When the browser tested the port 8088 to access the hello interface, it was found that the interface call was polling.
The processing power of each server is different, and the weight can be configured so that the capable server can handle more requests
upstream myservers {
server 192.168.0.113:8081 weight=1;
server 192.168.0.113:8082 weight=2;
server 192.168.0.113:8083 weight=1;
}
End
The above is the basic use of Nginx. If you find it useful, just like it. If you have any questions, you can also communicate together.
If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary