3

Nginx代理配置只允许指定IP访问

 1 year ago
source link: https://blog.51cto.com/u_15430445/5996775
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Nginx代理配置只允许指定IP访问

精选 原创

码农小宋 2023-01-08 19:04:08 ©著作权

文章标签 IP 子网掩码 Nginx 文章分类 Java 编程语言 yyds干货盘点 阅读数155

Nginx代理配置只允许指定IP访问_Nginx

在使用Nginx时,有时只想要指定的IP进行访问,其余IP都需要禁止,今天就来说一下,Nginx如何配置允许、禁止指定Ip访问

location / {
index index.jsp;
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
deny 192.168.1.1;
allow 127.0.0.0/50;
allow 173.56.0.0/25;
allow 132.16.0.0/25;
allow 12.170.0.0/25;
deny all;
}

注意事项:

  1. deny 

    一定要加一个ip,否则直接跳转到403,不往下执行了;如果403默认页是同一域名下,会造成死循环访问;

  2. allow的ip段

    从允许访问的段位从小到大排列,如:127.0.0.0/50

    下面才能是:12.170.0.0/25

    24表示子网掩码:255.255.255.0

    16表示子网掩码:255.255.0.0

    8表示子网掩码:255.0.0.0

  3. deny all;结尾

    表示除了上面allow的其他都禁止


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK