Nginx增加SSL證書是應該注意的問題
- 防火牆配置:關閉所有未使用的外部埠。通過為配置防火牆提供 CLI,不復雜的防火牆 (ufw) 為
iptables
提供了前端。
安裝
ufw
,並將其配置為允許所需任何埠上的流量。sudo apt-get install ufw
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable - HTTPS配置
配置反向代理,以便進行安全 (HTTPS) 客戶端連線
-
通過指定由受信任的證書頒發機構 (CA) 頒發的有效證書來配置伺服器,以偵聽埠
443
上的 HTTPS 流量。 -
通過採用以下“/etc/nginx/nginx.conf”檔案中所示的某些做法來增強安全保護。示例包括選擇更強的密碼並將通過 HTTP 的所有流量重定向到 HTTPS。
-
新增
HTTP Strict-Transport-Security
(HSTS) 標頭可確保由客戶端發起的所有後續請求都通過 HTTPS。 -
如果將來將禁用 HTTPS,請使用以下方法之一:
- 不要新增 HSTS 標頭。
- 選擇短的
max-age
值。
新增 /etc/nginx/proxy.conf 配置檔案:
proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffers 32 4k;
編輯 /etc/nginx/nginx.conf 配置檔案。示例包含一個配置檔案中的
http
和server
部分。http { include /etc/nginx/proxy.conf; limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s; server_tokens off; sendfile on; keepalive_timeout 29; # Adjust to the lowest possible value that makes sense for your use case. client_body_timeout 10; client_header_timeout 10; send_timeout 10; upstream helloapp{ server localhost:5000; } server { listen *:80; add_header Strict-Transport-Security max-age=15768000; return 301 https://$host$request_uri; } server { listen *:443 ssl; server_name example.com; ssl_certificate /etc/ssl/certs/testCert.crt; ssl_certificate_key /etc/ssl/certs/testCert.key; ssl_protocols TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"; ssl_ecdh_curve secp384r1; ssl_session_cache shared:SSL:10m; ssl_session_tickets off; ssl_stapling on; #ensure your cert is capable ssl_stapling_verify on; #ensure your cert is capable add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"; add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; #Redirects all traffic location / { proxy_pass http://helloapp; limit_req zone=one burst=10 nodelay; } } }
-
- 保護 Nginx 免受點選劫持的侵害
點選劫持(也稱為UI 偽裝攻擊)是一種惡意攻擊,其中網站訪問者會上當受騙,從而導致在與當前要訪問的頁面不同的頁面上單擊連結或按鈕。使用
X-FRAME-OPTIONS
可保護網站。緩解點選劫持攻擊:
-
編輯 nginx.conf 檔案:
sudo nano /etc/nginx/nginx.conf
新增行
add_header X-Frame-Options "SAMEORIGIN";
。 - 儲存該檔案。
- 重啟 Nginx。
-
- MIME 型別探查
此標頭可阻止大部分瀏覽器通過 MIME 方式探查來自已宣告內容型別的響應,因為標頭會指示瀏覽器不要替代響應內容型別。使用
nosniff
選項後,如果伺服器認為內容是“文字/html”,則瀏覽器將其顯示為“文字/html”。編輯 nginx.conf 檔案
sudo nano /etc/nginx/nginx.conf
新增行
add_header X-Content-Type-Options "nosniff";
並儲存檔案,然後重新啟動 Nginx。