目录
5.1 先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
5.2 修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置
1 Nginx简介
1.1 概述
Nginx
- Nginx是一个高性能的HTTP和反向代理服务器。
- 是一款轻量级的高性能的web服务器/反向代理服务器/电子邮件(IMAP/POP3)代理服务器单
- 台物理服务器可支持30 000~50 000个并发请求。
Apache
Apache是以进程为基础的结构,进程要比线程消耗更多的系统开支,不太适用于多处理器环境,因此,在一个apache Web站点扩容时,通常是增加服务器或扩充群集节点而不是增加处理器。
扩展:Tengine
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。
1.2 Nginx和Apache的差异
- Nginx是一个基于事件的Web服务器,Apache是一个基于流程的服务器
- Nginx所有请求都由一个线程处理,Apache单个线程处理单个请求
- Nginx避免子进程的概念,Apache是基于子进程的
- Nginx在内存消耗和连接方面更好,Apache在内存消耗和连接方面一般
- Nginx的性能和可伸缩性不依赖于硬件,Apache依赖于CPU和内存等硬件
- Nginx支持热部署,Apache不支持热部署
- Nginx对于静态文件处理具有更高效率,Apache相对一般
- Nginx在反向代理场景具有明显优势,Apache相对一般
2 Nginx的编译安装步骤详解
2.1 关闭防火墙,安装依赖关系包
1. #关闭防火墙
systemctl stop firewalld
setenforce 0
2. #安装依赖关系包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
2.2 新建用户和组便于管理
3. #新建用户 nginx 服务程序默认 以 nobody 身份运行,建议为其创建专门的用户账户,以便更准确的控制访问权限
[root@localhost opt]#useradd -M -s /sbin/nologin nginx
2.3 将压缩包传入到/opt目录下 ,编译安装
4.#切换至opt目录,将下载好的压缩包传进来
cd /opt
[root@localhost opt]#ls
nginx-1.12.0.tar.gz
5.#解压文件
tar -zxvf nginx-1.12.0.tar.gz
6.#切换至解压后的文件夹编译
./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
//解释
--prefix=/usr/local/nginx \
#安装路径
--user=nginx \
#指定用户名
--group=nginx \
#指定用户组
--with-http_stub_status_module
#启用此模块支持状态统计
//
7.#安装
make -j2 && make install
2.4 做软连接并启动nginx/停止nginx
8. #做软连接,让系统识别nginx的操作命令
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
9. #检查配置文件是否配置 正确
nginx -t
10. #启动nginx
nginx
11. #查看是否启动成功
netstat -antulp|grep nginx
12查进程号
lsof -i :80
13查看主进程
cat /usr/local/nginx/logs/nginx.pid
14停止进程
kill -3 <PID>
2.5 添加Nginx系统服务
[Unit] #服务的说明
Description=nginx #描述服务
After=network.target #依赖,当依赖的服务启动后再启动自定义的服务
[Service] #服务运行参数的设置
Type=forking #后台运行的形式,使用此启动类型同时指定
PIDFile=/usr/local/nginx/logs/nginx.pid #以便systemd能够跟踪服务的主进程
ExecStart=/usr/local/nginx/sbin/nginx #服务的具体运行命令
ExecReload=/usr/bin/kill -s HUP $MAINPID #重启命令
ExecStop=/usr/bin/kill -s QUIT $MAINPID #停止命令
PrivateTmp=true #表示给服务分配独立的临时空间
[Install] #服务安装的相关设置,可设置多用户
WantedBy=multi-user.target
3 Nginx的配置文件
3.1 Nginx的主配置文件
- 全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
- events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
- http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
- server块:配置虚拟主机的相关参数,一个http中可以有多个server。
- location块:配置请求的路由,以及各种页面的处理情况。
vim /usr/local/nginx/conf/nginx.conf
3.2 全局配置
#user nobody; ##运行用户
worker_processes 1; ##工作进程数,可配置成服务器内核数*2,如果网站访问量不大,一般设为1就够用了
#error_log logs/error.log; ####错误日志文件的位置
#pid logs/nginx.pid; ####PID文件的位置
events {
use epoll; #使用 epoll 模型以提高性能,2.6 以上版本建议使用
worker_connections 4096; #每个进程处理4096个连接
}
3.3 I/O事件配置
events {
use epoll; 使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
worker_connections 4096; 每个进程处理 4096 个连接
}
#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制.
3.4 http配置
http {
include mime.types; ##文件扩展名与文件类型映射表
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"';
#access_log logs/access.log main; #日志格式设定
sendfile on; ##支持文件发送(下载)
##此选项允许或禁止使用socket的TCP cORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#tcp_nopush on;
##连接保持超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on; ##gzip模块设置,设置是否开启gzip压缩输出
server {
listen 80; ##监听地址及端口
server_name www.yxp.com; ##站点域名,可以有多个,用空格隔开
#charset utf-8; #网页的默认字符集
#access_log logs/host.access.log main;
location / { ##根目录配置
root html; ##网站根目录的位置/usr/local/nginx/html
index index.html index.htm; ##默认首页文件名
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; ##内部错误的反馈页面
location = /50x.html { ##错误页面配置
root html;
}
3.5 验证
4 日志格式设定
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
location常见配置指令,root、alias、proxy_pass
root(根路径配置):请求www.gc.com/test,会返回文件/usr/local/nginx/html/test/index.html
alias(别名配置):请求www.gc.com/test,会返回文件/usr/local/nginx/html/index.html
5 访问状态统计配置
5.1 先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
5.2 修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.lic.com;
charset utf-8;
location / {
root html;
index index.html index.php;
}
##添加 stub_status 配置##
location /status { 访问位置为/status
stub_status on; 打开状态统计功能
access_log off; 关闭此位置的日志记录
}
}
}
5.3 重启服务,访问测试
systemctl restart nginx
浏览器访问 http://192.168.192.52/status
Active connections :表示当前的活动连接数;
server accepts handled requests :表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、 已处理的请求数。
5.4 监控并发量
脚本测试
#!/bin/bash
#设置变量,获取当前活跃的连接数
num=$(curl -Ls http://192.168.152.11/status | awk '/Active connections/ {print $3}')
if [ "$num" -gt 2 ];then
echo "警告!当前web服务并发量过大! 当前的并发量为$num"
fi
6 基于授权的访问控制
6.1 生成用户密码认证文件
yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhangsan
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db
6.2 修改主配置文件相对应的目录,添加认证配置项
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##添加认证配置##
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
}
6.3 重启服务,访问测试
7 基于客户端的访问控制
访问控制规则如下:
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##添加控制规则##
deny 192.168.152.11; 拒绝访问的客户端 IP
allow all; 允许其它IP客户端访问
}
}
systemctl restart nginx
8 基于域名的Nginx虚拟主机
8.1 为虚拟主机提供域名解析
echo "192.168.152.11 www.fl.com www.dl.com" >> /etc/hosts
8.2 为虚拟机准备网页文档
mkdir -p /var/www/html/fl
mkdir -p /var/www/html/dl
echo "<h1>www.fl.com</h1>" > /var/www/html/fl/index.html
echo "<h1>www.dl.com</h1>" > /var/www/html/dl/index.html
8.3 修改Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.fl.com; 设置域名www.fl.com
charset utf-8;
access_log logs/www.fl.access.log;
location / {
root /var/www/html/fl; 设置www.fl.com 的工作目录
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 80;
server_name www.dl.com; 设置域名www.dl.com
charset utf-8;
access_log logs/www.dl.access.log;
location / {
root /var/www/html/dl;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
8.4 重启服务,访问测试
systemctl restart nginx
浏览器访问
http://www.dl.com
http://www.fl.com
9 基于IP的Nginx虚拟主机
ifconfig ens33:0 192.168.152.11 netmask 255.255.255.0
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.152.11:8080; #设置监听地址
server_name www.dl.com;
charset utf-8;
access_log logs/www.dl.access.log;
location / {
root /var/www/html/dl;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.152.11:8888; #设置监听地址
server_name www.fl.com;
charset utf-8;
access_log logs/www.fl.access.log;
location / {
root /var/www/html/fl;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
systemctl restart nginx
浏览器访问
http://192.168.152.11:8080
http://192.168.152.11:8888
10 本章总结
- Nginx支持的最大并发量取决于CPU与系统允许进程最大读取文件数
- Nginx的单线程处理数不能大于系统允许进程最大读取文件数
- Nginx可以配置多种类型的虚拟主机,分别是基于 IP 的虚拟主机、基于域名的虚拟主机、基于端口的虚拟主机。