亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
? 1. Install apache2-utils (for htpasswd )
? 2. Create a Password File and Add a User
? 3. Configure Nginx to Use Basic Auth
? 4. Test and Reload Nginx
? Tips
首頁 運維 Nginx 設置NGINX基本身份驗證

設置NGINX基本身份驗證

Jul 27, 2025 am 02:33 AM

安裝apache2-utils獲取htpasswd工具;2. 使用htpasswd創(chuàng)建密碼文件並添加用戶;3. 在Nginx配置中指定auth_basic和密碼文件路徑;4. 測試並重載Nginx配置即可生效,完成基於用戶名密碼的訪問控制。

Setting Up Nginx Basic Authentication

Setting up Nginx basic authentication is a simple and effective way to restrict access to your web content using a username and password. It's commonly used for staging sites, internal tools, or protecting sensitive directories. Here's how to do it step by step:

Setting Up Nginx Basic Authentication

? 1. Install apache2-utils (for htpasswd )

Even though it's from Apache, the htpasswd tool is widely used to generate password files for HTTP basic auth — including with Nginx.

On Ubuntu/Debian:

Setting Up Nginx Basic Authentication
 sudo apt update
sudo apt install apache2-utils

On CentOS/RHEL:

 sudo yum install httpd-tools

? 2. Create a Password File and Add a User

Choose a secure location (outside web root) like /etc/nginx/.htpasswd .

Setting Up Nginx Basic Authentication

To create the first user:

 sudo htpasswd -c /etc/nginx/.htpasswd username
  • -c creates a new file (use only once per file).
  • You'll be prompted to enter and confirm the password.

To add more users later (omit -c or it will overwrite!):

 sudo htpasswd /etc/nginx/.htpasswd anotheruser

The file will look like:

 username:$apr1$H6GJZ...etc

? 3. Configure Nginx to Use Basic Auth

Edit your site's Nginx config (usually in /etc/nginx/sites-available/your-site or /etc/nginx/conf.d/your-site.conf ).

Inside the location block you want to protect — for example, the whole site:

 server {
    listen 80;
    server_name example.com;

    location / {
        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/.htpasswd;

        # rest of your config (root, index, etc.)
        root /var/www/html;
        index index.html;
    }
}

To protect only a specific path:

 location /admin/ {
    auth_basic "Admin Area";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

? 4. Test and Reload Nginx

Always test your config before reloading:

 sudo nginx -t

If it passes:

 sudo systemctl reload nginx

Now, when you visit the protected location, you'll see a browser prompt asking for a username and password.


? Tips

  • Keep the .htpasswd file outside the web root — never serve it!
  • Use strong passwords (consider a password manager).
  • For production, pair basic auth with HTTPS — credentials are sent in base64 over the wire.
  • If you need multiple users with different permissions, basic auth won't help — consider app-level auth instead.

That's it. No plugins, no extra services — just a quick, reliable way to add a password wall.
Basically: generate the file → point Nginx to it → reload. Done.

以上是設置NGINX基本身份驗證的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
是什麼導致NGINX中的'太多打開文件”錯誤? 是什麼導致NGINX中的'太多打開文件”錯誤? Jul 05, 2025 am 12:14 AM

當Nginx出現“Toomanyopenfiles”錯誤時,通常是因為系統(tǒng)或進程達到了文件描述符限制。解決方法包括:1.提高Linux系統(tǒng)的軟硬限制,在/etc/security/limits.conf中設置nginx或運行用戶的相關參數;2.調整Nginx的worker_connections值以適應預期流量,並確保重載配置;3.增加系統(tǒng)級文件描述符上限fs.file-max,編輯/etc/sysctl.conf並應用更改;4.優(yōu)化日誌和資源使用,減少不必要的文件句柄佔用,例如使用open_l

主Nginx配置文件(nginx.conf)在哪裡? 主Nginx配置文件(nginx.conf)在哪裡? Jul 05, 2025 am 12:10 AM

Nginx主配置文件通常位於/etc/nginx/nginx.conf(Ubuntu/Debian、CentOS/RHEL)、/usr/local/etc/nginx/nginx.conf(macOSHomebrew)或源碼安裝路徑下的conf目錄;可通過nginx-t查看加載的配置路徑,ps-ef|grepnginx檢查啟動參數指定的路徑,或用find/-namenginx.conf、locatenginx.conf快速查找;配置文件結構包含全局設置、events塊和http塊,常見站點配置常

如何啟用GZIP壓縮以減少文件大小? 如何啟用GZIP壓縮以減少文件大??? Jul 10, 2025 am 11:35 AM

啟用Gzip壓縮可有效減少網頁文件體積、提升加載速度。 1.Apache服務器需在.htaccess文件中添加配置並確保mod_deflate模塊已啟用;2.Nginx則需編輯站點配置文件,設置gzipon並定義壓縮類型、最小長度和壓縮等級;3.配置完成後可通過在線工具或瀏覽器開發(fā)者工具驗證是否生效。操作時需注意服務器模塊狀態(tài)與MIME類型完整性以確保壓縮正常運行。

什麼是Stub_Status模塊,如何啟用其監(jiān)視? 什麼是Stub_Status模塊,如何啟用其監(jiān)視? Jul 08, 2025 am 12:30 AM

stub_status模塊顯示Nginx的實時基礎狀態(tài)信息。具體包括:1.當前活躍連接數;2.總接受連接數、總處理連接數、總請求數;3.正在讀取、寫入、等待的連接數。要檢查是否啟用,可通過命令nginx-V查看是否存在--with-http_stub_status_module參數。若未啟用,需重新編譯加入該模塊。啟用時需在配置文件中添加location塊並設置訪問控制,最後重載Nginx服務即可通過指定路徑訪問狀態(tài)頁面。建議結合監(jiān)控工具使用,但僅限內部網絡訪問,且不能替代全面監(jiān)控方案。

錯誤'已經在使用中的地址”或'端口80已經在使用中”是什麼意思? 錯誤'已經在使用中的地址”或'端口80已經在使用中”是什麼意思? Jul 07, 2025 am 12:09 AM

"Addressalreadyinuse"錯誤意味著系統(tǒng)中另一程序或服務已佔用目標端口或IP地址。常見原因包括:1.服務器重複運行;2.其他服務佔用端口(如Apache佔用80端口導致Nginx無法啟動);3.崩潰或重啟後端口未釋放??赏ㄟ^命令行工具排查:Linux/macOS使用sudolsof-i:80或sud??onetstat-tulpn|grep:80;Windows通過netstat-ano|findstr:80並查PID。解決方法包括:1.停止衝突進程(如sudos

如何啟用HTTP嚴格運輸安全(HSTS)? 如何啟用HTTP嚴格運輸安全(HSTS)? Jul 12, 2025 am 01:00 AM

啟用HSTS的方法是在HTTPS網站中配置Strict-Transport-Security響應頭,具體操作為:1.Nginx在server塊添加add_header指令;2.Apache在配置文件或.htaccess添加Header指令;3.IIS在web.config添加customHeaders;需確保站點已完整支持HTTPS,參數包括max-age(有效期)、includeSubDomains(子域名生效)、preload(預加載列表),提交到HSTSPreload列表前提包括根域名和子

什麼是Nginx Plus,其主要功能是什麼? 什麼是Nginx Plus,其主要功能是什麼? Jul 07, 2025 am 12:37 AM

NginxPlus與開源Nginx的主要區(qū)別在於其為企業(yè)級應用提供的增強功能和官方支持。 1.它提供實時監(jiān)控儀錶盤,可追蹤連接數、請求率及服務器健康狀態(tài);2.支持更高級的負載均衡方法,如最少連接數分配、基於哈希的一致性算法及加權分發(fā);3.支持會話保持(粘性會話),確保用戶請求持續(xù)發(fā)送至同一後端服務器;4.允許動態(tài)更新配置,無需重啟服務即可調整上游服務器組;5.提供高級緩存和內容分發(fā)功能,降低後端壓力並提升響應速度;6.可通過API實現自動化配置更新,適應Kubernetes或自動擴展環(huán)境;7.包含

如何使用split_clients模塊執(zhí)行A/B測試? 如何使用split_clients模塊執(zhí)行A/B測試? Jul 08, 2025 am 12:22 AM

A/B測試可通過Nginx的split_clients模塊實現,該方法基於用戶屬性哈希將流量按比例分配至不同組。具體步驟如下:1.在http塊中使用split_clients指令定義分組及比例,如50%A和50%B;2.使用$cookie_jsessionid、$remote_addr或$arg_uid等變量作為哈希鍵,確保同一用戶持續(xù)分配至同一組;3.在server或location塊中通過if條件判斷使用對應後端;4.通過自定義日誌格式記錄分組信息以便分析效果;5.結合監(jiān)控工具跟蹤各組性能與

See all articles