参考资料

  1. nginx 配置静态资源
  2. Nginx upstream容错机制详解详细说明以及案例
  3. Nginx设置配置文件详细说明以及案例
  4. nginx 防止各种攻击
  5. NginxPHP服务器环境搭建详细说明以及案例
  6. Nginxtry_files 文件判断指令详细说明以及案例
  7. CGI(Common Gateway Interface,通用网关接口)详细说明以及案例
  8. 如何使用Nginx模块增强安全性?
  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;
    }
}