在Nginx中如何为页面配置用户名密码认证访问
作者:遇见火星
这篇文章主要介绍了在Nginx中如何为页面配置用户名密码认证访问的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
在Nginx中为页面配置用户名密码认证访问
1. 安装 htpasswd 工具
生成密码文件的工具 htpasswd
位于 Apache 工具包中,按系统安装:
# Debian/Ubuntu 系统 sudo apt-get install apache2-utils # CentOS/RHEL 系统 sudo yum install httpd-tools
2. 创建用户名密码文件
运行以下命令生成密码文件(保存在 /home/application/nginx/.htpasswd
):
htpasswd -c /home/application/nginx/.htpasswd pidin New password: Re-type new password: Adding password for user pidin
按提示输入密码,文件将包含加密后的凭证。
3. 配置 Nginx
#chromium server { listen 443 ssl; server_name chromium.srebro.cn; ##替换成自己的域名 error_page 404 /404/404.html; charset utf-8; ssl_certificate /home/application/nginx/cert/srebro.cn.pem; ssl_certificate_key /home/application/nginx/cert/srebro.cn.key; ssl_session_cache shared:SSL:1m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { auth_basic "Restricted Access"; # 认证提示标题 auth_basic_user_file /home/application/nginx/.htpasswd; # 指向密码文件 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header X-Real-IP $remote_addr; proxy_set_header x-wiz-real-ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://localhost:3010; } }
4. 访问验证
提示需要输入用户名/密码
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。