With the rapid development of computer and Internet technology, various Web sites have become the backbone for users. In various website server software. In addition to Apache, there is a lightweight HTTP server software, Nginx.
Building Web server based on Apache can refer to blog posts: Construction of Web Server Based on Apache
Introduction to Nginx Services
Nginx, developed by lgor Sysoev of Russia, is specially designed for performance optimization. Its most famous advantages are its stability, low system resource consumption and high stand-up capability for HTTP concurrent connections (a single physical server can support 30,000-50,000 concurrent requests). Because of this, a large number of enterprises providing services such as social network, news information, e-commerce and virtual host have chosen Nginx to provide Web services.
If the purpose of building Web services is to parse static web pages, dynamic web pages and so on, without too many functions, then Nginx is absolutely the first choice.
2. Compile and install Nginx service
The latest stable version of Nginx is 1.12.0, which can be accessed through the official website http://nginx.org/or a link to the website: https://pan.baidu.com/s/1H5DHcVWMPGDWFQ-kDsS7XA
Extraction code: 1zyi
Download for use.
1. Compile and install Nginx service
1) Installation of support software
The configuration and operation of Nginx require the support of software packages such as pcre (support regular expression), zlib (support compression), etc. Therefore, the development packages of these software should be installed first in order to provide corresponding functions to ensure the smooth completion of the installation of Nginx:
[root@localhost ~]# yum -y install pcre-devel zlib-devel
2) Create Running Users, Groups
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
3) Compile and install Nginx
[root@localhost ~]# tar zxf nginx-1.12.0.tar.gz -C /usr/src [root@localhost ~]# cd /usr/src/nginx-1.12.0/ [root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx \ --user=nginx --group=nginx --with-http_stub_status_module //Specify the installation directory, running users, and groups of Nginx services //Enable http_stub_status_module module to support status statistics for easy viewing of server connection information [root@localhost nginx-1.12.0]# make && make install
4) Optimizing Path
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
2. Running Control of Nginx Service
1) Check the Nginx service configuration file
Similar to Apache's main program httpd, Nginx uses the "-t" option to check its configuration file syntax. To check configuration files located elsewhere, use the "-c" option to specify the path.
[root@localhost nginx-1.12.0]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
perhaps
[root@localhost nginx-1.12.0]# nginx -c /usr/local/nginx/conf/nginx.conf
2) Start and stop Nginx services
[root@localhost ~]# nginx //Nginx service can be started by running nginx command directly [root@localhost ~]# netstat -anpt | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 46231nginx: master //The default is also TCP protocol port 80. If there are other Web services software, the port should be modified to avoid conflicts. [root@localhost ~]# lynx http://127.0.0.1 //You can use the lynx command for text browser checking (you need to install the lynx package) [root@localhost ~]# killall -s HUP nginx //Reload the Nginx configuration file [root@localhost ~]# killall -s QUIT nginx //Stop the Nginx service
3) Adding Nginx Service to System Service
In order to make the start, stop and overload of Nginx service more convenient, Nginx service script can be written. The script is as follows:
[root@localhost ~]# vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 99 20 PROG="/usr/local/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 [root@localhost ~]# chmod +x /etc/init.d/nginx [root@localhost ~]# chkconfig --add nginx [root@localhost ~]# systemctl start nginx //You can use the system CTL tool to manage Nginx services
3.Nginx Service Profile Details
The main configuration file for the Nginx service is: / usr/local/nginx/conf/nginx.conf.
1) Global configuration
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf #Usenginx; // Running Users worker_processes 1; //Number of work processes #Error_log logs/error.log;//location of error log file #error_log logs/error.log notice; #error_log logs/error.log info; #Pid logs/nginx.pid; //PID file storage location
The worker_processes configuration item represents the number of work processes. If the server has multiple CPUs or uses a multi-core processor, you can refer to the total number of CPU cores to specify the number of work processes (you can use the command cat/proc/cpuinfo | grep "processor"| wc-l).
If the demand for website visits is not large, it is usually set to 1 (self-adjusting according to the situation).
2) I/O Event Configuration
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf .............................. //Omit part of content events { use epoll; //Using epoll model worker_connections 1024; //Each process handles 1024 connections }
For 2.6 and above cores, epoll model is recommended to improve performance.
3) HTTP configuration
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf .............................. //Omit part of content http { include mime.types; //Supporting Multimedia Format default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; //Log Master Format access_log logs/access.log main; //Access log storage location sendfile on; //Open efficient file transfer mode #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; //Connection timeout (default is seconds) #gzip on; server { //Monitoring Configuration of Web Services listen 80; //Monitor address and port server_name localhost; //Website Name charset utf-8; //Default Character Set for Web Pages location / { //Root directory configuration (must exist) root html; //Location of website root directory index index.html index.php; //default page } error_page 500 502 503 504 /50x.html; //Internal error feedback page location = /50x.html { //Error Page Configuration root html; } } }
The root statement is used to set the path of the web document at a specific access location, defaulting to the html subdirectory under the Nginx installation directory. According to the actual situation, it should be revised by itself.
3. Access Status Statistics and Virtual Host Application
1.Nginx Access Status Statistics
Nginx has built-in HTTP_STUB_STATUS status statistics module to feedback current Web access. When compiling and installing Nginx, you need to add - with-http_stub_status_module to start the module. In addition, the configuration file should be changed again:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf ........................ //Omit part of content server { ............ //Eliminate some of the content and add the following location /status{ stub_status on; access_log off; } } [root@localhost ~]# systemctl restart nginx
User test access:
Where "Active connections" denotes the current number of active connections;
"server accepts handled requests" denotes the connection information processed:
The three numbers in turn represent the number of connections processed, the number of successful TCP handshakes, and the number of requests processed.
2. Virtual Web Host Based on Domain Name
Domain name-based virtual Web hosts differentiate different Web sites through domain names. When using Nginx to build a virtual host server, each virtual Web site has its own IP address, port number and domain name of its own "server {}" configuration segment. This example creates a virtual host for different domain names.
The basic steps to create a virtual host are as follows:
(1) Set up DNS service to resolve two domain names into the same IP address.
Building DNS services can refer to blog posts: Linux Builds DNS Service
(2) Preparing website catalogue and test files
[root@localhost ~]# mkdir -p /var/www/benet [root@localhost ~]# mkdir -p /var/www/accp [root@localhost ~]# echo "www.benet.com" > /var/www/benet/index.html [root@localhost ~]# echo "www.accp.com" > /var/www/accp/index.html
(3) Adjusting the main configuration file of Nginx service
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf .................. //Omit part of content http { .................. //Omit part of content server { listen 80; server_name www.benet.com; charset utf-8; location / { root /var/www/benet; index index.html index.php; } location /status{ stub_status on; access_log off; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.accp.com; charset utf-8; location / { root /var/www/accp; index index.html index.php; } location /status{ stub_status on; access_log off; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@localhost ~]# systemctl restart nginx
(4) Access to Virtual Host
———————— This is the end of the article. Thank you for reading.————————