目录
Nginx
一款高性能、轻量级web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高
单台物理服务器可支持30000~50000个并发请求
安装部署nginx
1.下载依赖包
yum -y install pcre-devel zlib-devel gcc++ gcc
2.创建管理nginx用户
useradd -M -s /sbin/nologin nginx
3.上传工具包
4.解压编译安装
5.
make && make install
启动关闭Nginx
启动
[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=4121,fd=6),("nginx",pid=4120,fd=6))
[root@bogon nginx-1.12.0]#
浏览器搜索IP
关闭
[root@bogon nginx-1.12.0]# /usr/local/nginx/sbin/nginx -s stop
[root@bogon nginx-1.12.0]# ss -nlpt | grep 80
[root@bogon nginx-1.12.0]#
优化(方便用户管理)
(软连接方便操作)
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
配置文件
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
保存退出(给予执行权限)
chmod +x /etc/init.d/nginx
chkconfig --add nginx
systemctl status nginx
重启服务,查看状态
[root@bogon nginx-1.12.0]# systemctl restart nginx
[root@bogon nginx-1.12.0]# systemctl status nginx
体验状态模块
编辑配置文件
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf
增加如图信息
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 restart nginx
[root@bogon ~]#
浏览器搜索,即可查看 状态
访问控制
基于账号密码
1.装命令工具
yum install -y httpd-tools
2.设置密码
htpasswd -c /usr/local/nginx/passwd.db test
3.调整权限
chmod 400 /usr/local/nginx/passwd.db
chown nginx /usr/local/nginx/passwd.db
ll -d /usr/local/nginx/passwd.db
4.修改配置文件
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf
5为其想要控制的增加认证
auth_basic "secret"; # 添加认证
auth_basic_user_file /usr/local/nginx/passwd.db; # 指定认证的账户密码文件
stub_status on;
6.检查配置文件,重启服务
[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 ~]#
这样在访问时就会让其登录账号,否则拒绝访问
基于IP的
修改配置
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf
编辑如图信息
检查配置文件,重启服务
[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
[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
检查配置,重启服务
验证
基于IP
修改配置
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf
修改如图设置