Nginx常見配置

# 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;    # 錯誤日志
  }
}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容