python - 具有双重授权的 gunicorn 的 Nginx

标签 python nginx gunicorn

我想使用 http auth,但也想使用 gunicorn 的反向代理。

对于 http 身份验证,我使用:

location = admin.html {
 auth_basic 'Login Required'
 auth_basic__use_file etc/nginx/.htpasswd;
}

对于 gunicorn,我发现的反向代理:

try_files $uri @gunicorn;

我怎样才能将两者结合起来?

最佳答案

你的意思是你想使用 nginx 作为 django 的反向代理服务器,并具有额外的授权级别?您只需将 auth_basicauth_basic_user_file 指令从 location block 移动到 server block :

upstream gunicorn_server {
    server unix:</path/to/socket/pseudo/file>;
}

server {
    listen ...;
    server_name ...;
    auth_basic "Login Required";
    auth_basic_user_file etc/nginx/.htpasswd;
    ... # other parameters
    location / {
        try_files $uri @gunicorn;
    }
    location @gunicorn {
        proxy_pass http://gunicorn_server;
    }
}

更新

假设有一个“管理”区域,其中包含 /admin.html/admin/any/other/uri 以使用 HTTP Basic Auth 额外保护该区域您可以使用以下配置:

upstream gunicorn_server {
    server unix:</path/to/socket/pseudo/file>;
}

server {
    listen ...;
    server_name ...;
    ... # other parameters
    location / {
        try_files $uri @gunicorn;
    }
    location /admin {
        auth_basic "Login Required";
        auth_basic_user_file etc/nginx/.htpasswd;
        try_files $uri @gunicorn;
    }
    location @gunicorn {
        proxy_pass http://gunicorn_server;
    }
}

要保护单个文件 admin.html,请将 location/admin { 替换为 location =/admin.html {

关于python - 具有双重授权的 gunicorn 的 Nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53601744/

相关文章:

python - 将 Pandas 对象展平到列

python - 对数据 Pandas 进行操作

python - 如何在 Python 中使用 Flask 执行周期性任务

python - Flask-Bokeh-Gunicorn : Models must be owned by a single document

python - ascii 与 MYSQL 的 unicode 中的 SQLAlchemy 大字符串

tomcat - 在 SERVERPILOT 配置的环境中设置 tomcat

php - Docker 没有 MySQL 连接到 Nginx

nginx - nginx大量端口转发

django - 运行 nginx 的 django 服务器上的大型媒体文件出现 403 错误

python - 从 Flask 应用程序中访问 SSL 证书信息