High performance web server -- nginx

1, Introduction

1. What is nginx?

l is a high-performance http server, reverse proxy server and e-mail (IMAP/POP3) proxy server developed in c language.

l is Igor Sysoev, a Russian programmer, and rambler, the second most visited in Russia Ru site development.

2. What are the advantages of nginx?

l lightweight

l when dealing with high concurrency, it can maintain low resources, low consumption and high performance

l highly modular design and simple configuration

l the official test shows that nginx can support 50000 concurrency, and the consumption of cpu, memory and other resources is very low, and the operation is very stable

3. Application scenario of nginx?

l http server. Nginx can provide http services independently and can be used as a static web server

l virtual host. Multiple websites can be virtualized on one server

l reverse proxy, load balancing

4. Nginx version download

l http://nginx.org/en/download.html

2, nginx installation

1. Installation Preparation

Type Yum - y install gcc-c + + PCRE PCRE devel zlib zlib devel OpenSSL OpenSSL devel

2. Nginx installation

  • decompression
[root@node01 local]# tar -zxvf nginx-1.10.3.tar.gz
  • Enter the decompression directory
[root@node01 local]# cd nginx-1.10.3/
  • Copy the following paragraph, execute configure, and generate a Makefile
./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/nginx/client \

--http-proxy-temp-path=/var/temp/nginx/proxy \

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \

  • compile
[root@node01 nginx-1.10.3]# make
  • install
[root@node01 nginx-1.10.3]# make install
  • create link
[root@node01 sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
  • Verify whether there is a problem with the configuration
[root@node01 conf]# nginx -t

  • If there is an error in a few folders, create it yourself
[root@node01 conf]# nginx

[root@node01 conf]# mkdir /var/temp/nginx

3. Nginx startup

  • Check that the firewall is closed
[root@node01 conf]# systemctl status firewalld.service

  • Start and view the process
[root@node01 conf]# nginx

[root@node01 conf]# ps aux|grep nginx
  • Browser access

4. Nginx stop

  • Close and view processes
[root@node01 conf]# nginx -s stop

[root@node01 conf]# ps aux|grep nginx

5. Nginx refresh configuration

  • After startup, if the configuration is modified, you can refresh as follows without restarting nginx
[root@node01 conf]# nginx

[root@node01 conf]# nginx -s reload

3, Detailed explanation of nginx

1. Nginx installation directory

[root@localhost html]# cd /usr/local/nginx

[root@localhost nginx]# ll

conf: configuration file of nginx

HTML: the default access root directory of nginx, which stores static resources

sbin: store nginx running script

2. Nginx configuration file

  • worker_processes: number of work processes. According to the hardware adjustment, it is usually equal to the number of CPUs or twice the number of CPUs.

  • worker_connections: the maximum number of connections per worker process. According to the hardware adjustment, it can be used together with the previous work process. Try to be as large as possible, but don't run the cpu to 100%.

  • include: set the supported file types. The details are in mime In types

  • Sendfile: the sendfile instruction specifies whether nginx calls the sendfile function (zero copy mode) to output the file. For ordinary applications, it must be set to on. If it is used for downloading and other application disk IO heavy load applications, it can be set to off to balance the processing speed of disk and network IO and reduce the system uptime.

  • keepalive_timeout: keepalive timeout.

  • Server: a server is a virtual machine

  • listen: the port number of the server virtual machine

  • server_name: host name of server virtual machine

  • location: resource accessed by default

  • root html: the root directory of the virtual host

4, nginx basic usage

1. Web site

The following directory stores things that can be accessed directly from the outside

2. Multiple virtual hosts

Save money and trouble

  • Modify configuration directly
[root@node01 conf]# vi nginx.conf
  • Add another server under the original server
	listen 85;
	server_name _;
	location / {
		root	py;
		index	index.html index.htm;
  • Copy an html folder called py, corresponding to the configuration
[root@node01 nginx]# cp -r html/ py
  • Modify index HTML, so that you can distinguish

  • Refresh configuration
[root@node01 py]# nginx -s reload 

3. 404 page setup

  • In the configuration, you can open the following comments and create your own error page

  • Create a corresponding 404 html

  • Refresh configuration

4. Nginx reverse proxy

  • As shown below, when accessing port 80, it will actually be forwarded to port 85

  • Refresh configuration

  • Visit 80 and jump to 85

5. Nginx load balancing

  • Modify profile
[root@node01 conf]# vi nginx.conf

=Add a load balancing pool and 80 port reverse proxy to the load balancing pool

  • There is an additional server with port 95 below

  • Copy py, create a p2, and modify the index HTML, just distinguish

  • Refresh configuration

  • Page access. At this time, the reverse proxy is realized to 85 and 90, which is also accessed according to the weight

Tags: Linux

Posted by guru2k9 on Tue, 24 May 2022 14:37:15 +0300