1. Nginx服务基础
1.1 关于Nginx
一款高性能、 轻量级反向代理Web服务软件,用于HTTP、HTTPS、 SMTP、 POP3 和 IMAP协议。他实现非常高效的反向代理、负载平衡,他可以处理2-3万并发连接数,官方监测能 支持5万并发,现在中国使用nginx网站用户有很多,例如:新浪、网易、腾讯等。
- 稳定性高
- 系统资源消耗低
- 对HTTP并发连接的处理能力高
- 单台物理服务器可支持30000 ~ 50000个并发请求
1.2 Nginx有哪些优点?
- 跨平台、配置简单。
- 非阻塞、高并发连接:处理2-3万并发连接数,官方监测能支持5万并发。
- 内存消耗小:开启10个Nginx才占150M内存。
- 成本低廉,且开源。
- 稳定性高,宕机的概率非常小。
- 内置的健康检查功能:如果有一个服务器宕机, 会做一个健康检查, 再发送的请求就不会发 送到宕机的服务器了。重新将请求提交到其他的节点上
1.3 Nginx应用场景?
- http服务器。Nginx是一个http服务 可以独立提供http服务。可以做网页静态服务器。
- 虚拟主机。可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机。
- 反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求 时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负 载,不会应为某台服务器负载高宕机而某台服务器闲置的情况。
- nginx中也可以配置安全管理、比如可以使用Nginx搭建 API接口网关对每个接口服务进行拦 截。
- web前端缓存
1.4 Nginx和Apache的差异
轻量级
:Nginx比Apache占用更少的内存及资源静态处理
:Nginx静态处理性能比Apache高Nginx可以实现无缓存的反向代理加速
,提高网站运行速度Nginx的性能和可伸缩性不依赖于硬件
,Apache依赖于硬件Nginx支持热部署
,启动速度迅速,可以在不间断服务的情况下,对软件版本或者配置进行升级Nginx是异步进程
,多个连接可以对应一个进程。Apache是同步多进程,一个连接对应一个进程Nginx高度模块化
,编写模块相对简单,且组件比Apache少 高并发下nginx能保持低资源低消耗高性能Nginx配置简洁
,Apache配置复杂- Nginx是基于
事件
的web服务器,Apache是基于流程
的web服务器
1.5 Nginx的进程
Apache和Nginx的默认端口都是80,如果其中一个已经启动了,那么再启动另一个会报错。如果想要同时使用,可以修改其中一个的端口号。
- Nginx有两个进程:
master process:主进程(守护进程),用来管理工作进程。
worker process:工作进程,用来处理用户的请求。
2. 安装配置Nginx服务
2.1 编译安装Nginx服务
2.1.1关闭防火墙,将安装nginx所需软件包传到/opt目录下
[root@www ~]# systemctl stop firewalld
[root@www ~]# systemctl disable firewalld
[root@www ~]# setenforce 0
nginx-1.12.0. tar.gz
2.1.2 安装依赖包
nginx的配置及运行需要pcre、zlib、opehssl等软件包的支持,因此需要安装这些软件的开发包,以便提供相应的库和头文件。
[root@www ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
2.1.3 创建运行用户、组
(Nginx 服务程序默认以nobody身份运行,建议为其创建专门门的用户账号,以便更准确地控制其访问权限)
[root@www ~]# useradd -M -s /sbin/nologin nginx
2.1.4 编译安装Nginx
[root@www ~]# cd /opt
[root@www ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@www ~]# cd nginx-1.12.0/
[root@www ~]# ./configure \
--prefix=/usr/local/nginx \ //指定nginx的安装路径
--user=nginx \ //指定用户名
--group=nginx \ //指定组名
--with-http_stub_status_module //启用http_ stub_status_module 模块以支持状态统计
----------------------------------------------------------------------------------------------
[root@www ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@www ~]# make && make install
[root@www ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //让系统识别nginx的操作命令
2.2 检查、启动、重启、停止nginx服务
- 检查配置文件是否配置正确
[root@www ~]# nginx -t
- 启动
[root@www ~]# nginx
- 停止
[root@www ~]# cat /usr/local/nginx/logs/nginx.pid //先查看nginx的PID号
[root@www ~]# kill -3 <PID号>
[root@www ~]# kill -s QUIT <PID号>
[root@www ~]# killall -3 nginx
[root@www ~]# killall -s QUIT nginx
- 重载
[root@www ~]# kill -1 <PID号>
[root@www ~]# kill -s HUP <PID号>
[root@www ~]# killall- 1 nginx
[root@www ~]# killall -s HUP nginx
- 日志分割,重新打开日志文件
[root@www ~]# kill -USR1 <PID号>
- 平滑升级
[root@www ~]# kill -USR2 <PID号>
- 新版本升级
[root@www ~]# tar -zxvf nginx-1.XX.XX.tar.gz
[root@www ~]# cd nginx-1.XX.XX
[root@www ~]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module
[root@www ~]# make
[root@www ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
[root@www ~]# cp objs/nginx /usr/local/nginx/sbin/nginx
[root@www ~]# make upgrade //或者先killall nginx ,再/usr/local/nginx/sbin/nginx
- 查看版本
[root@www nginx-1.12.0]# nginx -v
[root@www nginx-1.12.0]# nginx -V
2.3 添加Nginx 系统服务
- 方法一
[root@www ~]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;;
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@www ~]# chmod +x /etc/init.d/nginx
[root@www ~]# chkconfig --add nginx //添加为系统服务
[root@www ~]# systemctl stop nginx
[root@www ~]# systemctl start nginx
- 方法二
[root@www ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@www ~]# chmod 754 /lib/systemd/system/nginx.service
[root@www ~]# systemctl start nginx.service
[root@www ~]# systemctl enable nginx.service
[Unit]服务的说明
Description:描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务
[Service]服务运行参数的设置
Type=forking是后台运行的形式,使用此启动类型应同时指定PIDFile=,以便systemd能够跟踪服务的主进程。
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径
[Install]服务安装的相关设置,可设置为多用户
2.4 Nginx服务的主配置文件nginx.conf
全局块
:全局配置,对全局生效events块
: 配置影响 Nginx服务器与用户的网络连接http块
:配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置server块
: 配置虚拟主机的相关参数,一个http块中可以有多个server 块location块
: 用于配置匹配的uriupstream
:配置后端服务器具体地址,负载均衡配置不可或缺的部分。
2.4.1 全局配置
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody; ##运行用户,若编译时未指定则默认为nobody
worker_processes 1
##工作进程数量,一般设置为和CPU核数一样
#error_log logs/error.log; ##错误日志文件的位置
#pid logs/nginx.pid; ##PID文件的位置
2.4.2 I/O 事件配置
events {
use epoll; //使用epoll模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
worker_connections 4096; //每个进程处理4096个连接
}
软件最大支持并发数量 = 工作进程数量 * 每个进程处理的连接数
- 修改系统支持的最大并发量:
如提高每个进程的连接数还需执行“
ulimit -n 65535
”命令临时修改本地每个进程可以同时打开的最大文件数。
在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建 个socket句柄,每个socket句柄同时也是一个文件句柄)。
可使用
ulimit -a
命令查看系统允许当前用户进程打开的文件数限制。
epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情 况下的系统CPU利用率。
/etc/security/limits.conf编辑该文件可永久修改。
- 第一列为用户和组
- 第二列为磁盘限额,软硬限制。hard表示硬限制。
- 第三列为项目。nofile表示最大可打开文件数量。
- 第四列为相应项目的数量
2.4.3 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; ##开启文件传输模式
#tcp_nopush on; ##减少网络报文段的数量
#keepalive_timeout 0; ##连接保持超时时间,单位是秒
keepalive_timeout 65;
#gzip on; ##gzip模块设置,设置是否开启gzip压缩输出
##Web 服务的监听配置
server {
listen 80; ##监听地址及端口
server_name www.benet.com; ##站点域名,可以有多个,用空格隔开
charset utf-8; ##网页的默认字符集
location / { ##根目录配置
##网站根目录的位置/usr/local/nginx/html
root html;
index index.html index.php; ##默认首页文件名
}
error_page 500 502 503 504 /50x.html; ##内部错误的反馈页面
location = /50x.html { ##错误页面配置
root html;
}
}
}
- 日志格式设定
$remote_addr(只能记录上游主机IP)
与$http_x_forwarded_for(能记录所有经过主机IP)
用以记录客户端的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(根路径配置):root /var/www/html
请求www.benet.com/test/1.html,会返回文件/var/www/html/test/1.html
location / {
root html; ##表示此时根目录为/usr/local/nginx/html/
index index.html index.htm;
}
#此时访问路径和返回文件的关系为:
http://192.168.200.101/index.html --> /usr/loca/nginx/html/index.html
http://192.168.200.101/test/index.html --> /usr/loca/nginx/html/test/index.html
- alias(别名配置):alias /var/www/html
请求www.benet.com/test/1.html ,会返回文件/var/www/html/1.html
location / {
root html; #表示此时根目录为/usr/local/nginx/html/
index index.html index.htm;
}
location /test {
alias /var/www/html; #设置别名,即虚拟路径。别名是一个整体
index index.html index.htm;; } #此时 http://192.168.200.101/test/ 匹配 /var/www/html/
#此时访问路径和返回文件的关系为:
http://192.168.200.101/index.html --> /usr/loca/nginx/html/index.html
http://192.168.200.101/test/index.html --> /var/www/html/index.html
- proxy_pass(反向代理配置)
3. Nginx虚拟主机
3.1 基于域名的Nginx虚拟主机
3.1.1 为虚拟主机提供域名解析
[root@www ~]# echo "192.168.200.101 www.accp.com www.benet.com" >> /etc/hosts
3.1.2 为虚拟主机准备网页文档
[root@www ~]# mkdir -p /var/www/html/benet
[root@www ~]# mkdir -p /var/www/html/accp
[root@www ~]# echo "<h1>www.accp.com</h1>" > /var/www/html/accp/index.html
[root@www ~]# echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html
3.1.3 修改Nginx的配置文件
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.accp.com; 设置域名www.kgc.com
charset utf-8;
access_log logs/www.accp.access.log; #设置日志名
location / {
root /var/www/html/accp; #设置www.accp.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.benet.com; #设置域名www.benet.com
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
3.1.4 重启服务,访问测试
[root@www ~]# systemctl restart nginx
- 浏览器访问
3.2 基于IP 的 Nginx 虚拟主机
3.2.1 准备网卡并为虚拟主机提供域名解析
[root@www ~]# ifconfig ens33:0 192.168.200.102 netmask 255.255.255.0
[root@www ~]# echo "192.168.200.101 www.accp.com" >> /etc/hosts
[root@www ~]# echo "192.168.200.102 www.benet.com" >> /etc/hosts
3.2.2 修改配置文件
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.200.101:80; #设置监听地址192.168.200.101
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.access.log;
location / {
root /var/www/html/accp;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.200.102:80; #设置监听地址192.168.200.102
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
3.2.3 重启服务,访问测试
[root@www ~]# systemctl restart nginx
- 浏览器访问
3.3 基于端口的 Nginx 虚拟主机
3.3.1 为虚拟主机提供域名解析
root@www ~]# echo "192.168.200.101 www.accp.com www.benet.com" >> /etc/hosts
3.3.2 修改配置文件
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.200.101:80; #设置监听 80 端口
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.access.log;
location / {
root /var/www/html/accp;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.200.101:8080; #设置监听 8080 端口
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
3.3.3 重启服务,访问测试
[root@www ~]# systemctl restart nginx
- 浏览器访问
4. 访问状态统计配置
4.1 查看模块安装情况
先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
[root@www ~]# cat /opt/nginx-1.12.0/auto/options | grep YES #可查看 nginx 已安装的所有模块
4.2 修改 nginx.conf 配置文件
指定访问位置并添加 stub_status 配置
[root@www ~]# cd /usr/local/nginx/conf
[root@www ~]# cp nginx.conf nginx.conf.bak
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.benet.com;
charset utf-8;
location / {
root html;
index index.html index.php;
}
##添加 stub_status 配置##
location /status { #访问位置为/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志记录
}
}
}
4.3 重启服务,访问测试
[root@www ~]# systemctl restart nginx
- 浏览器访问
Active connections
:表示当前的活动连接数;
server accepts handled requests
:表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、 已处理的请求数。
可
curl -Ls http://192.168.200.101/status
结合 awk与if 语句进行性能监控。
5. 基于授权的访问控制
5.1 生成用户密码认证文件
[root@www ~]# yum install -y httpd-tools
[root@www ~]# htpasswd -c /usr/local/nginx/passwd.db zhangsan
[root@www ~]# chown nginx /usr/local/nginx/passwd.db
[root@www ~]# chmod 400 /usr/local/nginx/passwd.db
5.2 修改主配置文件相对应目录,添加认证配置项
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##添加认证配置##
auth_basic "please input your username and password......"; #设置密码提示框文字信息
auth_basic_user_file /usr/local/nginx/passwd.db;
}
}
5.3 重启服务,访问测试
[root@www ~]# nginx -t
[root@www ~]# systemctl restart nginx
- 浏览器访问
6. 基于客户端的访问控制
- 访问控制规则如下
deny IP/IP 段:拒绝某个IP或IP段的客户端访问。
allow IP/IP 段:允许某个IP或IP段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##添加控制规则##
allow 192.168.200.101; #允许访问的客户端 IP
deny all; #拒绝其它IP客户端访问
}
}
[root@www ~]# systemctl restart nginx