美文网首页
Nginx常见配置

Nginx常见配置

作者: zkzhengmeng | 来源:发表于2024-10-29 13:37 被阅读0次
# Nginx 防护配置示例
user www-data;                # 运行 Nginx 的用户
worker_processes auto;       # 自动设置工作进程数

events {
  worker_connections 1024;  # 每个工作进程的最大连接数
}

http {
  include /etc/nginx/mime.types;             # 包含 MIME 类型设置
  default_type application/octet-stream;      # 默认 MIME 类型

  # 日志设置
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
  access_log /var/log/nginx/access.log main;  # 访问日志
  error_log /var/log/nginx/error.log;          # 错误日志

  # 启用 gzip 压缩
  gzip on;
  gzip_types text/plain application/json;       # 指定要压缩的 MIME 类型
  gzip_min_length 1000;                        # 最小压缩长度

  # 隐藏服务器信息
  server_tokens off;                           # 禁用服务器版本信息
  add_header Server "";                        # 清空 Server 头部


  # 设置 Content Security Policy 头部  解释:CSP 以允许从同一源加载资源,同时限制脚本和样式的来源
  add_header Content-Security-Policy "default-src 'self'; img-src 'self' data:; script-src 'self' http://192.168.8.248; style-src 'self' 'unsafe-inline';";

  # 设置安全头部
  add_header X-Content-Type-Options "nosniff" always;    # 用于防止浏览器进行 MIME 类型嗅探。它告知浏览器严格按照服务器提供的内容类型(Content-Type)来处理响应,而不允许尝试猜测或推测内容的类型。
  add_header X-XSS-Protection "1; mode=block" always;    # 防止 XSS 攻击
  add_header X-Frame-Options "DENY" always;              # 防止点击劫持  DENY:完全禁止任何页面通过 <iframe> 嵌入你的网页。 SAMEORIGIN:仅允许来自同一源的页面嵌入你的网页。

  add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; # HSTS 强制浏览器使用HTTPS连接 
  add_header Referrer-Policy "no-referrer" always;       # 隐藏来源信息 设置为 no-referrer 时,浏览器在进行链接导航或资源请求时,不会发送来源页面的 URL。换句话说,用户从一个页面跳转到另一个页面时,目标页面将不会接收到来源页面的信息。
  # 控制浏览器功能   
  #geolocation 'none':禁用地理定位功能,表示页面不允许访问用户的位置信息。
  #microphone 'none':禁用麦克风访问,表示页面不允许使用用户的麦克风。
  #camera 'none':禁用摄像头访问,表示页面不允许使用用户的摄像头。
  add_header Feature-Policy "geolocation 'none'; microphone 'none'; camera 'none'";  

  # 限制请求速率
  limit_req_zone $binary_remote_addr zone=one:10m rate=30r/s;  # 每个 IP 每秒最多 30 个请求
  limit_req zone=one burst=10 nodelay;                       # 允许突发请求

  # 设置服务器
  server {
      listen 80;
      server_name yourdomain.com;  # 服务器名称

      # 强制使用 HTTPS
      return 301 https://$host$request_uri;  # 将 HTTP 请求重定向到 HTTPS
  }

  server {
      listen 443 ssl;  # 监听 HTTPS 端口
      server_name yourdomain.com;  # 服务器名称
      # 强制浏览器使用HTTPS连接   
 

      # SSL 设置
      ssl_certificate /etc/ssl/certs/your_cert.crt;     # SSL 证书路径
      ssl_certificate_key /etc/ssl/private/your_key.key; # SSL 密钥路径
      ssl_protocols TLSv1.2 TLSv1.3;                    # 启用安全的 SSL 协议
      ssl_ciphers 'HIGH:!aNULL:!MD5';                    # 启用安全的加密算法
      ssl_prefer_server_ciphers on;                      # 优先使用服务器的加密算法

      location / {
          root /var/www/html;  # 网站根目录
          index index.html index.htm;  # 默认首页
          try_files $uri $uri/ =404;  # 尝试文件,若不存在则返回 404
      }

      # 错误页面处理
      error_page 404 /404.html;            # 404 错误页面
      error_page 500 502 503 504 /50x.html; # 500 错误页面

      location = /50x.html {
          root /usr/share/nginx/html;      # 错误页面的路径
      }

      # 处理 PHP 文件
      location ~ \.php$ {
          include snippets/fastcgi-php.conf;  # PHP 配置
          fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # PHP 处理
      }

      # 限制 HTTP 方法
      if ($request_method !~ ^(GET|POST|OPTIONS)$ ) {
          return 444;  # 其他方法返回 444
      }

      # 日志设置
      access_log /var/log/nginx/yourdomain_access.log;  # 访问日志
      error_log /var/log/nginx/yourdomain_error.log;    # 错误日志
  }
}

相关文章

网友评论

      本文标题:Nginx常见配置

      本文链接:https://www.haomeiwen.com/subject/zzjmdjtx.html