使用BASIC auth保护ngnix中的网站



嗨,我正在努力保护NGNIX上托管的网站。我的问题是,当我访问localhost时,我无法看到身份验证凭据对话框。

这是ngnx.conf

user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
worker_connections  1024;
}

http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log  /var/log/nginx/access.log  main;
sendfile        on;
#tcp_nopush     on;
keepalive_timeout  65;
#gzip  on;
include /etc/nginx/conf.d/*.conf;
server {
auth_basic          "Admin area";
auth_basic_user_file /etc/apache2/.htpasswd;
}
}

该站点托管在本地主机的docker容器中。这是码头文件

FROM nginx:alpine
RUN apk update
RUN apk add apache2-utils
WORKDIR /etc/apache2/
RUN htpasswd -c -B -b /etc/apache2/.htpasswd user1 password
## Remove default nginx index page
RUN rm -rf /usr/share/nginx/html/*
# copy the package.json to install dependencies
ADD root /usr/share/nginx/html/
ADD ngnix.conf /etc/nginx
EXPOSE 4200 80
ENTRYPOINT ["nginx", "-g", "daemon off;"]

问题是/etc/nginx/conf.d/中有另一个server配置,并且您将其包含在include /etc/nginx/conf.d/*.conf;中。将您的基本身份验证放入/etc/nginx/conf.d/default.conf或删除该文件。

此外,您的server块缺少listen指令,如listen 80;。请查看上述/etc/nginx/conf.d/default.conf中的基本server示例。

最新更新