Nginx
is a high-performance HTTP and reverse proxy service. It is a lightweight web server, reverse proxy server and email proxy server, features: less memory, strong concurrency,
epoll:
The enhanced version of select and poll proposed in the Linux 2.6 kernel
support horizontal trigger LT and edge trigger ET, The biggest feature is edge triggering, it only tells the process which fd has just become in demand state, and will only notify once
using the “event” ready notification method, register fd through epoll_ctl, once the fd is ready, the kernel will Use a callback mechanism similar to callback to activate the fd, and epoll_wait can receive notifications. Advantages:
There is no limit to the maximum concurrent connection: the upper limit of the FD that can be opened is much greater than 1024 (1G of memory can monitor About 100,000 ports), check
see /proc/sys/fs/file-max for details. This value is related to the system memory size.
Efficiency improvement: non-polling method will not increase with the number of FDs Increase and decrease efficiency; only active available FD will call the callback function, that is, the biggest advantage of epoll is that it only manages “active” connections, and has nothing to do with the total number of connections.
Memory copy, use mmap (Memory Mapping) speed up the message transfer with the kernel space; that is, epoll uses mma pReduce copy overhead
Basic features:
Features:
Modular design, 1, better scalability 2, high reliability, 3, support for hot deployment: update configuration files without stopping the machine, Upgraded version, change log files
4. Low memory consumption: 10,000 inactive connections in keep-alive connection mode, only 2.5M memory required
Basic functions:
1, static resource web server 2 , Http protocol reverse proxy server 3, pop3/imap4 protocol reverse proxy server 4, FastCGI (LNMP), uWSGI (python) and other protocols 5, modular (non-DSO), such as zip, SSL module
Nginx is more Process organization model, and it is a master process and worker process group
Nginx installation:
The first is that the version of yum is relatively old, and the other is that it is more convenient to customize the related path by compiling and installing. The third is to use source code compilation to customize related functions, which is more convenient for business use. Source code installation needs to prepare a standard compiler in advance. The full name of GCC is (GNU Compiler collection), which is developed by GNU and licensed under GPL or LGPL. , Is a free UNIX-like standard compiler of Apple’s Mac OS X operating system. Because GCC can only handle the C language, it was originally called the GNU C language compiler. Later it has been developed rapidly and can handle C++, Fortran, pascal, Objective-C, Java, Ada and other languages, in addition to Automake tool, to complete the work of automatically creating Makefile, some modules of Nginx need to rely on third-party libraries, such as pcre (support rewrite), zlib (support gzip module) and openssl (Support ssl module) and so on.
Use the installed binary file nginx:
[[emailprotected] ~]# nginx -h
nginx version: nginx/1.12.2
Usage: nginx [-?hvVtTq ] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h: this help
-v: show version and exit
-V: show version and configure options then exit #Display version and compile parameters
-t: test configuration and exit #Test whether the configuration file is abnormal
-T: test configuration, dump it and exit #Test and print< br>-q: suppress non-error messages during configuration testing #Silent mode
-s signal: send signal to a master process: stop, quit, reopen, reload #Send signal
-p prefix: set prefix path (default: /usr/share/nginx/) #Specify Nginx directory
-c filename: set configuration file (default: /etc/nginx/nginx.conf) #Configuration file path
-g directives: set global directives out of configuration file #Set global directives
Nginx startup script:
yum installation: official website: https://nginx.org/packages/centos/7/x86_64/RPMS/
wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
yum install nginx-1.14.2-1.el7_4. ngx.x86_6 4.rpm
Default startup script:
[[emailprotected] ~]#vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx-high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/ bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
Default configuration file:/ etc/nginx/nginx.conf
[[email protected] ~]#grep -v “#” /etc/nginx/nginx.conf | grep -v “^$”
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/.conf;
events {
worker_connections 1024;
}
http {
log_format main’$remote_addr-$remote_user [$time_local] “$request”‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_f or”‘;
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
servername ;
root /usr/share/nginx/html;
include /etc/ nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
systemctl start nginx to log in.
Compile and install:
150: yum install, 200: compile and install
Official website: https://nginx.org/en/download.html
[[emailprotected] src ]cd /usr/local/src source code packages are generally stored here
[[emailprotected] src]#wget https://nginx.org/download/nginx-1.14.2.tar.gz
[ [emailprotected] src]#tar xvf nginx-1.14.2.tar.gz
[[emailprotected] src]#cd nginx-1.14.2/
[[emailprotected] nginx-1.14.2] #yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate
gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel
net-tools iotop bc zip unzip zlib-devel bash -completion nfs-utils automake libxml2
libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
[[emailprotected] nginx-1.14.2]#ll
total 732
drwxr-xr-x. 6 1001 1001 4096 Mar 15 11:07 auto
-rw-r–r–. 1 1001 1001 288742 Dec 4 22:52 CHANGES
-rw-r–r –. 1 1001 1001 440121 Dec 4 22:52 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 Mar 15 11:07 conf
-rwxr-xr-x. 1 1001 1001 2502 Dec 4 22:52 configure
drwxr-xr-x. 4 1001 1001 72 Mar 15 11:07 contrib
drwxr-xr-x. 2 1001 1001 40 Mar 15 11:07 html
-rw-r–r–. 1 1001 1001 1397 Dec 4 22:52 LICENSE< br>drwxr-xr-x. 2 1001 1001 21 Mar 15 11:07 man
-rw-r–r–. 1 1001 1001 49 Dec 4 22:52 README
drwxr-xr-x. 9 1001 1001 91 Mar 15 11:07 src
[[emailprotected] nginx-1.14.2]#./configure –prefix=/apps/nginx
[[emailprotected] nginx- 1.14.2]#make / make install
[[emailprotected] nginx-1.14.2]#ll /apps/
total 0
drwxr-xr-x. 6 root root 54 Mar 15 11:20 nginx
[[email protected] nginx-1.14.2]#ll /apps/nginx/
total 4
drwxr-xr-x. 2 root root 4096 Mar 15 11:20 conf stores configuration files
drwxr-xr-x. 2 root root 40 Mar 15 11:20 html directory where static pages are stored
drwxr-xr-x. 2 root root 6 Mar 15 11:20 logs stores logs< br>drwxr-xr-x. 2 root root 19 Mar 15 11:20 sbin stores executable programs
[[emailprotected] nginx-1.14.2]#./configure –with-http_ssl_module- -with-http_v2_module –with-http_realip_module –with-http_stub_status_module –with-http_gzip_static_module –with-pcre –with-str eam –with-stream_ssl_module –with-stream_realip_module
[[emailprotected] nginx-1.14.2]#/apps/nginx/sbin/nginx -V
nginx version: nginx/1.14. 2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: –prefix=/apps/nginx –with-http_ssl_module –with-http_v2_module –with-http_realip_module –with-http_stub_status_module –with-http_gzip_static_module –with-pcre –with-stream –with-stream_ssl_module – with-stream_realip_module
[[emailprotected] nginx-1.14.2]#/apps/nginx/sbin/nginx
[[emailprotected] nginx-1.14.2]#/apps/nginx/sbin/nginx- s stop
Now on 150 host:
scp /usr/lib/systemd/system/nginx.service 172.18.9.200:/usr/lib/systemd/system/nginx.service
200 host:< br>[[email protected] nginx-1.14.2]#vim /usr/lib/systemd/system/nginx.service
[Service]
#PIDFile=/var/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf change the configuration file path
[[email pro tected] nginx-1.14.2]#systemctl daemon-reload
[[emailprotected] nginx-1.14.2]#systemctl start nginx
[[emailprotected] nginx-1.14.2]# vim /apps/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2; 2 processes
worker_cpu_affinity 0001 0010;
[[emailprotected] nginx-1.14.2]#/ apps/nginx/sbin/nginx -s reload
[[emailprotected] nginx-1.14.2]#ps -ef |grep nginx
root 34192 1 0 16:33? 00:00:00 nginx: master process /apps/nginx/sbin/nginx
nginx 34321 34192 0 16:43? 00:00:00 nginx: worker process
nginx 34322 34192 0 16:43? 00:00:00 nginx: worker process< br>root 34324 20781 0 16:43 pts/0 00:00:00 grep –color=auto nginx
[[emailprotected] nginx-1.14.2]#cat /apps/nginx/html/ index.html
172.18.9.200
At this time, Nginx is configured and ready to use.
Afternoon Session 1:
Nginx default configuration file:
[[emailprotected] nginx]#grep -v “#” /apps/nginx/conf/nginx.conf | grep -v “^$” Filter out blank lines etc.
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 0001 0010;
pid logs/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[[emailprotected] nginx]#cd / apps/nginx/
[[emailprotected] nginx]#ll
total 4
drwx——. 2 nginx root 6 Mar 15 13:49 client_body_temp
drwxr-xr-x . 2 root root 4096 Mar 15 17:28 conf
drwx——. 2 nginx root 6 Mar 15 13:49 fastcgi_temp
drwxr-xr-x. 2 root root 40 Mar 15 16:50 html
drwxr-xr-x. 2 root root 58 Mar 15 17:13 logs
drwx——. 2 nginx root 6 Ma r 15 13:49 proxy_temp
drwxr-xr-x. 2 root root 36 Mar 15 15:40 sbin
drwx——. 2 nginx root 6 Mar 15 13:49 scgi_temp
drwx ——. 2 nginx root 6 Mar 15 13:49 uwsgi_temp
[[emailprotected] nginx]#cd conf
[[emailprotected] conf]#ll
total 68
-rw-r–r–. 1 root root 1077 Mar 15 13:47 fastcgi.conf
-rw-r–r–. 1 root root 1077 Mar 15 15:40 fastcgi.conf.default< br>-rw-r–r–. 1 root root 1007 Mar 15 13:47 fastcgi_params
-rw-r–r–. 1 root root 1007 Mar 15 15:40 fastcgi_params.default
-rw-r–r–. 1 root root 2837 Mar 15 15:40 koi-utf
-rw-r–r–. 1 root root 2223 Mar 15 15:40 koi-win
-rw-r–r–. 1 root root 5170 Mar 15 13:47 mime.types
-rw-r–r–. 1 root root 5170 Mar 15 15:40 mime.types.default< br>-rw-r–r–. 1 root root 2705 Mar 15 17:28 nginx.conf
-rw-r–r–. 1 root root 2656 Mar 15 15:40 nginx.conf. default
-rw-r–r–. 1 root root 636 Mar 15 13:47 scgi_params
-rw-r–r–. 1 root root 636 Mar 15 15:40 scgi_params.default< br>-rw-r–r–. 1 root roo t 664 Mar 15 13:47 uwsgi_params
-rw-r–r–. 1 root root 664 Mar 15 15:40 uwsgi_params.default
-rw-r–r–. 1 root root 3610 Mar 15 15:40 win-utf
[[email protected] conf]#vim mime.types
types {
text/html html htm shtml; When accessing the Internet, the network will parse according to these suffixes .
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom ;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;< br />text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
application/font-woff woff;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
[[emailprotected] ~]# vim /apps/nginx/conf/nginx.conf
http {
server {
listen 172.18.9.200:80;
listen 8080;
}
[[emailprotected] ~ ]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[[emailprotected] ~]#/apps/nginx/sbin/nginx -s reload
[[emailprotected] ~]#/apps/nginx/sbin/nginx -s stop Later, 8080 will appear.
[[email Protected] ~]#/apps/nginx/sbin/nginx
[[email Protected] ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address: Port
LISTEN 0 128 :111 :
LISTEN 0 128 :8080 :
LISTEN 0 128 172.18.9.200: 80
3.1: Global configuration:
user nginx nginx; #Start the Nginx worker process user and group worker_processes [number | auto]; #Start the number of Nginx worker processes worker_cpu_affinity 00000001 00000010 00000100 00001000; #The Nginx worker process Bind to the specified CPU core. By default, Nginx does not perform process binding. Binding does not mean that the current nginx process monopolizes one core CPU, but it can guarantee that this process will not run on other cores, which is great It reduces the back and forth jumps of nginx work processes on different cpu cores, reduces the resource allocation and recycling of the process and memory management by the CPU, so it can effectively improve the performance of the nginx server. [[email protected] ~]# ps axo pid,cmd,psr | grep nginx20061 nginx: master process /apps 020062 nginx: worker process 020063 nginx: worker process 120097 grep –color=auto nginx 0123456
The second question: Location matching the access path to access different pages to display different content
1. [[emailprotected] conf]#ll /apps/nginx/
total 4
drwx—— . 2 nginx root 6 Mar 15 13:49 client_body_temp
drwxr-xr-x. 2 root root 4096 Mar 15 17:56 conf
drwx——. 2 nginx root 6 Mar 15 13:49 fastcgi_temp
drwxr-xr-x. 2 root root 40 Mar 15 16:50 html
drwxr-xr-x. 2 root root 58 Mar 15 17:45 logs
drwx——. 2 nginx root 6 Mar 15 13:49 proxy_temp
drwxr-xr-x. 2 root root 36 Mar 15 15:40 sbin
drwx——. 2 nginx root 6 Mar 15 13:49 scgi_temp
drwx——. 2 nginx root 6 Mar 15 13:49 uwsgi_temp
[[emailprotected] conf]#ll /apps/nginx/html Store files for accessing pages
total 8< br>-rw-r–r–. 1 root root 537 Mar 15 13:47 50x.html
-rw-r–r–. 1 root root 13 Mar 15 13:57 index.html< /p>
2. The location part of the configuration file:
[[email protected] ~]#vim /apps/nginx/con f/nginx.conf
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
#redirect server error pages to the static page /50x.html
#error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#proxy the PHP scripts to Apache listening on 127.0.0.1:80
#location ~ \.php$ {
#proxy_pass http://127.0 .0.1;
#}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#location ~ \.php$ {
#root html;
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#include fastcgi_params;
#}
[[email protected] ~]#vim /apps/nginx/conf/nginx.conf Add 2 items to events in this file and set them to on
events {
worker_connections 1024;
use epoll;
multi_accept on;
accept_mutex on; }
[[email pr otected] ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/ nginx.conf test is successful
[[emailprotected] ~]#/apps/nginx/sbin/nginx -s reload
[[emailprotected] ~]#ps -ef |grep nginx displays as follows:
root 35110 1 0 17:45? 00:00:00 nginx: master process /apps/nginx/sbin/nginx
nginx 35111 35110 0 17:45? 00:00:00 nginx: worker process
nginx 35112 35110 0 17:45? 00:00:00 nginx: worker process
root 35596 24183 0 18:20 pts/1 00:00:00 grep –color=auto nginx
Three, Add “172.18.9.200 www.magedu.net” to the hosts of the c drive
[[emailprotected] ~]#vim /apps/nginx/conf/nginx.conf
Add “include /apps/nginx/ conf.d/*.conf;” added to the HTTP document at the end of the article
[[emailprotected] ~]#mkdir /apps/nginx/conf/conf.d
[[emailprotected] ~]#cd / apps/nginx/conf/conf.d
[[emailprotected] conf.d]#ll
total 0
[[emailprotected] conf.d]#vim pc.conf First create one, You can use it first.
server {
listen 80;
server_name www.magedu.net;
location / {
root /data/nginx/html/pc;
}
#location / about {
#root /data/nginx/html/pc;
#index index.html;
}
}
[[emailprotected] ~]#cd / apps/nginx/conf/conf.d
[[emailprotected] conf.d]#mkdir /data/nginx/html/pc -p
[[emailprotected] conf.d]#vim /data/ nginx/html/pc/index.html
pc web
Go to www.magedu.net
[[emailprotected] conf.d]#cp pc.conf mobile.conf
[[emailprotected] conf. d]#vim mobile.conf
server {
listen 80;
server_name mobile.magedu.net;
location / {
root /data/nginx/html/mobile; }
#location /about {
#root /data/nginx/html/pc;
#index index.html;
#}
}
[[email protected] conf.d] #/apps/nginx/sbin/nginx -t
[[emailprotected] conf.d]#/apps/nginx/sbin/nginx -s reload
[[emailprotected] conf.d]#mkdir / data/nginx/html/mob ile
[[email protected] conf.d]#vim /data/nginx/html/mobile/index.html
mobile web
Now visit mobile.magedu.net
[[email Protected] conf.d]#vim /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.net;
location / {
root /data/nginx/html/pc;
}
location /about {Remove the comment
root /data/nginx/html/pc;
index index.html;
}
}
[[emailprotected] conf.d]#mkdir /data/nginx/html/pc/about
[[emailprotected] conf.d]#vim /data/nginx /html/pc/about/index.html
about page
Four. Nginx location configuration:
Detailed use of location:
= #Before using the standard uri, the request string must match the uri exactly. If the match is successful, stop the downward match and process the request immediately.
~ #case-sensitive
~ #case-insensitive
!~ #case-sensitive does not match
!~ #case-insensitive does not match
^ ~ #What begins with matching
$ #What ends with matching
\ #Escape character. Can be transferred. *? Etc.
- #Represents any character of any length
Example 1:
1, [[emailprotected] images]#mv 7c72236492a311ba7e81b4044a405615.jpg 1.jpg< br>[[emailprotected] images]#ll
total 180
-rw-r–r–. 1 root root 49584 Mar 15 20:23 1.jpg
2,[[emailprotected ] conf.d]#vim /apps/nginx/conf/conf.d/pc.conf
server {
location ~ /1.jpg {
root /data /nginx/html/images;}
}
[[emailprotected] conf.d]# /apps/nginx/sbin/nginx -t
[[emailprotected] conf.d]# /apps /nginx/sbin/nginx -s reload
3、
Example 2: What does the match start with
[[emailprotected] conf.d]#cd /data/nginx/html/pc
[[email protected] pc]#mkdir images images
[[emailprotected] pc]#mkdir images images1
[[emailprotected] pc]#vim images/index.html
images
[[emailprotected] ] pc]#vim images1/index.html
images1
[[emailprotected] conf.d]#vim /apps/nginx/conf/conf.d/pc.conf
server {
location ^~ /images {
root /data/nginx/html/pc;
index index.html;
}
location^~/images1{
root /data/ nginx/html/pc;
index index.html;
}
}