Nginx 反向代理需要在 htpasswd 结束时使用 }

Das*_*ash 2 proxy nginx

在线生成 htpasswd 文件后,nginx 给我错误“nginx: [emerg] 意外的文件结尾,在 /etc/nginx/sites-enabled/htpasswd:2 中需要“;”或“}”。奇怪的是,只有一行。我的配置文件如下:

server {
    listen 80;

    index index.html
    auth_basic "User: (username) Password: (password)";
    auth_basic_user_file htpasswd;
    location / {
         proxy_pass http://localhost:8080;
    }
}
Run Code Online (Sandbox Code Playgroud)

我很好奇这是为什么。谢谢。

编辑:我的htpasswd内容如下:xxx:$apr1$xxx

小智 6

您将 htpasswd 放在 nginx 的启用站点的目录中。
在nginx.conf中,大概有这么一行:

include /etc/nginx/sites-enabled/*;
Run Code Online (Sandbox Code Playgroud)

因此,该目录中的所有文件都被视为 nginx 配置文件。内容与 nginx 配置无关,因此您会收到该错误。
您需要将该文件放在其他地方,并在 auth_basic_user_file 选项中使用正确的路径。