一:目录索引
作用:可以做简单的文件下载服务器
参数:
Syntax:
autoindex on | off; #on表示开启索引
autoindex_exact_size off #默认为 on,显示出文件的确切大小,单位是 bytes。修改为 off,显示出文件的大概大小,单位是 kB 或者 MB 或者 GB。
autoindex_localtime on; #默认为 off,显示的文件时间为 GMT 时间;修改为 on,显示的文件时间为文件的服务器时间。
charset utf-8,gbk; #默认中文目录乱码,添加上解决乱码
1.1 配置文件
[root@web02-8 conf.d]# cat cxy.conf
server {
listen 80;
server_name game.cxytest.com;
location / {
root /cxy_code/www/;
index index.html;
}
location /download {
root /cxy_code/www/;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8,gbk;
}
}
1.2 创建下载目录
[root@web02-8 conf.d]# mkdir /cxy_code/www/download/cxy{1..10} -p
1.3 检查语法,重载nginx
[root@web02-8 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02-8 conf.d]# systemctl reload nginx
1.4 测试访问
二:基于ip的访问控制
2.1 拒绝某个ip访问
[root@web02-8 conf.d]# vim cxy.conf
server {
listen 80;
server_name game.cxytest.com;
location / {
root /cxy_code/www/;
index index.html;
deny 10.0.0.1;
allow all;
}
}
2.2 通过日志查看规则生效
[root@web02-8 conf.d]# tailf /var/log/nginx/error.log
2.2 只允许某个ip访问
[root@web02-8 conf.d]# vim cxy.conf
server {
listen 80;
server_name game.cxytest.com;
location / {
root /cxy_code/www/;
index index.html;
allow 10.0.0.1;
deny all;
}
}
三:基于用户的访问控制
参考官网的语法
3.1 需要安装http-tools工具生成用户名和密码
[root@web02-8 conf.d]# yum install -y httpd-tools
[root@web02-8 conf.d]# htpasswd -b -c /cxy_code/www/auth_conf www www
Adding password for user www
3.2 配置文件
[root@web02-8 conf.d]# vim cxy.conf
server {
listen 80;
server_name game.cxytest.com;
location / {
root /cxy_code/www/;
index index.html;
auth_basic "Input your user&&passwd";
auth_basic_user_file /cxy_code/www/auth_conf;
}
}
3.3 访问测试
四:访问限制
4.1 连接限制(tcp连接限制)
参考官网语法
4.1.1 配置文件
root@web02-8 conf.d]# vim cxy.conf
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
server {
listen 80;
server_name game.cxytest.com;
location / {
root /cxy_code/xiaoniao/;
index index.html;
limit_conn conn_zone 1; #同一时间只允许一个ip连接
}
}
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
参数说明:
$binary_remote_addr 变量长度是固定的4个字节
$remote_addr 变量长度固定是7-15个字节
而一个ip地址=32个bit=4个字节
这里设置成con_zone:10M代表
10M=(10*1024K=10*1024*1024KB)/4=2621440
大概可以存储2621440个ip
4.2 请求限制(http请求限制,使用请求限制效果最佳)
参考官网语法
4.2.1 配置文件
[root@web02-8 conf.d]# cat cxy.conf
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; #一秒钟最多允许ip一个连接
server {
listen 80;
server_name game.cxytest.com;
location / {
root /cxy_code/xiaoniao/;
index index.html;
limit_req zone=one burst=5; #突发量为5,超过5个之后访问错误代码
# limit_conn conn_zone 1;
}
}
4.2.2 使用ab工具测试
[root@web01 ~]# yum install httpd-tools -y
[root@web03-9 conf.d]# ab -n 50 -c 20 http://game.cxytest.com/index.html #-n 50代表发送50个请求;-c 20代表并发数,表示同时有20个用户发送请求
Concurrency Level: 20
Time taken for tests: 5.001 seconds
Complete requests: 50 #发起50次请求
Failed requests: 44 #失败了44次
五`:状态监控模板
参照官网说明
5.1 配置文件
server {
listen 80;
server_name game.cxytest.com;
location / {
root /cxy_code/xiaoniao/;
index index.html;
#limit_req zone=one burst=5;
# limit_conn conn_zone 1;
}
location = /basic_status {
stub_status;
access_log off;
}
}
5.2 测试访问
Active connections: 1 #当前活动的连接数
accepts 207 #当前的总连接数TCP
handled 207 #成功的连接数TCP
requests 664 #总的HTTP请求数
通过这个页面可以监控tcp长连接和短连接
修改主配置文件里的keepalive的时间为0
[root@web02-8 conf.d]# vim /etc/nginx/nginx.conf
keepalive_timeout 0;
再去查看监控状态页面会发现,每刷新一次网页,accepts和handled都会增加一次,这就是tcp短连接
六:日志模块
6.1 日志参数说明
$remote_addr # 记录客户端 IP 地址
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录 ISO8601 标准格式下的本地时间
$request # 记录请求的方法以及请求的 http 协议
$status # 记录请求状态码(用于定位错误信息)
$body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent # 发送给客户端的总字节数
$msec # 日志写入时间。单位为秒,精度是毫秒。
$http_referer # 记录从哪个页面链接访问过来的
$http_user_agent # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端 IP 地址
$request_length # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time # 请求花费的时间,单位为秒,精度毫秒
# 注:如果 Nginx 位于负载均衡器, nginx 反向代理之后, web 服务器无法直接获取到客 户端真实的 IP 地址。
# $remote_addr 获取的是反向代理的 IP 地址。 反向代理服务器在转发请求的 http 头信息中,
# 增加 X-Forwarded-For 信息,用来记录客户端 IP 地址和客户端请求的服务器地址。
系统默认的日志格式是
http {
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 /var/log/nginx/access.log main;
6.2 如果有多个server模块,建议区别不同目录的log日志
server {
listen 80;
server_name game.cxytest.com;
location / {
root /cxy_code/xiaoniao/;
access_log /cxy_code/log/xiaoniao_access.log main; #可以直接在对应的location里配置log文件以及目录
index index.html;
}
}
七:虚拟主机
环境:在一个台服务器上实现多个站点
基于ip的虚拟主机: ip不一样(不常用),不做配置
基于端口的虚拟主机: ip都一样,端口不一样
基于域名的虚拟主机: ip和端口都一样,域名不一样
7.1 基于端口的虚拟主机
[root@web02-8 conf.d]# cat www.conf
server {
listen 83;
server_name game.cxytest.com;
location / {
root /cxy_code/test/www/;
index index.html;
access_log /cxy_code/test/log/www/access.log main;
}
}
[root@web02-8 conf.d]# cat bbs.conf
server {
listen 81;
server_name game.cxytest.com;
location / {
root /cxy_code/test/bbs/;
index index.html;
access_log /cxy_code/test/log/bbs/access.log main;
}
}
[root@web02-8 conf.d]# cat blog.conf
server {
listen 82;
server_name game.cxytest.com;
location / {
root /cxy_code/test/blog/;
index index.html;
access_log /cxy_code/test/log/blog/access.log main;
}
}
#域名可以保持一样,也可以不一样
7.2 基于域名的虚拟主机
server {
listen 80;
server_name www.cxytest.com;
location / {
root /cxy_code/test/www/;
index index.html;
access_log /cxy_code/test/log/www/access.log main;
}
}
server {
listen 80;
server_name bbs.cxytest.com;
location / {
root /cxy_code/test/bbs/;
index index.html;
access_log /cxy_code/test/log/bbs/access.log main;
}
}
server {
listen 80;
server_name blog.cxytest.com;
location / {
root /cxy_code/test/blog/;
index index.html;
access_log /cxy_code/test/log/blog/access.log main;
}
}
八:location规则
8.1 location语法
location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
}
8.2 location优先级
常用的location是
~ 区分大小写正则匹配
~* 不区分大小写正则匹配
/ 通用匹配,任何请求都会匹配
8.3 location实际应用场景
8.3.1 通用匹配
location / {
}
8.3.2 严格区分大小写,匹配以.php结尾的都走这个location
location ~ \.php$ {
fastcgi_pass http://172.0.0.1:9000;
}
8.3.3 严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
proxy_pass http://127.0.0.1:8000;
}
8.3.4 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这个location
location ~* .*\.(jpg|gif|png|js|css)$ {
rewrite (.*) http://cdn.cxy.com$request_uri;
}
8.3.5 不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|git)$" {
default_type text/html;
return 403 "启用访问控制成功"
}