CentOS7+Virtualmin+Nginx环境下如何设置网站仅内网访问/离线模式?
Hey Eric, since you're using Virtualmin with Nginx on CentOS 7, here are a few reliable, straightforward ways to restrict your site to internal-only access or put it into offline mode to block external crawlers like Google:
方法1:通过Nginx配置直接限制内网访问
This is the most direct approach, as it leverages Nginx's core access control capabilities:
- Log into Virtualmin, navigate to your target website, then go to Services > Configure Website > Edit Directives (this lets you edit raw Nginx config for the site).
- Inside the
serverblock, add these rules (replace the IP ranges with your actual internal network segments, e.g.,10.0.0.0/8for a typical private network):# Allow internal IPs and local loopback allow 192.168.0.0/24; allow 127.0.0.1; # Block all other external traffic deny all; - Save the changes, then restart Nginx to apply them:
sudo systemctl restart nginx - Test it out: External devices should get a 403 Forbidden error, while internal devices can access the site normally.
方法2:设置离线(维护)模式 + 内网限制
If you want to show a friendly maintenance page to external visitors while keeping access open internally:
- First, create a simple maintenance page (save it to your site's public directory, e.g.,
/home/yourdomain/public_html/maintenance.html):<!DOCTYPE html> <html> <head> <title>Site Under Maintenance</title> <style>body { text-align: center; margin-top: 50px; font-family: Arial; }</style> </head> <body> <h1>We're Performing Maintenance</h1> <p>Our site will be back online soon. Thank you for your patience!</p> </body> </html> - Go back to the Nginx directive editor in Virtualmin, and add these rules to the
serverblock:# Allow internal IPs only allow 192.168.0.0/24; allow 127.0.0.1; deny all; # Serve maintenance page for blocked requests error_page 403 /maintenance.html; location = /maintenance.html { root /home/yourdomain/public_html; internal; # Prevent direct access to this page from external sources } # Block crawlers even if they slip through (extra layer) add_header X-Robots-Tag "noindex, nofollow" always; - Restart Nginx with
sudo systemctl restart nginx— external visitors will see your maintenance page, and internal users get full access.
方法3:Use Virtualmin's Graphical Access Control (No Manual Config)
If you prefer a point-and-click interface instead of editing config files:
- In your website's Virtualmin dashboard, go to Security > Access Control.
- In the Allowed IP addresses field, add your internal network range(s) (e.g.,
192.168.0.0/24) plus127.0.0.1. - In the Denied IP addresses field, type
allto block everything else. - Click Save — Virtualmin will automatically update the Nginx config and restart the service for you.
内容的提问来源于stack exchange,提问作者Eric
相关产品推荐
相关产品推荐

