只针对给定位置的Nginx auth

我使用Nginx作为python WSGI web应用程序的反向代理。

它看起来像这样:

location / { #auth_basic "Administrator Login"; #auth_basic_user_file /var/www/static/.htpasswd; proxy_pass http://mywebapp_gunicorn; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } 

在Web应用程序内部,我有一些pipe理员页面,我希望得到很好的保护,所以现在我在Web应用程序中使用一些authentication来保护他们,我想添加Nginxauthentication。

如何激活:

  auth_basic "Administrator Login"; auth_basic_user_file /var/www/static/.htpasswd; 

对于path: /managers ,但不适用于所有其他URL。

您只需要当前拥有的位置块之前添加另一个位置块,以匹配要保护的url。

 location /managers { auth_basic "Administrator Login"; auth_basic_user_file /var/www/static/.htpasswd; proxy_pass http://mywebapp_gunicorn; proxy_redirect off; 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 / { proxy_pass http://mywebapp_gunicorn; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } 

因为它在/之前,它将被优先用于path/pipe理者。