12.1 一个nginx部署多个站点
基于不同的IP实现(很少使用)
10.0.0.7 返回 hello 10.7
172.16.1.7 返回 hello 172.7
[root@web01 ~]# cat /etc/nginx/conf.d/ip.conf
server {
listen 10.0.0.7:80;
location / {
root /ip1;
index index.html;
}
}
server {
listen 172.16.1.7:80;
location / {
root /ip2;
index index.html;
}
}
[root@web01 ~]# mkdir /ip1
[root@web01 ~]# mkdir /ip2
[root@web01 ~]# echo "hello,10.7" > /ip1/index.html
[root@web01 ~]# echo "hello,172.7" > /ip2/index.html
[root@web01 ~]# systemctl restart nginx
测试
[root@web01 ~]# curl 10.0.0.7:80
hello,10.7
[root@web01 ~]# curl 172.16.1.7:80
hello,172.7
基于不同的端口实现(测试)
[root@web01 ~]# cat /etc/nginx/conf.d/port.conf
server {
listen 8081;
location / {
root /port1;
index index.html;
}
}
server {
listen 8082;
location / {
root /port2;
index index.html;
}
}
[root@web01 ~]# mkdir /port1
[root@web01 ~]# mkdir /port2
[root@web01 ~]# echo "8081" > /port1/index.html
[root@web01 ~]# echo "8082" > /port2/index.html
[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl reload nginx
基于不同的域名实现(生成)
docs.oldux.com docs
test.oldxu.com test
www.oldxu.com www
[root@web01 ~]# cat /etc/nginx/conf.d/all.oldxu.com.conf
#在一个文件中,编写三个站点(实际生产都是一个域名对应一个文件,这样能够清晰找到对应的配置)
server {
listen 80;
server_name docs.oldxu.com;
location / {
root /docs;
index index.html;
}
}
server {
listen 80;
server_name test.oldxu.com;
location / {
root /test;
index index.html;
}
}
[root@web01 ~]# mkdir /docs
[root@web01 ~]# mkdir /test
[root@web01 ~]# echo "docs" > /docs/index.html
[root@web01 ~]# echo "test" > /test/index.html
[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl restart nginx
记得配置域名解析(我们由于是假的域名,所以配置DNS劫持即可)
实战模拟阿里云镜像提供站点
# 修改配置文件为以下内容
[root@web01 mirror]# cat /etc/nginx/conf.d/mirror.wenjie.com.conf
server{
listen 80;
server_name mirror.wenjie.com;
charset utf-8;
root /mirror;
location / {
index index.html;
}
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
# 编辑/mirror/index.html内容如下(实现跳转)
[root@web01 mirror]# cat index.html
<h1> 欢迎来到mirror.wenjie.com镜像下载地址 <h1>
<br>
<a href="repo">点击跳转</a>
# 同步源至本地
[root@web01 mirror]# rsync -avz rsync://rsync.mirrors.ustc.edu.cn/repo/centos/ /mirror/repo/
Nginx访问控制
基于IP的访问控制
allow 表示允许某个ip段访问
deny 表示拒绝某个ip段访问
按照顺序匹配,前面的规则匹配成功,后面的规则就都不会被匹配
基于密码的访问控制
- 准备密码文件
htppasswd -cb 密码文件路径 用户名 密码 - 添加用户认证
auth_basic “网站名称”;
auth_basic_user_fire “密码文件路径”;
Nginx限速
- 为什么要限速?
限制某个用户在一定时间内能够下载的速度. - 限速类型
下载限速:限制用户下载资源的速度
limit_ rate_ after 100m; #先让其最快速下载100m, ran' hou
limit_ rate 200k; # 限速
请求限速:
限制单位时间内http的请求数。使用
limit_req_zone用于http层。
# 定义一个10m内存空间,名称叫req_one 限制的速率是每秒1个请求,针对的来源的IP地址。(在http层调用)
limit. req. zond Sbinary_ remote. addr zone-req. _one:10m rate-1r/s;
# 调用(在server层调用)请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量,则返回503
himit_ req zone=req_ one burst=3 nodelay;
连接限速:
定义了一个10m内存空间,名称叫conn_od,针对的是来源的IP
limit_ conn_ zone Sbinary- remote_ addr zone= conn_ od: 10m;
Nginx状态模块stub_stauts
在配置文件中添加stub_status 即可开启Nginx状态模块(需要写入location层中),其下的各个指标的意思是:
Active connects : 活跃连接数
accepts:已接受的tcp连接数
handled:已处理的tcp连接数
requests:总http请求数量
Reading:当前读取的请求头数量
Writing:当前响应的请求头数量
waiting:当前等待请求的空闲客户端连接数
(主要用于监控)
location匹配
location匹配规则
- 什么是location
用来控制访问网站的uri路径 - location匹配的符号
= 表示精准匹配 优先级:1
^~ 表示以某个字符开头 优先级:2
~ 表示区分大小写的正则匹配 优先级:3
~* 表示不区分大小写正则匹配 优先级:4
/ 通用匹配,任何请求都会匹配到 优先级:5
@ 表示该location不用于常规请求,而是用于请求重定向,实现方法:
# 把返回的状态码是404,403,401的结果交给err location处理
error_ page 404 403 401 @err;
# 处理方式为返回页面“你可能是不小心走丢了”
location @err {
default_ type text/html ;
return 200 ’你可能是不小心走丢了。';
location的uri中末尾加不加/的区别:
加/:默认上/test目录下寻找index. html文件。如果没有index. htmL则会直接返回403
不加/:默认上/test目录下寻找index.html文件,如栗没有index. html则会查找uri目录下的test文件
nginx日志
log_format:定义日志格式
日志格式的表示方法有以下这些:
$remote_ addr #记录客户端IP地址
$remote_ user #记录客户端用户名
$time_ local #记录通用的本地时间
$time_ iso8601 #记录IS08601标准格式 下的本地时间
$request #记录请求的方法以及请求的http协议
$status #记录请求状态玛(用于定位错误信息)
$body_ bytes_ sent #发送给客户端的资源字节数,不包括响应头的大小
$bytes_ sent #发送给客户端的总字节数
$msec #日志写入时间。单位为秒,精度是毫秒。
$http_ referer #记录从哪个页面链接访问过来的
$http_ user_ agent #记录客户端浏览器相关信息
$http_ x forwarded_ for #记录客户端IP地址
$request_ length #请求的长度(包括请求行,请求头和请求正文) 。
$request_ time #请求花费的时间,单位为秒,精度毫秒
log_format main '日志格式';
access_log :定义访问的路径
access_log 日志路径 main(日志格式)
errot_log:定义错误日志的路径
error_log 日志路径 main(日志格式)
如果某个网站不想记录日志,那么就可以使用
access_log off;或
access_log /dev/null;
这两种方法来停止记录日志的功能。