在Nginx中如何為頁面配置用戶名密碼認證訪問
更新時間:2025年06月07日 09:27:24 作者:遇見火星
這篇文章主要介紹了在Nginx中如何為頁面配置用戶名密碼認證訪問的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
在Nginx中為頁面配置用戶名密碼認證訪問
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"; # 認證提示標題 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. 訪問驗證
提示需要輸入用戶名/密碼
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
nginx rewrite重寫規(guī)則與防盜鏈配置方法教程詳解
這篇文章主要介紹了nginx rewrite重寫規(guī)則與防盜鏈配置方法教程詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09