关于Nginx
一款高新能,轻量级Web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高
单台物理服务器可支持3w到5w个并发请求
Nginx安装部署
[root@bogon ~]# yum -y install pcre-devel zlib-devel gcc++ gcc (1) 安装支持软件
#创建运行用户、组
#Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限,增加灵活性、降低安全风险
[root@bogon ~]# useradd -M -s /sbin/nologin nginx
[root@bogon ~]# 上传包 nginx-1.12.0.tar.gz
[root@bogon ~]# tar zxf nginx-1.12.0.tar.gz -C /usr/src/
[root@bogon ~]# cd /usr/src/nginx-1.12.0/
[root@bogon nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@bogon nginx-1.12.0]# echo $?
0
[root@bogon nginx-1.12.0]# make && make install
[root@bogon nginx-1.12.0]# ls /usr/local/nginx/
conf html logs sbin
[root@bogon nginx-1.12.0]# /usr/local/nginx/sbin/nginx 启动
[root@bogon nginx-1.12.0]# ss -nlpt | grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=10716,fd=6),("nginx",pid=10715,fd=6))
浏览器上访问http://192.168.16.129/
[root@bogon nginx-1.12.0]# /usr/local/nginx/sbin/nginx -s stop 停止
#为了使 Nginx 服务器的运行更加方便,可以为主程序 nginx 创建链接文件,以便管理员直接执行“nginx”命令就可以调用 Nginx 的主程序
[root@bogon nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
Nginx 的运行控制
[root@bogon nginx-1.12.0]# vi /etc/init.d/nginx
#!/bin/bash
# 必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10
# 90是启动优先级,10是停止优先级,优先级范围是0-100,数字越大,优先级越低
#chkconfig: 2345 10 90
#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@bogon nginx-1.12.0]# chmod +x /etc/init.d/nginx
[root@bogon nginx-1.12.0]# chkconfig --add nginx
[root@bogon nginx-1.12.0]# systemctl status nginx
● nginx.service - SYSV: Nginx Service Control Script
Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
[root@bogon nginx-1.12.0]#
这样一来,就可以systemctl 命令来启动、停止、重启、重载 Nginx 服务器了,方法是在执行时添加相应的 start、stop、restart、reload 参
访问状态统计
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf
47行添加
location /status {
stub_status on;
}
[root@bogon ~]# 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@bogon ~]# systemctl start nginx
Nginx访问控制
基于授权访问控制
Ø 生成用户密码认证文件。
Ø 修改主配置文件相对应目录,添加认证配置项。
Ø 重启服务,访问测试。
[root@bogon ~]# yum install -y httpd-tools
[root@bogon ~]# htpasswd -c /usr/local/nginx/passwd.db test
New password:
Re-type new password:
Adding password for user test
[root@bogon ~]# chmod 400 /usr/local/nginx/passwd.db
[root@bogon ~]# chown nginx /usr/local/nginx/passwd.db
[root@bogon ~]# ll -d /usr/local/nginx/passwd.db
-r-------- 1 nginx root 43 7月 16 09:46 /usr/local/nginx/passwd.db
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf
47行加
location /status {
auth_basic "secret"; # 添加认证
auth_basic_user_file /usr/local/nginx/passwd.db; # 指定认证的账户密码文件
stub_status on;
}
[root@bogon ~]# 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@bogon ~]# systemctl restart nginx
[root@bogon ~]#
基于客户端的访问控制
Ø deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
Ø allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
Ø 规则从上往下执行,如匹配则停止,不再往下匹配。
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf
47行加
location /status {
deny 192.168.16.1;
allow all;
stub_status on;
}
[root@bogon ~]# 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@bogon ~]# systemctl restart nginx
[root@bogon ~]#
Nginx 虚拟主机
基于域名的虚拟主机
实现不同的域名访问不同的虚拟主机
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.16.129:81;
server_name 192.168.16.129:81;
location / {
root html/kgc01;
index index.html index.htm;
}
}
server {
listen 192.168.16.129:82;
server_name 192.168.16.129:82;
location / {
root html/kgc02;
index index.html index.htm;
}
}
[root@bogon ~]# ls /usr/local/nginx/html/
50x.html index.html
[root@bogon ~]# mkdir /usr/local/nginx/html/kgc01
[root@bogon ~]# mkdir /usr/local/nginx/html/kgc02
[root@bogon ~]# echo 'This is kgc01' > /usr/local/nginx/html/kgc01/index.html
[root@bogon ~]# echo 'This is kgc02' > /usr/local/nginx/html/kgc02/index.html
[root@bogon ~]#
验证
[root@bogon ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.16.129 www.kgc01.com
192.168.16.129 www.kgc02.com
[root@bogon ~]# curl www.kgc01.com
This is kgc01
[root@bogon ~]#
基于 IP 的虚拟主机
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.16.129:81;
server_name 192.168.16.129:81;
location / {
root html/kgc01;
index index.html index.htm;
}
}
server {
listen 192.168.16.129:82;
server_name 192.168.16.129:82;
location / {
root html/kgc02;
index index.html index.htm;
}
}
[root@bogon ~]# 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@bogon ~]# systemctl restart nginx
[root@bogon ~]# curl 192.168.16.129:81
This is kgc01
[root@bogon ~]# curl 192.168.16.129:82
This is kgc02
[root@bogon ~]#