Article Directory
Preface
- Among various web server software, in addition to Apache HTTP Server, there is also a lightweight HTTP server software—Nginx. Its stable and efficient features are gradually recognized by more and more users.
- It will be the source code to BSD license issued in the form, because of its stability, rich feature set, simple configuration files and low system resource consumption and famous
- Its characteristics are: less memory and strong concurrency
- In Mainland China, users of nginx websites include: Baidu, JD , Sina , Netease , Tencent , Taobao, etc.
One: Nginx service foundation
1.1: Overview of Nginx
-
A high-performance, lightweight web service software
●High stability
●Low system resource consumption
●High processing capacity for concurrent HTTP connections
●A single physical server can support 30 000 ~ 50000 concurrent requests
●Occupy less memory and strong concurrency
1.2: Nginx compilation and installation
Installation Environment
CentOs 7.6 Nginx 1.15
- Install support software
[root@localhost opt]# iptables -F
[root@localhost opt]# setenforce 0
[root@localhost opt]# mkdir LNMP
[root@localhost opt]# cd LNMP/
#把包移到opt目录中
[root@localhost LNMP]# rz -E
rz waiting to receive.
[root@localhost LNMP]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz
mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2
ncurses-5.6.tar.gz
[root@localhost LNMP]# tar zxvf nginx-1.12.2.tar.gz -C /opt
- Installation environment dependent packages
[root@localhost nginx-1.12.2]# yum -y install gcc gcc-c++ zlib-devel pcre pcre-devel
- Create users and groups for management
-M:不创建家目录 -s:用于登录shell
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# id nginx
uid=1001(nginx) gid=1001(nginx) 组=1001(nginx)
- Compile and install
[root@localhost nginx-1.12.2]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@localhost nginx-1.12.2]# ./con
conf/ configure contrib/
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \ #设置安装路径
> --user=nginx \ #运行用户和组设为nginx
> --group=nginx \
> --with-http_stub_status_module #启动模块来支持状态统计
#编译
[root@localhost nginx-1.12.2]# make && make install
- Path optimization
- In order to make the operation of the Nginx server more convenient, you can create a soft link file for the main program nginx, so that the administrator can call the main program of Nginx by executing the "nginx" command
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@localhost nginx-1.12.2]# ls /usr/local/bin/
nginx
#测试语法是否正确
[root@localhost nginx-1.12.2]# 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
##查看主配置文件
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp html sbin
conf logs scgi_temp
fastcgi_temp proxy_temp uwsgi_temp
[root@localhost nginx]# cd conf/
#nginx.conf就是主配置文件
[root@localhost conf]# ls
fastcgi.conf nginx.conf
fastcgi.conf.default nginx.conf.default
fastcgi_params scgi_params
fastcgi_params.default scgi_params.default
koi-utf uwsgi_params
koi-win uwsgi_params.default
mime.types win-utf
mime.types.default
#html是主页文件
[root@localhost conf]# cd ..
[root@localhost nginx]# ls
client_body_temp html sbin
conf logs scgi_temp
fastcgi_temp proxy_temp uwsgi_temp
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# netstat -anpt | grep nginx
##服务没有开启
-
Start service
-
Run Nginx directly to start the Nginx server
[root@localhost html]# nginx
[root@localhost html]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 90530/nginx: master
- Host visit
- Use pkill to close the process (close all service processes)
[root@localhost html]# pkill nginx
[root@localhost html]# netstat -anpt | grep nginx
- Start, reload configuration, stop Nginx
[root@localhost ~]# nginx '#启动服务
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7180/nginx: master
[root@localhost ~]# yum -y install elinks
[root@localhost ~]# elinks http://localhost 3显示"Welcome to nginx!"页面,表明Nginx服务已经正常运行'
[root@localhost ~]# killall -s HUP nginx #-S选项指定信号种类,HUP信号表示重载配置'
[root@localhost ~]# killall -s QUIT nginx #QUIT信号表示退出进程
- Nginx added as a system service
[root@localhost ~]# vim /lib/systemd/system/nginx.service #添加使用systemctl工具进行管理
[Unit]
Description=nginx #描述
After=network.target #描述服务类别
[Service]
Type=forking #后台运行形势
PIDFile =/usr/local/nginx/logs/nginx.pid PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx 启动服务
ExecReload=/usr/bin/kill -S HUP $MAINPID 根据PID重载配置
ExecStop=/usr/bin/kill -S QUIT $MAINPID 根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
- There is also service tool management
[root@localhost ~]# cd /etc/inid.d 添加使用service工具进行管理
[root@localhost init.d]# ls
[root@localhost init.d]# vim nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
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
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# chkconfig --level 35 nginx on
Two: Configure the statistics page
[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf
####添加下面的图片内容
#在重启服务
[root@localhost init.d]# service nginx stop
[root@localhost init.d]# service nginx start
[root@localhost init.d]# 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
[root@localhost init.d]# netstat -ntap | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 90311/nginx: master
- Go to host test
- Refresh will make statistics
Three: configure virtual host
3.1: Nginx virtual host application
-
There are three types of virtual hosts supported by Nginx
●Virtual hosting based on domain name
●IP-based virtual host
●Port-based virtual host
-
Through the "server{}" configuration section
3.2: Virtual Web Hosting Based on Domain Name
- Prepare website directory and test files
[root@localhost init.d]# cd /var/
[root@localhost var]# ls
account crash games lib log opt spool yp
adm db gopher local mail preserve target
cache empty kerberos lock nis run tmp
[root@localhost var]# mkdir www
[root@localhost var]# cd www/
#新建两个站点
[root@localhost www]# mkdir shuai mei
[root@localhost www]# ls
mei shuai
[root@localhost www]# cd shuai/
[root@localhost shuai]# vim index.html
#添加以下内容
<h1>hell</h1>
[root@localhost shuai]# cd ../
[root@localhost www]# ls mei/
[root@localhost mei]# vim index.html
#添加网页内容
<h1>this is mei web</h1>
#下载tree
[root@localhost www]# yum -y install tree
[root@localhost www]# tree ./
./
├── mei
│ └── index.html
└── shuai
└── index.html
2 directories, 2 files
#Configure domain name resolution
[root@localhost www]# vim /etc/named.conf
#Area configuration
Configure area data
[root@localhost named]# cp -p named.localhost abc.com.zone
[root@localhost named]# vim abc.com.zone
[root@localhost named]# cp -p abc.com.zone ab.com.zone
#重启服务
[root@localhost named]# systemctl start named
- Modify the configuration file
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
server {
server_name www.shuai.com;
location / {
root /var/www/shuai;
index index.html index.php;
}
}
server {
server_name www.shi.com;
location / {
root /var/www/shi;
index index.html index.php;
}
}
- Restart service
[root@localhost named]# 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
[root@localhost named]# service nginx stop
[root@localhost named]# service nginx start
- Client test
3.3: Port-based virtual web host
-
Configuration steps
-
Modify the configuration file
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
server {
server_name 20.0.0.41:8080; ## 将域名修改为IP地址+端口
listen 20.0.0.41:8080; ## 增加这一行,内容为IP地址+监听端口
location / {
root /var/www/shuai;
index index.html index.php;
}
}
server {
server_name 20.0.0.41:80; ## 同上
listen 20.0.0.41:80; ## 同上
location / {
root /var/www/shi;
index index.html index.php;
}
}
3.4: IP-based virtual host configuration
- Add a network card and configure an IP address (20.0.0.50)
Edit nginx configuration file
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf ## 编辑配置文件
server {
server_name 20.0.0.41:80; ## IP地址+80端口
listen 20.0.0.41:80; ## IP地址+80端口
location / {
root /var/www/shuai;
index index.html index.php;
}
}
server {
server_name 20.0.0.50:80; ## 另一个IP地址+80端口
listen 20.0.0.50:80; ## 另一个IP地址+监听端口80
location / {
root /var/www/shi;
index index.html index.php;
}
}
Four: Nginx access control
4.1: Authorization-based access control
-
The configuration steps are basically the same as Apache
●Generate user password authentication file
●Modify the main configuration file to the corresponding directory, add authentication configuration items
●Restart service, access test
-
Generate user password authentication file
[root@localhost shuai]# yum -y install httpd
[root@localhost shuai]# which htpasswd
/usr/bin/htpasswd
#创建用户
[root@localhost shuai]# htpasswd -c /usr/local/nginx/passwd.db liu
New password: #输入密码
Re-type new password: #再次输入密码
Adding password for user liu
#把账户树组变为nginx
[root@localhost shuai]# chown nginx /usr/local/nginx/passwd.db
#设置权限
[root@localhost shuai]# chmod 400 /usr/local/nginx/passwd.db
- Modify the main configuration file to the corresponding directory, add authentication configuration items
[root@localhost shuai]# vim /usr/local/nginx/conf/nginx.conf
##验证语法
[root@localhost shuai]# 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
#关闭服务在开启
[root@localhost shuai]# service nginx stop
[root@localhost shuai]# service nginx start
Client test
4.2: Client-based access control
-
Determine whether to allow access to the page through the client IP address
-
Configuration rules
deny IP/IP segment: deny client access to a certain IP or IP segment
allow IP/IP segment: Allow client access of a certain IP or IP segment
The rule is executed from top to bottom, if it matches, it will stop and no longer match
-
Configuration steps
- Modify the main configuration file nginx.conf and add corresponding configuration items
[root@localhost shuai]# vim /usr/local/nginx/conf/nginx.conf