Nginx basic authentification
标签(空格分隔): nginx
资料:nginx的认证资料
我们某一个web服务,特定ip/域名 + 端口 需要一定的权限才能浏览。我们需要验证,基础的nginx basic authentification验证就是其中一种方式。
准备工作:
1, nginx 镜像。
2,nginx 配置文件,我们命名为nginx.conf,我们将容器的8080端口暴露出去,host访问8080端口需要验证,验证成功之后重定向到容器内部的localhost:80,然后返回给用户。也就是说用户访问localhost:8080端口验证成功之后,返回的才是localhost:80的内容[也就是localhost的默认页面]。
user nginx;
worker_processes 1;
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 {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
auth_basic "nginx basic auth";
auth_basic_user_file /etc/nginx/conf.d/pass.db;
proxy_set_header X-PROXY-USER $remote_user;
proxy_pass http://localhost:80;
}
}
}
3,启动容器,将本地的nginx配置文件挂载到容器内
docker run -d -p 8080:8080 -v D:\nginx.conf:/etc/nginx/nginx.conf --name my-nginx nginx
4, 基础的认证需要用户名和密码,用户名和密码有多种方式生成。我们在nginx配置的存储用户名和密码的文件是/etc/nginx/conf.d/pass.db
我们进入容器
docker exec -it my-nginx /bin/bash
查看没有这个文件,我们使用apache的htpasswd生成一个即可。安装工具之前更新apt。
apt-get update
apt install apache2-utils
cd /etc/nginx/conf.d
htpasswd -c pass.db kevin #会提示你输入密码
然后就会生成pass.db文件,文件里面存储了账户和密码。
然后重启容器。访问localhost:8080即可。