我想使用 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_basic
和 auth_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/