Nginx in brief

What is Nginx?

Nginx (engine x) is a high-performance HTTP and reverse proxy web server, and also provides IMAP/POP3/SMTP services. Nginx was developed by Igor Sesoyev for the second most visited site in Russia, Rambler.ru (Russian: Рамблер). The first public version 0.1.0 was released on October 4, 2004. On June 1, 2011, nginx 1.0.4 was released.

Its characteristics are that it occupies less memory and has strong concurrency capabilities. In fact, nginx's concurrency capabilities perform better in the same type of web server. Users of nginx websites in mainland China include: Baidu, Jingdong, Sina, Netease, Tencent, Taobao, etc. There is a 12.18% usage rate among the global active websites, which is approximately 22.2 million websites.

Nginx is a service with very simple installation, very concise configuration files (also able to support perl syntax), and very few bugs. Nginx is particularly easy to start, and it can almost run 7*24 uninterrupted, even if it runs for several months, it does not need to be restarted. You can also upgrade the software version without interruption of service.

Nginx code is completely written from scratch in C language. Official data tests show that it can support responses up to 50,000 concurrent connections.

What does Nginx do?

Http proxy, reverse proxy: As one of the most commonly used functions of web servers, especially reverse proxy.

Forward proxy There are two types of load balancing strategies provided by the
Insert picture description here
reverse proxy
Insert picture description here
Nginx: built-in strategies and extended strategies. The built-in strategies are polling, weighted polling, and Ip hash. Expansion strategy is just wild, there are only things you can't think of, nothing he can't do.

polling
Insert picture description here

Weighted polling
Insert picture description here

Iphash performs a hash operation on the ip requested by the client, and then distributes the request of the same client ip to the same server for processing according to the hash result, which can solve the problem of session non-sharing.
Insert picture description here

Movement and static separation. In our software development, some requests need to be processed in the background, and some requests do not need to be processed in the background (such as: css, html, jpg, js, etc.), these files do not need to be processed in the background It is called a static file. Let the dynamic web pages in the dynamic website distinguish constant resources from frequently changing resources according to certain rules. After the dynamic and static resources are split, we can cache them according to the characteristics of the static resources. Improve the speed of resource response.
Insert picture description here
By using Nginx, the response speed of the website is greatly improved, the user experience is optimized, and the robustness of the website is improved!

Nginx installation

Install under windows

1. Download nginx
http://nginx.org/en/download.html to download the stable version.
Take nginx/Windows-1.16.1 as an example, and download nginx-1.16.1.zip directly.
After downloading, decompress and decompress as follows:
Insert picture description here
2.
There are many ways to start nginx to start nginx
(1) Double-click nginx.exe directly, a black pop-up window flashes after double-click
(2) Open cmd command window, switch to nginx decompression Under the directory, enter the command nginx.exe and press Enter

3. Check whether nginx is started successfully.
Directly enter the URL http://localhost:80 in the address bar of the browser and press Enter. The following page appears, indicating that the startup is successful!

4.
The configuration file for configuring monitoring nginx is nginx.conf in the conf directory. The default configured nginx monitoring port is 80. If port 80 is occupied, it can be modified to an unoccupied port.
Insert picture description here
When we modify the nginx configuration file nginx.conf, there is no need to close nginx and restart nginx, just execute the command nginx -s reload to make the changes take effect

5. Close nginx.
If you use the cmd command window to start nginx, closing the cmd window will not end the nginx process, you can use two methods to close nginx
(1) Enter the nginx command nginx -s stop (quickly stop nginx) or nginx -s quit( Stop nginx completely and orderly)
(2) Use taskkill taskkill /f /t /im nginx.exe

  • taskkill is used to terminate the process
  • /f is for forced termination
  • /t terminates the specified process and any child processes started thereby
  • /im shows the specified process name

Install under linux

1. To install gcc and
install nginx, you need to compile the source code downloaded from the official website. Compilation depends on the gcc environment. If there is no gcc environment, you need to install:

yum install gcc-c++

2. PCRE pcre-devel install
PCRE (Perl Compatible Regular Expressions) is a Perl library, including Perl compatible regular expression library. The http module of nginx uses pcre to parse regular expressions, so the pcre library needs to be installed on linux. pcre-devel is a secondary development library developed using pcre. Nginx also needs this library. command:

yum install -y pcre pcre-devel

3. The zlib installation
zlib library provides a variety of compression and decompression methods. nginx uses zlib to gzip the content of the http package, so you need to install the zlib library on Centos.

yum install -y zlib zlib-devel

4. OpenSSL installation
OpenSSL is a powerful secure socket layer cryptographic library, including main cryptographic algorithms, commonly used key and certificate packaging management functions and SSL protocols, and provides a wealth of applications for testing or other purposes.
Nginx not only supports the http protocol, but also supports https (that is, HTTP is transmitted over the ssl protocol), so you need to install the OpenSSL library in Centos.

yum install -y openssl openssl-devel

5. Download the installation package
Manually download the .tar.gz installation package, address: https://nginx.org/en/download.html
Insert picture description here
After downloading, upload it to the server/root

6. Unzip

tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

Insert picture description here
7. Configuration
Use the default configuration and execute in the nginx root directory

./configure
make
make install

Find the installation path: whereis nginx
Insert picture description here

Nginx common commands

cd /usr/local/nginx/sbin/
./nginx  启动
./nginx -s stop  停止
./nginx -s quit  安全退出
./nginx -s reload  重新加载配置文件
ps aux|grep nginx  查看nginx进程

Successful startup to access the server ip: 80
Insert picture description here
Note: How to connect to the server , check whether the Alibaba Cloud security group has open ports, or whether the server firewall has open ports!
Related commands:

# 开启
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop
# 查看防火墙规则
firewall-cmd --list-all
# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;

Guess you like

Origin blog.csdn.net/weixin_45879810/article/details/114105720