Nginx常用模块
Nginx常用模块
nginx目录索引模块
ngx_http_autoindex_module模块处理字符以字符/ 结尾的请求。当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。
[root@web01 ~]# vim /etc/nginx/conf.d/zh2.conf
server{
# 监听端口
listen 8081;
# 域名 (ip,localhost,_,域名)
server_name 10.0.0.7;
# uri
location /{
# 站点目录
root /game/zh2;
# 站点目录索引模块 开启
autoindex on;
# 显示带单位大小
autoindex_exact_size off;
# 显示本地时间
autoindex_localtime on ;
# 目录索引页面格式(默认html)
# autoindex_format json;
allow 10.0.0.1;
deny all;
windows启动telnet命令
Nginx状态模块
[root@web01 ~]# vim /etc/nginx/conf.d/zh2.conf
server{
listen 8081;
server_name 10.0.0.7;
location /{
root /game/zh2;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on ;
allow 10.0.0.1;
deny all;
}
location /jiankon{
stub_status;
}
}
Nginx访问控制模块
基于用户密码
# 安装htpasswd
[root@web02 ~]# yum -y install httpd
# 创建存放认证文件的目录
[root@web02 ~]# mkdir /etc/nginx/auth
# 创建认证文件
[root@web02 ~]# htpasswd -b -c /etc/nginx/auth/zh_auth zh 123
# 查看认证文件内容
[root@web02 ~]# cat /etc/nginx/auth/zh_auth
zh:$apr1$BZGw4rSv$5hVT0Wz0AC6a6j1AZzXYV0
# 修改nginx配置文件,添加认证
[root@web02 ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
auth_basic "zh:123" ;
auth_basic_user_file /etc/nginx/auth/zh_auth;
root /usr/share/nginx/html;
index index.html index.htm;
}
}
-b :允许命令输入密码
-c :创建一个新文件,将用户名和密码保存到指定文件中
[root@web02 ~]# htpasswd -b /etc/nginx/auth/zh_auth zh2 123
Adding password for user zh2
[root@web02 ~]# cat /etc/nginx/auth/zh_auth
zh:$apr1$BZGw4rSv$5hVT0Wz0AC6a6j1AZzXYV0
zh2:$apr1$yQJpYaro$8Lq3n6oRlhWYYTnCBftwf1
nginx默认路径: /etc/nginx
基于IP访问控制
[root@web02 ~]# !vim
vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
auth_basic "zh:123" ;
auth_basic_user_file /etc/nginx/auth/zh_auth;
root /usr/share/nginx/html;
index index.html index.htm;
deny all
}
}
[root@web02 ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
auth_basic "zh:123" ;
auth_basic_user_file /etc/nginx/auth/zh_auth;
root /usr/share/nginx/html;
index index.html index.htm;
allow 10.0.0.1 ;
deny all ;
}
}
## 注意 :默认nginx是alow all;如果只允许某一个IP需要配合deny all使用,deny all需要放到最下面
curl http://用户名:密码@10.0.0.7
访问频率限制
连接频率限制
[root@web02 ~]# vim /etc/nginx/conf.d/default.conf
limut_conn_zone $remote_addr zone=zh:10m;
server {
listen 80;
limit_conn zh 1;
server_name localhost;
location / {
auth_basic "zh:123" ;
auth_basic_user_file /etc/nginx/auth/zh_auth;
root /usr/share/nginx/html;
index index.html index.htm;
allow 10.0.0.1 ;
deny all ;
}
}
请求频率限制
# http标签段定义请求限制,rate限制速率,限制一秒最多一个请求
[root@web02 ~]# vim /etc/nginx/conf.d/default.conf
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s ;
server {
listen 80;
server_name localhost;
# 1r/s只接收一个请求,其余请求拒绝处理并返回错误码给客户端
# 请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量,多余的请求返回503
limit_req zone=req_zone burst=3 nodelay;
location / {
auth_basic "zh:123" ;
auth_basic_user_file /etc/nginx/auth/zh_auth;
root /usr/share/nginx/html;
index index.html index.htm;
allow 10.0.0.1 ;
deny all ;
}
}
# 优化页面
[root@web01 ~]# vim /etc/nginx/conf.d/zh2.conf
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s ;
server{
listen 8081;
server_name 10.0.0.7;
limit_req zone=req_zone burst=3 nodelay;
limit_req_status 508 ;
error_page 508 /508.html ;
location /{
root /game/zh2;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on ;
allow 10.0.0.1;
deny all;
}
location /jiankon{
stub_status;
auth_basic "sfas";
auth_basic_user_file /etc/nginx/auth/zh_auth;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!