1. Overview of Nginx
1. What is Lginx
Lginx is a high-performance, lightweight Web service software
advantage:
- High stability
- Low system resource consumption
- High processing power for HTTP concurrent (access me at the same time) connections
- A single physical server can support 30 000 - 50 000 concurrent requests
2. How Nginx works
- Nginx consists of a kernel and modules
- Nginx itself does very little work. When it receives an HTTP request, it just maps the request to a location block by looking up the configuration file, and the instructions configured in this location will start different modules to Complete the work, so the module can be seen as a real labor worker in Nginx
3.Nginx modules are functionally divided into the following three categories
- handler (processor) module: responsible for processing requests and completing the generation of response content
- filter (filter) module: the filter module is to process the response content
- Proxies (proxy class) module: This type of module is a module like Nginx's HTTP Upstream
These modules mainly interact with some back-end services such as FastCGI (dynamic web page request), etc., to realize functions such as service proxy and load balancing
4.Nginx modules are structurally divided into core modules, basic modules and third-party modules
- Core modules: HTTP (web page) module, EVENT (event) module and MAIL (mail) module;
- Basic modules: HTTP Access (access) module, HTTP FastCGI (dynamic web page request) module, HTTP Proxy (proxy) module and HTTP Rewrite (rewrite) module
- Third-party modules: HTTP Upstream Request Hash (dynamic pool) module, Notice (attention) module and HTTP Access Key module;
Second, build Nginx virtual Web host
1. Build Nginx service
1.1 Prepare the installation package and transfer the XShell file to the root directory
1.2 Turn off the firewall to prevent self-starting
systemctl stop firewalld setenforce 0 systemctl disable firewalld [root@server1 ~]# sed -i '/SELINUX/s/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config [root@server1 ~]# grep -v "#" /etc/selinux/config
1.3 Install dependency packages
[root@server1 ~]# yum -y install \ gcc \ gcc-c++ \ make \ pcre-devel \ #is a regular expression in perl language zlib-devel \ #The development kit of the software package, a function library, the library includes resources such as header files, static libraries and even source code
1.4 Create a non-login user
[root@server1 ~]# useradd -M -s /sbin/nologin nginx
1.5 Unzip the nginx package
[root@server1 ~]# tar zxvf nginx-1.12.2.tar.gz
1.6 Configure nginx
[root@server1 ~]# cd nginx-1.12.2/ [root@server1 nginx-1.12.2]# ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ #Specify the account and group under which to run --group=nginx \ --with-http_stub_status_module #enable the state module
1.7 Compile and install
make && make install
1.8 Establish soft links and check syntax
[root@server1 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #soft link [root@server1 nginx-1.12.2]# nginx -t #Check grammar [root@server1 nginx-1.12.2]# nginx #restart the service [root@server1 nginx-1.12.2]# netstat -anpt | grep nginx #View service port status [root@server1 nginx-1.12.2]# killall -1 nginx #Safe reboot [root@server1 nginx-1.12.2]# killall -3 nginx #Out of service
To do this, the Nginx environment has been deployed
2. Manage Nginx services
1. Make a management script
vi /etc/init.d/nginx #!/bin/bash #chkconfig:35 76 21 #description:Nginx Service Control PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0
2. Authorize the script file and set chkconfig to manage the nginx service
[root@server1 ~]# chmod +x /etc/init.d/nginx [root@server1 ~]# chkconfig --add nginx [root@server1 ~]# chkconfig --list
3. Configure the log level
[root@server1 ~]# ln -s /usr/local/nginx/conf/nginx.conf /etc/ #soft link [root@server1 ~]# vi /etc/nginx.conf [root@server1 ~]# nginx -t [root@server1 ~]# ulimit -n [root@server1 ~]# ulimit -n 65535 >> /etc/rc.local [root@server1 ~]# ulimit -n
When the number of resources is not enough, you need to add the number of resources
vi /etc/nginx.conf Modify the configuration file
4. Configure the statistics function
vi /etc/nginx.conf systemctl stop nginx systemctl start nginx Add to: location ~ /status { stub_status on; access_log off; }
5. View nginx status (status) details
Access to this machine 20.0.0.14/status
3. Configure the authentication function of nginx
[root@server2 ~]# vi /usr/local/nginx/conf/nginx.conf [root@server2 ~]# nginx -t [root@server2 ~]# systemctl stop nginx [root@server2 ~]# systemctl start nginx Add to: location / { deny 20.0.0.1/32; #20.0.0.1 access denied allow all; #allow all access auth_basic "secret"; #ciphertext authentication auth_basic_user_file /usr/local/nginx/passwd.db; #User file points to usr/local/nginx/passwd.db }
test:
Because the execution order of nginx is from top to bottom, unlike apache, put the denied one in front and directly execute the denied access
Third, configure the virtual host function
1. Domain Based
[root@server2 ~]# vi /etc/nginx.conf [root@server2 ~]# systemctl stop nginx [root@server2 ~]# systemctl start nginx Add to: server { listen 80; server_name www.nb.com; charset utf-8; access_log logs/nb.com.access.log; location / { root /var/www/html/nb; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
write page
[root@server1 ~]# mkdir -p /var/www/fa [root@server1 ~]# mkdir -p /var/www/nb [root@server1 ~]# echo "<h1>This is fa.</h1>" > /var/www/fa/index.html [root@server1 ~]# echo "<h1>This is nb.</h1>" > /var/www/nb/index.html [root@server1 ~]# vi /etc/hosts [root@server1 ~]# curl http://www.fa.com <h1>This is fa.</h1> [root@server1 ~]# curl http://www.nb.com <h1>This is nb.</h1>
test domain name access
www.fa.com
www.nb.com
2. IP based
1. Create a virtual host
[root@server1 ~]# ifconfig ens33:1 192.168.1.100/24 [root@server1 ~]# ifconfig
[root@server1 ~]# vi /etc/nginx.conf [root@server1 ~]# systemctl stop nginx [root@server1 ~]# systemctl start nginx [root@server1 ~]# curl http://20.0.0.13 <h1>This is fa.</h1> [root@server1 ~]# curl http://192.168.1.100 <h1>This is nb.</h1>
test:
20.0.0.13
192.168.1.100
3. Port based
[root@server1 ~]# vi /etc/nginx.conf [root@server1 ~]# systemctl stop nginx [root@server1 ~]# systemctl start nginx [root@server1 ~]# netstat -anpt | grep nginx [root@server1 ~]# curl http://20.0.0.13:80 <h1>This is fa.</h1> [root@server1 ~]# curl http://192.168.1.100:8080 <h1>This is nb.</h1>
test:
20.0.0.13:80
192.168.1.100:8080