In this article, let's learn about nginx, So the first question before us is : What is the nginx?nginx What can be done ?
nginx Is a high-performance HTTP And reverse proxy web The server , It also provides IMAP/POP3/SMTP service , The feature is less memory , Strong concurrency .nginx It can be used as a static page web The server , It also supports CGI Dynamic language of protocol , Such as :perl、php etc. , But does not support java,java Programs can only be done with tomcat Cooperate to complete .nginx Developed for performance optimization , Performance is the most important consideration , Focus on efficiency , Able to withstand the test of high load .
In order to have a deeper understanding of nginx, Here are some very important concepts :
Before you know about reverse proxy , We can take a look at what a positive agent is .
If the LAN outside of Internet Think of it as a huge resource bank , Users in the LAN want to access Internet, It needs to be accessed through a proxy server , This kind of proxy service is called forward proxy .
So what is reverse proxy ?
In reverse proxy , In fact, the client is insensitive to the agent , Because the client does not need any configuration to be able to access , We just need to send the request to the reverse proxy , The reverse proxy server selects the target server to obtain the data , And back to the client , At this time, the reverse proxy server and the target server are external servers , What's exposed is the proxy address , Hiding the real server ip Address .
Client sends multiple requests to server , Server processing request , There are some possibilities to interact with the database , After the server finishes processing , Then return the result to the client .
This architecture pattern is relatively single for early systems 、 It is more suitable when there are relatively few concurrent requests , Low cost , But as the data grows , At present, the server has been unable to cope with high concurrency , What to do ?
The easiest way, of course, is to upgrade the server configuration , But it's too expensive , If the server configuration has reached its peak , But still can't resist huge concurrent requests , How to solve this problem at this time ?
Looking back on the reverse agent , Requests sent by the client will go through the reverse proxy server , The reverse proxy server will choose the target server , Let's say there's now 30 The request , We have three servers , that Load balancing
The thing to do is to put this 30 Requests are evenly distributed among the three servers , Make each server process requests close to 10 strip .
In order to speed up the analysis of the website , Dynamic pages and static pages are usually parsed by different servers , To speed up the resolution , Reduce the pressure of the original single server .
We're done with the concept , How to install nginx 了 , Here we use centos6 Environment, for example .
First come to the official website to download nginx:http://nginx.org/
Just download a version of it , Here we use nginx-1.19.0 Version as an example .
Then click here to start downloading :
Put it aside when the download is complete , Let's download nginx Required dependencies .
Execute the following command to download pcre:
wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
After downloading, unzip it , Execution instruction :
tar -xvf pcre-8.37.tar.gz
After decompression, you need to enter the directory :
cd pcre-8.37.tar.gz
Then execute instructions to compile and check it :
./configure
Some students may encounter this problem during the compilation check :
The problem arises from the lack of gcc compiler , Just install it , Execute the following instructions to install :
yum install gcc-c++
Finally, execute the installation instructions :
make && make install
Come here pcre Just install it , Then install it zlib, Execution instruction :
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
All dependencies are installed , You can start the installation nginx 了 , Remember we downloaded the good one earlier nginx Compressed package , Put the compressed package into linux Environment , Or you can download it directly through instructions :
wget https://nginx.org/download/nginx-1.19.1 tar.gz
Download it and unzip it :
tar -xvf nginx-1.19.1 tar.gz
Then go to the unzip Directory , Execution instruction :
./configure
Finally, execute the installation instructions :
make && make install
In the official launch nginx Before , We also need to set up a firewall , Execution instruction :
vi /etc/sysconfig/iptables
Change the file to read as follows :
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
-A INPUT -j REJECT –reject-with icmp-host-prohibited
-A FORWARD -j REJECT –reject-with icmp-host-prohibited
COMMIT
nginx The port is 80, So we let 80 Open the port , Then restart the firewall :
service iptables restart
Now we come to nginx In the catalog sbin:
cd /usr/local/nginx/sbin/
Execution instruction :
./nginx
nginx And it started , If we encounter this problem :
Nginx: error while loading shared libraries: libpcre.so.
Just execute this command :
ln -s /usr/local/lib/libpcre.so.1 /lib64/
After successful startup , So let's see linux Environmental ip Address :
The browser opens , Enter... In the address field 192.168.124.7
, Successful visit .
Use nginx The premise of the order is that we are currently in nginx Directory , namely :/usr/local/nginx/sbin
, Here are some of the most commonly used nginx command :
View version
./nginx -v
start-up nginx
./nginx
close nginx
./nginx -s stop
Reload nginx
./nginx -s reload
Now let's implement it concretely nginx Configuration of , It is divided into :
Before implementation , Make a request first : Open the browser , Enter in the address field www.test.com
, Jump to tomcat The main page .
Let's first analyze the running process , We type... In the browser address bar www.test.com
after , This request needs to be submitted to nginx The server , Again by nginx Servers turn to tomcat The server , Because it involves a domain name , So it needs to be in windows Under the system hosts File to configure .
First of all, hosts File , Come to the directory C:\Windows\System32\drivers\etc
find hosts file , And make the following changes :
The front is ip Address , After that is the domain name , Let's make a mapping between them .
And then there's the nginx Request forwarding configuration in , Came to /usr/local/nginx/conf
Under the table of contents , Yes nginx.conf File modification :
Use after modification ./nginx
Command to restart nginx, Do remember to be in nginx Of sbin Directory to execute the command .
Finally, let's test , Type in the browser address bar www.test.com
, Successful visit .
Let's specifically implement the effect of load balancing , Make a request first , Type in the browser address bar http://192.168.124.7/test.html
, Distribute the request to 8080 and 8081 Port .
Make preparations first , Place two tomcat Used to simulate two servers :
Also on tomcat8081 In the catalog tomcat To configure , Configure it to 8081 port , Finally, in two tomcat Of webapps Put one in the directory html file :
And then start two tomcat, So the preparation is done , The following is about nginx Load balancing configuration :
Restart after the configuration is complete nginx The server .
At this point, the load balancing operation is completed , But how does it work ? Take a look at the demo below :
because nginx The server is listening to 80 port , So we can omit the port number and go directly through ip visit , You will find that when you constantly refresh the page for requests , The page sometimes shows 8080, Sometimes it shows 8081, This shows that load balancing has been successfully implemented ,nginx This happens only when the server allocates the request to two servers for processing each time .
The separation of static and dynamic is to give all static requests to nginx Handle , Pass all dynamic requests through nginx Give it back tomcat Handle .
Do the preparatory work first , stay linux Created in the root directory data Folder , And create... Under this folder html and image Folder , One of them is test.html Document and test.png file .
The next thing is right nginx To configure :
After the configuration is completed, restart it again nginx The server , Finally, see the running effect :