提高Nginx伺服器硬度的12個技巧

陶然陶然發表於2018-08-14

Nginx是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,並在一個BSD-like 協議下發行。其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力確實在同型別的網頁伺服器中表現較好,中國大陸使用nginx網站使用者有:百度、京東、新浪、網易、騰訊、淘寶等。

也許你聽過以上關於Nginx的美妙的事情,您可能已經很喜歡它了,正在考慮如何提高Nginx伺服器的安全性,穩定性,或者您考慮把Apache替換成Nginx,那麼本篇文章非常適合您繼續看下去。

本文將介紹用來提高Nginx伺服器的安全性,穩定性和效能的12種操作。

TIP #1: 保持Nginx的及時升級

目前Nginx的穩定版本為1.14.0,最好升級到最新版本,看官方的release note你會發現他們修復了很多bug,任何一款產品的生產環境都不想在這樣的bug風險下執行的。

另外,雖然安裝包安裝比通過原始碼編譯安裝更容易,但後一個選項有兩個優點:

1)它允許您將額外的模組新增到Nginx中(如more_header,mod_security),

2)它總是提供比安裝包更新的版本,在Nginx網站上可看release note。

TIP #2: 去掉不用的Nginx模組

在編譯安裝時,執行./configure方法時加上以下配置指令,可以顯式的刪除不用的模組:

./configure --without-module1 --without-module2 --without-module3

例如:

./configure --without-http_dav_module --withouthttp_spdy_module

注意事項:配置指令是由模組提供的。確保你禁用的模組不包含你需要使用的指令!在決定禁用模組之前,應該檢查Nginx文件中每個模組可用的指令列表。

TIP #3: 在Nginx配置中禁用server_tokens項

server_tokens在開啟的情況下會使404頁面顯示Nginx的當前版本號。這樣做顯然不安全,因為黑客會利用此資訊嘗試相應Nginx版本的漏洞。

只需要在nginx.conf中http模組設定server_tokens off即可,例如:

server { listen 192.168.0.25:80; Server_tokens off; server_name tecmintlovesnginx.com www.tecmintlovesnginx.com; access_log /var/www/logs/tecmintlovesnginx.access.log; error_log /var/www/logs/tecmintlovesnginx.error.log error; root /var/www/tecmintlovesnginx.com/public_html; index index.html index.htm; }

重啟Nginx後生效:

TIP #4: 禁止非法的HTTP User Agents

User Agent是HTTP協議中對瀏覽器的一種標識,禁止非法的User Agent可以阻止爬蟲和掃描器的一些請求,防止這些請求大量消耗Nginx伺服器資源。

為了更好的維護,最好建立一個檔案,包含不期望的user agent列表例如/etc/nginx/blockuseragents.rules包含如下內容:

map $http_user_agent $blockedagent { default 0; ~*malicious 1; ~*bot 1; ~*backdoor 1; ~*crawler 1; ~*bandit 1; }

然後將如下語句放入配置檔案的server模組內:

include /etc/nginx/blockuseragents.rules;

並加入if語句設定阻止後進入的頁面:

TIP #5: 禁掉不需要的 HTTP 方法

例如一些web站點和應用,可以只支援GET、POST和HEAD方法。

在配置檔案中的server模組加入如下方法可以阻止一些欺騙攻擊

if ($request_method !~ ^(GET|HEAD|POST)$) {  return 444;  }

TIP #6: 設定緩衝區容量上限

這樣的設定可以阻止緩衝區溢位攻擊(同樣是Server模組)

client_div_buffer_size 1k; client_header_buffer_size 1k; client_max_div_size 1k; large_client_header_buffers 2 1k;

設定後,不管多少HTTP請求都不會使伺服器系統的緩衝區溢位了。

TIP #7: 限制最大連線數

在http模組內,server模組外設定limit_conn_zone,可以設定連線的IP

在http,server或location模組設定limit_conn,可以設定IP的最大連線數

例如:

limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn addr 1;

TIP #8: 設定日誌監控

上面的截圖中已經有了,如何設定nginx日誌

你或許需要拿一下因為Tip #7的設定訪問失敗的日誌

grep addr /var/www/logs/tecmintlovesnginx.error.log --color=auto

同時你在日誌中還可以篩選如下內容:

  • 客戶端IP

  • 瀏覽器型別

  • HTTP請求方法

  • 請求內容

  • 伺服器相應

TIP #9: 阻止圖片外鏈自你的伺服器

這樣做顯然會增加你伺服器的頻寬壓力。

假設你有一個img目錄用來儲存圖片,你自己的IP是192.168.0.25,加入如下配置可以防止外鏈

location /img/ { valid_referers none blocked 192.168.0.25; if ($invalid_referer) { return 403; } }

TIP #10: 禁止 SSL 並且只開啟 TLS

只要可以的話,儘量避免使用SSL,要用TLS替代,以下設定可以放在Server模組內:

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

TIP #11: 做證書加密(HTTPS)

首先生成金鑰和整數,用以下哪種都可以:

# openssl genrsa -aes256 -out tecmintlovesnginx.key 1024 # openssl req -new -key tecmintlovesnginx.key -out tecmintlovesnginx.csr # cp tecmintlovesnginx.key tecmintlovesnginx.key.org # openssl rsa -in tecmintlovesnginx.key.org -out tecmintlovesnginx.key # openssl x509 -req -days 365 -in tecmintlovesnginx.csr -signkey tecmintlovesnginx.key -out tecmintlovesnginx.crt

然後配置Server模組

server { listen 192.168.0.25:443 ssl; server_tokens off; server_name tecmintlovesnginx.com www.tecmintlovesnginx.com; root /var/www/tecmintlovesnginx.com/public_html; ssl_certificate /etc/nginx/sites-enabled/certs/tecmintlovesnginx.crt; ssl_certificate_key /etc/nginx/sites-enabled/certs/tecmintlovesnginx.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; }

TIP #12: 重定向HTTP請求到HTTPS

在TIP#11基礎上增加

return 301 https://$server_name$request_uri;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28285180/viewspace-2199992/,如需轉載,請註明出處,否則將追究法律責任。

相關文章