隐藏版本号
123
http { server_tokens off;}
nginx Basic 认证
生成用户密码
1
htpasswd -c /usr/local/nginx/conf/nginx.passwd.db admin
更新密码
1
htpasswd /usr/local/nginx/conf/nginx.passwd.db admin
配置内容
1234
location / { auth_basic "View is cheap,show me the password!"; auth_basic_user_file nginx.passwd.db;}
限制$http_x_forwarded_for
12345678910
set $allow false;if ($http_x_forwarded_for = "185.199.111.153") { set $allow true;}if ($http_x_forwarded_for ~ "10.0.0.*") { set $allow true;}if ($allow = false) { return 404;}
限制请求方法
123
if ($request_method !~ ^(GET|POST)$ ) { return 405;}
过滤特殊UA
if ($http_user_agent ~* sqlmap|wget|curl) { return 403; }
图片防盗链
12345678910111213
location /images/ { valid_referers none blocked lengyuewusheng.com www.lengyuewusheng.com; if ($invalid_referer) { return 403; }}或location /images/ { valid_referers blocked lengyuewusheng.com www.lengyuewusheng.com; if ($invalid_referer) { rewrite ^/images/.*\.(gif|jpg|jpeg|png)$ /error.html last; }}
“Referer”请求头为指定值时,内嵌变量$invalid_referer被设置为空字符串, 否则这个变量会被置成”1”。查找匹配时不区分大小写。
valid_referers:验证referer
none: 允许referer为空,
blocked: 允许不带协议的请求.
配置反向代理
12345678910111213141516171819
http {... upstream realservers { server 10.0.0.1:8080 weight=3; server 10.0.0.2:8080 weight=7; } server { location /admin/ { proxy_pass http://realserver; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }}
nginx开启keepalive
1234567891011121314151617
upstream realserver { server 10.0.0.1:8080; keepalive 10; keepalive_timeout 60s; # This directive appeared in version 1.15.3. keepalive_requests 100; # This directive appeared in version 1.15.3.}http { keepalive_timeout 75s; server { location / { proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass http://realserver; ... } }}
keepalive: 指定每个nginx worker可以保持的最大连接数量为1024,默认不设置,即nginx作为client时keepalive未生效。
proxy_http_version 1.1: 开启keepalive要求HTTP协议版本为HTTP 1.1。
proxy_set_header Connection “”: 为了兼容老的协议以及防止http头中有Connection close导致的keepalive失效,这里需要及时清掉HTTP头部的Connection。
keepalive_requests: 设置可通过一个保持连接提供服务的最大请求数。在发出最大请求数后,连接将关闭。
keepalive_timeout:
upstream中的keepalive_timeoute是ngx_http_upstream_module模块中的功能,是1.15.3后的新特性,设置空闲保持连接的超时时间。
upstream外的keepalived是ngx_http_core_module模块中的功能,第一个参数是保持活动的客户机连接将在服务器端保持打开状态的超时时间。0表示禁用保持活动的客户端连接。可选的第二个参数在”Keep-Alive: timeout=time”响应头字段中设置一个值。两个参数可以不同。
nginx开启目录查看
1234567
server { location /download/ { autoindex on; autoindex_exact_size off; autoindex_localtime on; }}
浏览器不显示文件内容直接下载文件
123
if ($request_filename ~* ^.*?\.(txt|pdf)$) { add_header Content-Disposition 'attachment';}
nginx获取用户真实IP
1234567
set_real_ip_from 10.0.0.0/8; #真实服务器上一级代理的IP地址或者IP段,可以写多行。set_real_ip_from 192.168.0.0/16;real_ip_header X-Forwarded-For; #从哪个header头检索出要的IP地址。real_ip_recursive on; #递归的排除所配置中的可信IP。proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
X-Forwarded-For 变量,是squid开发的,用于识别请求通过了哪些HTTP代理或负载平衡器,记录相应IP地址列表的非rfc标准,如果设置了X-Forwarded-For,那么每次经过proxy转发请求后都会有记录。
nginx 强制跳转https
1234567
if ( $scheme = http ){ return 301 https://$server_name$request_uri;}或if ( $scheme = http ){ rewrite ^(.*)$ https://$host$1 permanent;}
防恶意抓取
1234567891011121314151617181920
http { geo $limited { default 1; 10.0.0.0/8 0; 123.207.144.220/32 0; 36.110.170.248/30 0; 118.184.170.248/30 0; } map $limited $limit { 1 $binary_remote_addr; 0 ""; } limit_req_zone $limit zone=reqids:100m rate=10r/s; server { limit_req zone=reqids burst=2 nodelay; }}
nginx健康检查机制
check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ] 返回指定HTTP code,符合预期就算检测成功
通过nginx查看txt文本文件乱码
文件后缀必须是.txt而不是.html,否则换行显示会有问题
123
server: default_type 'text/html'; charset utf-8,gbk;
nginx代理文件下载以及时间显示问题
1234
location / { autoindex on; autoindex_localtime on;}
nginx try_files
nginx location @
nginx缓存配置
nginx 根据uuid灰度
nginx限制并发连接数
nginx限制请求频率
nginx存活检测
nginx upstream backup机制
1234
rewrite_log on;proxy_ignore_client_abort on;expires epoch;
相关资料
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!