【发布时间】:2015-11-07 03:36:44
【问题描述】:
我有一个使用 gunicorn 提供的烧瓶应用程序,上面有 nginx。我想使用基本身份验证(用户/密码)来保护所有以/admin 开头的网址,这是后台,但仍继续使用 gunicorn 提供所有其他网址,无需密码。
这是我当前的 nginx 配置:
server {
listen 80;
server_name example.com;
charset utf-8;
location / {
proxy_pass http://localhost: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 {
auth_basic "Administrator Login";
auth_basic_user_file /home/app/.htpasswd;
# the following four directives are duplicated :(
proxy_pass http://localhost: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;
}
}
如果我没有在第二个位置块中复制 proxy_* 指令,那么以 /admin 开头的 URL 不会被转发到 gunicorn,我会得到 404。
有什么办法可以避免重复配置?我尝试了位置嵌套,但显然最后 nginx 只“执行”了一个位置块。
【问题讨论】:
标签: nginx flask reverse-proxy basic-authentication