How can you configure nginx to restrict access by IP?
To restrict IP access, you can use the allow and deny directives in the Nginx configuration file. Here is an example configuration:
http {
server {
listen 80;
server_name example.com;
location / {
allow 192.168.0.0/24; # 允许IP范围访问
deny all; # 禁止所有其他IP访问
# 其他配置项...
}
}
}
In the example above, the allow directive is used to specify the range of IP addresses that are allowed access, and deny all is used to block access to all other IPs. The order and content of the allow and deny directives can be adjusted as needed.
After configuring, be sure to use the command “nginx -s reload” to reload the Nginx configuration and make it take effect.