文章目录
一、Nginx简介
(1) 一款高性能、轻量级Web服务软件
稳定性高
系统资源消耗低
(2) 对HTTP并发连接的处理能力高(能够处理高并发)
单台物理服务器可支持30000~50000个并发请求
二、Nginx服务
2.1、编译安装nginx
yum -y install pcre-devel zlib-devel
yum -y install \
pcre-devel \# 是perl语言的正则表达式库
lib-devel # 软件包的开发包,一个函数库,库里面包括头文件、静态库甚至源码等资源
tar zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2/
./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \ // 指定其运行时的账户与组
> --group=nginx \
> --with-http_stub_status_module //统计状态模块
make && make install
useradd -M -s /sbin/nologin nginx //创建一个不可登录的程序用户
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //优化执行路径
ln -s /usr/local/nginx/conf/nginx.conf /etc/
nginx -t //配置文件语法检查
nginx //启动服务
netstat -anpt | grep nginx //查看nginx服务是否开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 48917/nginx: master
killall -3 nginx //停止服务
netstat -anpt | grep nginx
killall -1 nginx //安全重启
netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 49947/nginx: master
vi /etc/init.d/nginx ###制作管理脚本
#!/bin/bash
#chkconfig: 35 99 20
#description: nginx server
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
2.2、Nginx的访问状态统计
1、修改主配置文件
vi /etc/nginx.conf
user nginx nginx //修改#user nobody为user nginx nginx
error_log logs/error.log info //去除#号
---日志级别---
events {
use epoll; //新增此行,默认使用select/poll
worker_connections 4096;//表示1个工作进程允许4096个连接。
}
log_format main //定义日志格式,去除#号
access_log logs/access.log main; //去除#号
access_log logs/aa.com.access.log main; //去除#号,并修改
ulimit -n //查看和更改系统本地打开资源数
ulimit -n 65535 >>/etc/rc.local
---配置统计功能---
location ~ /status {
stub_status on;
access_log off;
} //在server模块里的error_page上面增加
systemctl restart nginx //重新开启nginx服务
systemctl stop firewalld //关闭防火墙和核心防护
setenforce 0
2、验证
在客户端浏览器输入:http://192.168.40.11/status 验证
3、nginx status 详解
active connections:活跃的连接数量;
server accepts handled requests:总共处理n个连接,成功创建n次握手,共处理n个请求;
reading:读取客户端的连接数;
writing:响应数据到客户端的数量;
waiting:开启keep-alive的情况下,这个值等于active-(reading+writing),意思就是Nginx已经处理完正在等候下一次指令的驻留地址。
2.3、Nginx的验证功能
1、修改主配置文件
yum -y install httpd-tools
htpasswd -c /usr/local/nginx/passwd.db jack //创建访问用户
New password:
Re-type new password:
Adding password for user jack
chown nginx /usr/local/nginx/passwd.db
vi /etc/nginx.conf
location / {
root html;
index index.html index.htm;
allow 192.168.40.11/24; ###允许本机访问
deny all; ###拒绝所有
auth_basic "secret"; ###验证方式为密码验证
auth_basic_user_file /usr/local/nginx/passwd.db; //密码所在文件
}
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
systemctl restart nginx //重启服务
2、验证
三、配置Nginx的虚拟主机
3.1、基于域名
1、修改主配置文件
vi /etc/nginx.conf
server {
listen 80;
server_name www.aa.com;
charset utf-8;
access_log logs/aa.com.access.log main;
location / {
root /var/www/aa;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www. ab.com;
charset utf-8;
access_log logs/ab.com.access.log main;
location / {
root /var/www/ab;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x. html; {
root html;
}
}
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
systemctl restart nginx //重启nginx服务
systemctl stop firewalld
setenforce 0
vi /etc/hosts ###增加映射域名
192.168.40.11 www.aa.com www.ab.com
2、准备测试页
mkdir -p /var/www/aa
mkdir -p /var/www/ab
echo "<html><body><h1>This is aa test.</h1></body></html>" > /var/www/aa/index.html
echo "<html><body><h1>This is ab test.</h1></body></html>" > /var/www/ab/index.html
3、测试
3.2、基于IP地址
1、新增一张虚拟网卡
ifconfig ens33:1 192.168.50.11/24
2、修改主配置文件
vi /etc/nginx.conf
server {
listen 192.168.40.11:80; //修改IP地址
server_name www.aa.com;
server {
listen 192.168.50.11:80; //修改IP地址
server_name www.ab.com;
systemctl restart nginx //重启服务
3、验证
3.3、基于端口号
1、修改主配置文件
vi /etc/nginx.conf
server {
listen 192.168.40.11:80;
server_name www.aa.com;
server {
listen 192.168.50.11:8080; //更改端口号
server_name www.ab.com;
systemctl restart nginx //重启服务
2、验证