当前位置: 代码网 > it编程>数据库>Mysql > 在Nginx中如何为页面配置用户名密码认证访问

在Nginx中如何为页面配置用户名密码认证访问

2025年06月08日 Mysql 我要评论
在nginx中为页面配置用户名密码认证访问1. 安装 htpasswd 工具生成密码文件的工具 htpasswd 位于 apache 工具包中,按系统安装:# debian/ubuntu 系统sudo

在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. 访问验证

提示需要输入用户名/密码

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com