Getting started with nginx

~wangweijun 2020-11-13 07:21:42
getting started nginx

brief introduction

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 .

Relevant concepts

In order to have a deeper understanding of nginx, Here are some very important concepts :

  1. Reverse proxy
  2. Load balancing
  3. Dynamic and static separation

Reverse proxy

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 .

Load balancing

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 .

Dynamic and static separation

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 .

Installation tutorial

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:
 Insert picture description here
Just download a version of it , Here we use nginx-1.19.0 Version as an example .

Then click here to start downloading :
 Insert picture description here
Put it aside when the download is complete , Let's download nginx Required dependencies .

Execute the following command to download pcre:


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 :


Some students may encounter this problem during the compilation check :
 Insert picture description here
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 tar.gz

Download it and unzip it :

tar -xvf nginx-1.19.1 tar.gz

Then go to the unzip Directory , Execution instruction :


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.
-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

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 And it started , If we encounter this problem :

Nginx: error while loading shared libraries:

Just execute this command :

ln -s /usr/local/lib/ /lib64/

After successful startup , So let's see linux Environmental ip Address :
 Insert picture description here
The browser opens , Enter... In the address field, Successful visit .
 Insert picture description here

nginx Common commands

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
  • start-up nginx
  • close nginx
  • Reload nginx

View version

./nginx -v

start-up nginx


close nginx

./nginx -s stop

Reload nginx

./nginx -s reload

nginx Configuration to realize

Now let's implement it concretely nginx Configuration of , It is divided into :

  1. Reverse proxy
  2. Load balancing
  3. Dynamic and static separation

Reverse proxy

Before implementation , Make a request first : Open the browser , Enter in the address field, Jump to tomcat The main page .

Let's first analyze the running process , We type... In the browser address bar 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 :
 Insert picture description here
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 :
 Insert picture description here
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, Successful visit .
 Insert picture description here

Load balancing

Let's specifically implement the effect of load balancing , Make a request first , Type in the browser address bar, Distribute the request to 8080 and 8081 Port .

Make preparations first , Place two tomcat Used to simulate two servers :
 Insert picture description here
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 :
 Insert picture description here
 Insert picture description here
And then start two tomcat, So the preparation is done , The following is about nginx Load balancing configuration :
 Insert picture description here
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 :
 Insert picture description here
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 .

Dynamic and static separation

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 .
 Insert picture description here
The next thing is right nginx To configure :
 Insert picture description here
After the configuration is completed, restart it again nginx The server , Finally, see the running effect :
 Insert picture description here
 Insert picture description here


  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple