在Nginx中如何為頁面配置用戶名密碼認(rèn)證訪問
更新時間:2025年06月07日 09:27:24 作者:遇見火星
這篇文章主要介紹了在Nginx中如何為頁面配置用戶名密碼認(rèn)證訪問的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
在Nginx中為頁面配置用戶名密碼認(rèn)證訪問
1. 安裝 htpasswd 工具
生成密碼文件的工具 htpasswd 位于 Apache 工具包中,按系統(tǒng)安裝:
# Debian/Ubuntu 系統(tǒng) sudo apt-get install apache2-utils # CentOS/RHEL 系統(tǒng) sudo yum install httpd-tools
2. 創(chuàng)建用戶名密碼文件
運行以下命令生成密碼文件(保存在 /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"; # 認(rèn)證提示標(biāo)題
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. 訪問驗證
提示需要輸入用戶名/密碼

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
linux(centos5.5)/windows下nginx開啟phpinfo模式功能的配置方法分享
某站點用到結(jié)合phpinfo功能的urlrewrite,在nginx中需要在nginx.conf文件中進(jìn)行配置才可支持phpinfo2013-02-02
nginx通過location配置代理的原理和實現(xiàn)方式
這篇文章主要介紹了nginx通過location配置代理的原理和實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
淺析nginx剛剛發(fā)布的JavaScript能力nginScript
Nginx [engine x]是全球最受歡迎,也是最優(yōu)秀的web服務(wù)器、反向代理服務(wù)器。nginScript是JavaScript/ECMAscript的子集,nginScript不是通過V8引擎實現(xiàn)的。本文給大家介紹nginx剛剛發(fā)布的JavaScript能力nginScript,感興趣的朋友跟著小編一起了解了解吧2015-09-09
Nginx+keepalived實現(xiàn)七層的負(fù)載均衡的高可用(最新解決方案)
這篇文章主要介紹了Nginx+keepalived實現(xiàn)七層的負(fù)載均衡的高可用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03

