参考资料

  1. Nginx的负载均衡能力
  2. Tengine编译安装详细说明以及案例
  3. 如何通过响应头防御XSS?
  4. Nginx教程,一看就懂,一学就会
  5. Nginx镜像模块:ngx_http_mirror_modu详细说明以及案例le
  6. Nginx upstream容错机制详解详细说明以及案例
  7. Nginx Keepalived配置详细说明以及案例
  8. Nginx端口监听(listen指令)详细说明以及案例
  1. 安装htpasswd工具(Apache工具包):

    • Ubuntu/Debian: sudo apt-get install apache2-utils

    • CentOS/RHEL: sudo yum install httpd-tools

  2. 创建密码文件:

    sudo htpasswd -c /etc/nginx/.htpasswd username
  3. 编辑Nginx配置文件(通常在/etc/nginx/sites-available/default/etc/nginx/conf.d/default.conf):

    server {
        listen 80;
        server_name example.com;
    
        location / {
            auth_basic "Restricted Area";
            auth_basic_user_file /etc/nginx/.htpasswd;
        }
    }
  4. 测试并重载Nginx配置:

    sudo nginx -t
    sudo systemctl reload nginx

完整示例:

server {
    listen 80;
    server_name mysite.com;

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

        # 其他配置
        proxy_pass http://localhost:8080;
    }
}