nginx-虚拟主机/日志配置/模块

nginx虚拟主机
🌹 1、基于多ip的方式
[root@web03 /etc/nginx/conf.d]# vim game3.conf

[root@web03 /etc/nginx/conf.d]# nginx -t
[root@web03 /etc/nginx/conf.d]# systemctl restart nginx
🌹 2、基于多端口的方式
[root@web03 /etc/nginx/conf.d]# vim game3.conf

[root@web03 /etc/nginx/conf.d]# nginx -t
[root@web03 /etc/nginx/conf.d]# systemctl restart nginx
🌹 3、基于多域名的方式
[root@web03 /etc/nginx/conf.d]# vim game3.conf

[root@web03 /etc/nginx/conf.d]# nginx -t
[root@web03 /etc/nginx/conf.d]# systemctl restart nginx
nginx日志
nginx日志位置: /var/log/nginx/,有access.log(访问日志)和error.log(错误日志)
$remote_addr 客户端地址
$remote_user 客户端用户名称
[$time_local] 访问时间和时区
$request 请求的url和http协议
$status http请求状态
$body_bytes_sent 发送给客户端的文件内容的大小
$http_referer url跳转来源 (客户端ip)
$http_user_agent 用户终端浏览器等信息
$http_x_forwarded_for 真实的客户端ip(在反向代理中生效)
模块
一、nginx访问控制模块
🌹 ngx_http_access_module
🐱 官网地址:http://nginx.org/en/docs/http/ngx_http_access_module.html
⚠️详情:
允许火拒绝某些ip访问
deny:拒绝
allow:允许
⚠️案例:
1、允许192.168.15.1 访问,不允许其他ip访问
allow 192.168.15.1;
deny all;
2、允许192.168.15.0这个望断,不允许其他网段访问
allow 192.168.15.0/24;
deny all;
🌹 ngx_http_auth_basic_module
🐱 官网地址:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
⚠️详情:
访问之前需要登入
1、安装httpd-tools工具
[root@web03 /var/log]# yum install httpd-tools -y
2、生成用户名密码文件
[root@web03 /var/log]# htpasswd -c /etc/nginx/auth judy
New password:
Re-type new password:
Adding password for user judy
3、将文件路径加入nginx配置
[root@web03 /var/log]# vim /etc/nginx/conf.d/game3.conf

4、[root@web03 /var/log]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5、重启
[root@web03 /var/log]# systemctl restart nginx
🌹 ngx_http_autoindex_module
🐱 官网地址:http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
⚠️ 详情:
展示目录索引 打开nginx.conf文件,在location server或http段加入
autoindex on;
表示展示索引开启
autoindex_exact_size on;
默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是KB或MB或GB
autoindex_localtime on;
默认为off,显示的时间为GMT时间
改为on后,显示文件时间为文件的服务器时间
autoindex_format json;
⚠️ 案例
1、创建目标文件夹
[root@web03 /etc/nginx/conf.d]# mkdir /opt/test
2、创建目标文件
[root@web03 /etc/nginx/conf.d]# cd /opt/test/
[root@web03 /opt/test]# touch test{1..3}.txt
3、编辑配置文件
[root@web03 /opt/test]# vim test.conf

4、[root@web03 /opt/test]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5、重启
[root@web03 /opt/test]# systemctl restart nginx
6、测试

🐖:其他参数也可以加上,是一样的操作
二、nginx状态监控模块
🌹 ngx_http_stub_status_module
🐱 官网地址:http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
⚠️案例
1、编辑配置文件
[root@web03 /etc/nginx/conf.d]# vim test.conf

2、nginx -t 测试
3、重启
4、输入网址测试

三、nginx访问链接控制模块
🌹 ngx_http_limit_conn_module <根据定义的key限制某个IP的链接数量>
🐱 官网地址:http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html
🍀 配置:limit_conn_zone $remote_addr zone=addr:10m;
《说明:定义key(zone=addr)为addr,分配大小为10M(zone=addr:10M)》
🐖:修改配置文件

🌹 ngx_http_limit_req_module <nginx限制请求数,限制访问两>
limit_req_zone 声明链接池
$remote_addr 变量
zone=one:10m 名称:链接池的大小
rate=1r/s 速率
⚠️案例
要求每秒只能有一个访问
修改配置文件

⚠️测试
1、安装ab命令测试
-bash: limit_req_zone: 未找到命令
[root@web03 /etc/nginx/conf.d]# yum install http-tools -y
2、ab参数
-n 总共访问多少次
-c 每次访问多少个
[root@web03 /etc/nginx/conf.d]# ab -c 10 -n 100000 http://网址
浙公网安备 33010602011771号