在django中用nginx和gunicorn限制ip的访问权限
我正在通过在nginx中使用简单的基于主机的访问控制来限制对django应用程序的管理员部分的访问。
不幸的是,nginx似乎不遵守配置请求:
I am trying to restrict access to the admin section of my django app by using simple host-based access control in nginx. Unfortunately nginx does not seem to abide by the configuration request:
这是我在nginx中这个特定部分的设置:
this is my setting for this particular section in nginx:
# gunicorn setup
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /admin/ { # restrict access to admin section
allow 192.168.0.1;
deny all;
}
这仍然阻止我的ip 192.168.0.1。
我做错了什么?
是否有另一种方式阻止访问django应用程序的/ admin / section?
This does still block my ip 192.168.0.1. What am I doing wrong? Is there another way to block access to the /admin/ section of a django app?
我发现通过以下方式替换/ admin / location来解决此问题:
I found a solution to this problem by replacing the /admin/ location with the following:
location ^~ /admin/ { # restrict access to admin section
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
allow 192.168.0.1;
deny all;
}
我希望这可以在互联网上保存一些长时间的搜索。
我会感谢提供更好的解决方案的答案。
I hope this will save someone some long searches on the internet. I would appreciate answers offering a better solution.