10人参与 • 2025-12-08 • Linux
location /admin/ {
allow 192.168.1.100; # 仅允许该ip访问
allow 10.0.0.0/24; # 或允许整个网段
deny all; # 其他全部拒绝
}http {
geo $block_ip {
default 0;
1.2.3.4 1;
5.6.7.0/24 1;
}
server {
if ($block_ip) {
return 403;
}
...
}
}geo模块适合大规模黑名单。
location /private/ {
deny all;
}location /api/ {
if ($request_method !~ ^(get|post)$) {
return 405;
}
}location /api/ {
if ($arg_token = "") {
return 403;
}
}location /secure/ {
auth_basic "restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}.htpasswd 文件可用 htpasswd 工具生成。location /static/ {
valid_referers none blocked *.example.com;
if ($invalid_referer) {
return 403;
}
}if ($http_user_agent ~* "curl|bot|spider") {
return 403;
}server {
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_client_certificate /etc/nginx/ssl/ca.crt;
ssl_verify_client on;
...
}适用于金融、政企等高安全场景。
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=5r/s;
location /api/ {
limit_req zone=req_limit burst=10 nodelay;
}limit_conn_zone $binary_remote_addr zone=conn_limit:10m; limit_conn conn_limit 10;
需安装第三方模块(如 ngx_http_geoip_module),可按国家/地区控制访问:
geoip_country /usr/share/geoip/geoip.dat;
map $geoip_country_code $allowed_country {
default 0;
cn 1; # 仅允许中国
}
server {
if ($allowed_country = 0) {
return 403;
}
}location /api/admin/ {
allow 127.0.0.1;
deny all;
}
location ~* /api/(delete|update)/ {
if ($request_method != "post") {
return 405;
}
}可以根据业务逻辑、数据库、redis等动态判断是否允许访问。
http {
geo $block_ip {
default 0;
1.2.3.4 1;
5.6.7.0/24 1;
}
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
server {
listen 80;
server_name www.example.com;
if ($block_ip) { return 403; }
location /admin/ {
allow 192.168.1.100;
deny all;
auth_basic "admin";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /static/ {
valid_referers none blocked *.example.com;
if ($invalid_referer) { return 403; }
}
location /api/ {
limit_req zone=req_limit burst=20 nodelay;
if ($request_method !~ ^(get|post)$) { return 405; }
if ($arg_token = "") { return 403; }
}
}
}示例:
http {
geo $block_ip { ... }
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=20r/s;
server {
if ($block_ip) { return 403; }
location /admin/ {
allow 10.0.0.0/8;
deny all;
}
location /api/ {
limit_req zone=req_limit burst=10 nodelay;
}
}
}建议高优先级规则(如黑名单)放在上层,局部规则覆盖全局。
常用于api签名、token、特定应用接入。
location /api/secure/ {
if ($http_x_token != "your-secret-token") {
return 403;
}
}if ($http_origin !~* ^https://trusted\.example\.com$) {
return 403;
}适用于限时活动、夜间维护、灰度发布等。
map $time_iso8601 $block_time {
default 0;
~t02:..:.. 1; # 02点整点到02:59:59 拒绝
}
server {
if ($block_time) {
return 403;
}
}也可用 lua/定时脚本实现更复杂的时间控制。
map $cookie_abtest $upstream_group {
default "old";
"a" "new";
}
server {
location / {
proxy_pass http://$upstream_group;
}
}可结合 lua 实现更灵活的灰度策略。
示例:
access_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
red:connect("127.0.0.1", 6379)
local is_blocked = red:get("ip:" .. ngx.var.remote_addr)
if is_blocked == "1" then
ngx.exit(403)
end
}log_format rejectlog '$remote_addr $request $status $http_user_agent $time_local'; access_log /var/log/nginx/reject.log rejectlog if=$access_denied;
$access_denied 可用 map/lua 动态赋值。
include /etc/nginx/whitelist.conf; include /etc/nginx/blacklist.conf;
location /admin/ {
allow 10.0.0.0/8;
deny all;
auth_basic "admin only";
auth_basic_user_file /etc/nginx/.htpasswd;
}limit_req_zone $binary_remote_addr zone=api_limit:10m rate=5r/s;
location /api/ {
limit_req zone=api_limit burst=20 nodelay;
if ($http_x_sign = "") { return 403; }
}location /static/ {
valid_referers none blocked *.example.com;
if ($invalid_referer) { return 403; }
limit_rate 100k;
}到此这篇关于nginx 访问控制的多种方法的文章就介绍到这了,更多相关nginx 访问控制内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论