NGINX 封禁 IP(IP 段)访问方法:修改配置文件或 .htaccess 文件

方法一:修改 Nginx 配置文件 nginx.conf

Nginx 配置访问 IP 可以修改 nginx.conf 文件,只需要在 server 中添加 allow 和 deny 的 IP 即可,如下:

server {
    listen       80;
    server_name  localhost;
    allow all;
    deny 123.123.123.123;
    error_page   500 502 503 504  /50x.html;
}

注意!上面的配置中 allow 必须在 deny 的前面配置,不然 allow 不生效。

Nginx 中允许或者拒绝某个 IP 访问是这么写的:

allow 192.168.0.1;
deny  192.168.0.2;

如果你需要对整个 IP 段设置不允许访问,可以这么写:

deny 123.0.0.0/8;    // 封 123.0.0.1~123.255.255.254 这个段的ip
deny 123.123.0.0/16;   // 封 123.123.0.1~123.123.255.254 这个段的ip
deny 123.123.123.0/24;   // 封 123.123.123.1~123.123.123.254 这个段的ip

deny all;  // 封所有ip

说明,allow 跟 deny 配置相同,如果需要开放某个 IP 段,只需要把上面的 deny 改成 allow

另外需要注意,该配置修改需要重启 Nginx 服务器才能生效。

方法二:修改网站配置文件 .htaccess

除了修改 Nginx 配置文件之外,还可以通过修改网站配置文件 .htaccess 来限制某 IP(段)访问,方法如下。

将以下内容保存为 .htaccess 文件,上传至网站根目录。如果原来已经存在 .htaccess 文件的,将以下内容加到该文件开头。

Order allow,deny
allow from all
deny from 123.123.123.123

如果要限制一个 IP 段,比如要限制 123.123.123.* 这个 C 段,则输入:

Order allow,deny
allow from all
deny from 123.123.123

以上内容表示拒绝 123.123.123 开头的所有 IP,其他 IP 允许。

如果要限制多个 IP 段,中间用空格隔开,如:

Order allow,deny
allow from all
deny from 111.111.111 111.222.222 111.333.333

说明,该配置不用重启 web 服务器,修改立即生效。

此外,该方法适用任何 web 网站,例如 IIS、Apache 网站。

sanradar
sanradar
文章: 104

76 评论

  1. You really make it appear really easy together with your
    presentation but I to find this topic to be really one thing which I
    feel I would never understand. It kind of feels too complex and very broad for me.
    I am looking forward to your next submit, I’ll try to get the cling of it!
    Escape room lista

  2. An interesting discussion is definitely worth comment. I do believe that you should write more on this subject matter, it might not be a taboo matter but generally people do not talk about these subjects. To the next! Best wishes.

  3. Hi there! I simply wish to give you a big thumbs up for your great information you’ve got right here on this post. I am returning to your website for more soon.

  4. Oh my goodness! Impressive article dude! Thank you, However I am having issues with your RSS. I don’t understand why I am unable to join it. Is there anybody getting similar RSS issues? Anybody who knows the answer can you kindly respond? Thanx.

  5. An outstanding share! I’ve just forwarded this onto a coworker who has been doing a little research on this. And he in fact ordered me lunch due to the fact that I discovered it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanx for spending some time to discuss this topic here on your blog.

  6. May I just say what a comfort to find someone that really understands what they are talking about over the internet. You actually realize how to bring a problem to light and make it important. More people have to check this out and understand this side of the story. It’s surprising you are not more popular since you surely have the gift.

  7. Oh my goodness! Amazing article dude! Thanks, However I am going through problems with your RSS. I don’t know the reason why I am unable to subscribe to it. Is there anyone else having similar RSS issues? Anyone that knows the answer will you kindly respond? Thanks!

  8. This is the perfect web site for anybody who would like to understand this topic. You realize a whole lot its almost tough to argue with you (not that I actually would want to…HaHa). You definitely put a new spin on a topic which has been written about for decades. Excellent stuff, just wonderful.

  9. You’re so awesome! I don’t believe I’ve truly read through anything like that before. So nice to discover somebody with some genuine thoughts on this issue. Really.. thank you for starting this up. This site is one thing that is required on the web, someone with a little originality.

  10. Hello there! I could have sworn I’ve been to this blog before but after going through many of the posts I realized it’s new to me. Anyhow, I’m certainly pleased I found it and I’ll be bookmarking it and checking back frequently!

  11. I really love your website.. Great colors & theme. Did you develop this website yourself? Please reply back as I’m planning to create my very own site and would like to know where you got this from or what the theme is called. Kudos.

  12. Hi there! This post couldn’t be written much better! Going through this article reminds me of my previous roommate! He constantly kept talking about this. I will send this post to him. Fairly certain he’ll have a good read. I appreciate you for sharing!

  13. Greetings! Very useful advice within this article! It is the little changes that will make the most important changes. Many thanks for sharing!

  14. Everything is very open with a really clear explanation of the challenges. It was truly informative. Your website is very helpful. Thank you for sharing.

  15. I truly love your site.. Very nice colors & theme. Did you make this amazing site yourself? Please reply back as I’m attempting to create my own site and would love to know where you got this from or exactly what the theme is called. Appreciate it.

  16. After going over a handful of the blog articles on your site, I seriously like your technique of blogging. I book marked it to my bookmark webpage list and will be checking back soon. Please check out my web site too and let me know how you feel.

  17. After I originally commented I appear to have clicked on the -Notify me when new comments are added- checkbox and now every time a comment is added I receive four emails with the exact same comment. There has to be a way you are able to remove me from that service? Many thanks.

  18. I’m impressed, I must say. Rarely do I come across a blog that’s both educative and entertaining, and without a doubt, you’ve hit the nail on the head. The problem is something which not enough folks are speaking intelligently about. I am very happy that I came across this during my hunt for something relating to this.

  19. Hi! I just wish to offer you a big thumbs up for your excellent information you have here on this post. I will be returning to your blog for more soon.

  20. Having read this I believed it was very enlightening. I appreciate you finding the time and effort to put this content together. I once again find myself personally spending a lot of time both reading and posting comments. But so what, it was still worth it!

  21. Hi there! This blog post couldn’t be written much better! Looking through this post reminds me of my previous roommate! He continually kept talking about this. I will send this information to him. Fairly certain he’ll have a good read. I appreciate you for sharing!

  22. I’m more than happy to uncover this website. I want to to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and i also have you book marked to see new stuff on your site.

  23. When I originally commented I seem to have clicked on the -Notify me when new comments are added- checkbox and from now on every time a comment is added I recieve four emails with the same comment. Perhaps there is a means you are able to remove me from that service? Thank you.

  24. Good day! I could have sworn I’ve visited this website before but after browsing through a few of the articles I realized it’s new to me. Nonetheless, I’m certainly pleased I came across it and I’ll be book-marking it and checking back frequently.

  25. Right here is the perfect website for anybody who hopes to find out about this topic. You know a whole lot its almost tough to argue with you (not that I actually would want to…HaHa). You definitely put a new spin on a topic that has been written about for decades. Excellent stuff, just wonderful.

  26. Right here is the perfect blog for anybody who wants to find out about this topic. You realize so much its almost hard to argue with you (not that I really will need to…HaHa). You definitely put a brand new spin on a topic which has been discussed for decades. Great stuff, just great.

  27. I have actually struggled with blood sugar variations for several years,
    and it truly influenced my power levels throughout
    the day. Considering that beginning Sugar Protector,
    I feel more well balanced and alert, and I
    do not experience those afternoon plunges any longer!
    I like that it’s a natural service that works with no rough side effects.
    It’s truly been a game-changer for me

  28. After going over a handful of the blog posts on your website, I truly like your way of writing a blog. I added it to my bookmark webpage list and will be checking back in the near future. Please check out my website too and tell me what you think.

  29. Hi! Do you know if they make any plugins to assist with SEO?
    I’m trying to get my site to rank for some
    targeted keywords but I’m not seeing very good success. If you know of any please share.
    Thank you! You can read similar text here:
    Wool product

  30. Everything is very open with a clear description of the issues. It was definitely informative. Your website is extremely helpful. Thank you for sharing!

  31. An impressive share! I have just forwarded this onto a coworker who had been doing a little research on this. And he actually ordered me breakfast due to the fact that I found it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanks for spending some time to discuss this matter here on your site.

  32. I’m amazed, I have to admit. Rarely do I come across a blog that’s both equally educative and interesting, and let me tell you, you’ve hit the nail on the head. The issue is something too few men and women are speaking intelligently about. I’m very happy I came across this during my hunt for something relating to this.

  33. A fascinating discussion is definitely worth comment. I do believe that you need to write more on this issue, it may not be a taboo matter but usually folks don’t discuss these topics. To the next! Many thanks.

  34. Good post. I learn something new and challenging on blogs I stumbleupon on a daily basis. It’s always useful to read through content from other writers and use something from other sites.

  35. Hello there! This article couldn’t be written any better! Going through this article reminds me of my previous roommate! He continually kept talking about this. I’ll send this article to him. Pretty sure he’s going to have a great read. Thank you for sharing!

  36. Your style is so unique in comparison to other people I have read stuff from. I appreciate you for posting when you’ve got the opportunity, Guess I will just bookmark this page.

  37. That is a good tip particularly to those fresh to the blogosphere. Simple but very precise info… Thanks for sharing this one. A must read article.

  38. I truly love your website.. Excellent colors & theme. Did you create this site yourself? Please reply back as I’m looking to create my own personal blog and would like to learn where you got this from or just what the theme is named. Cheers.

  39. Hello! I could have sworn I’ve been to this website before but after reading through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be book-marking and checking back frequently!

  40. An impressive share! I have just forwarded this onto a coworker who has been conducting a little research on this. And he actually ordered me lunch due to the fact that I found it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanx for spending time to talk about this matter here on your site.

  41. I’m very happy to find this website. I need to to thank you for ones time due to this wonderful read!! I definitely appreciated every bit of it and I have you book marked to look at new information in your site.

  42. I blog frequently and I really thank you for your content. This great article has truly peaked my interest. I am going to book mark your blog and keep checking for new details about once a week. I subscribed to your RSS feed as well.

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注