【中介軟體安全】Nginx 安全加固規範
1. 適用情況
適用於使用Nginx進行部署的Web網站。
2. 技能要求
熟悉Nginx配置,能夠Nginx進行部署,並能針對站點使用Nginx進行安全加固。
3. 前置條件
1、 根據站點開放埠,程序ID,確認站點採用Nginx進行部署;
2、 找到Nginx安裝目錄,針對具體站點對配置檔案進行修改;
3、 在執行過程中若有任何疑問或建議,應及時反饋。
4. 詳細操作
4.1 日誌配置
1、備份nginx.conf 配置檔案。
修改配置,按如下設定日誌記錄檔案、記錄內容、記錄格式,新增標籤為main的log_format格式
(http標籤內,在所有的server標籤內可以呼叫): log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
2、在server標籤內,定義日誌路徑
access_log logs/host.access.log main
3、儲存,然後後重啟nginx服務。
4.2 禁止目錄瀏覽
備份nginx.conf配置檔案。
編輯配置檔案,HTTP模組新增如下一行內容:
autoindex off;
儲存,然後後重啟nginx服務。
4.3 限制目錄執行許可權
備份nginx.conf配置檔案。
編輯配置檔案,在server標籤內新增如下內容:
#示例:去掉單個目錄的PHP執行許可權 location ~ /attachments/.*\.(php|php5)?$ { deny all; } #示例:去掉多個目錄的PHP執行許可權 location~ /(attachments|upload)/.*\.(php|php5)?$ { deny all; }
儲存,然後後重啟nginx服務。
需要注意兩點:
1、以上的配置檔案程式碼需要放到 location ~ .php{...}上面,如果放到下面是無效的;
2、attachments需要寫相對路徑,不能寫絕對路徑。
4.4 錯誤頁面重定向
備份nginx.conf配置檔案。
修改配置,在http{}段加入如下內容
http { ... fastcgi_intercept_errors on; error_page 401 /401.html; error_page 402 /402.html; error_page 403 /403.html; error_page 404 /404.html; error_page 405 /405.html; error_page 500 /500.html; ... } 修改內容: ErrorDocument 400 /custom400.html ErrorDocument 401 /custom401.html ErrorDocument 403 /custom403.html ErrorDocument 404 /custom404.html ErrorDocument 405 /custom405.html ErrorDocument 500 /custom500.html 其中401.html、402.html、403.html、404.html、405.html、500.html 為要指定的錯誤提示頁面。
儲存,重啟 nginx 服務生效
4.5 最佳經驗實踐
4.5.1 隱藏版本資訊
備份nginx.conf配置檔案。
編輯配置檔案,新增http模組中如下一行內容:
server_tokens off;
儲存,然後後重啟nginx服務。
4.5.2 限制HTTP請求方法
備份nginx.conf配置檔案。
編輯配置檔案,新增如下內容:
if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 444; }
儲存,然後後重啟nginx服務。
備註:只允許常用的GET和POST方法,頂多再加一個HEAD方法
4.5.3 限制IP訪問
備份nginx.conf配置檔案。
編輯配置檔案,在server標籤內新增如下內容:
location / { deny 192.168.1.1; #拒絕IP allow 192.168.1.0/24; #允許IP allow 10.1.1.0/16; #允許IP deny all; #拒絕其他所有IP }
儲存,然後後重啟nginx服務。
4.5.4 限制併發和速度
備份nginx.conf配置檔案。
編輯配置檔案,在server標籤內新增如下內容:
limit_zone one $binary_remote_addr 10m; server { listen 80; server_name down.test.com; index index.html index.htm index.php; root /usr/local/www; #Zone limit; location / { limit_conn one 1; limit_rate 20k; } ……… }
儲存,然後後重啟nginx服務。
4.5.5 控制超時時間
備份nginx.conf配置檔案。
編輯配置檔案,具體設定如下:
client_body_timeout 10; #設定客戶端請求主體讀取超時時間 client_header_timeout 10; #設定客戶端請求頭讀取超時時間 keepalive_timeout 5 5; #第一個引數指定客戶端連線保持活動的超時時間,第二個引數是可選的,它指定了訊息頭保持活動的有效時間 send_timeout10; #指定響應客戶端的超時時間
儲存,然後後重啟nginx服務。
4.6 風險操作項
4.6.1 Nginx降權
備份nginx.conf配置檔案。
編輯配置檔案,新增如下一行內容:
user nobody;
儲存,然後後重啟nginx服務。
4.6.2 防盜鏈
備份nginx.conf配置檔案。
編輯配置檔案,在server標籤內新增如下內容:
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ { valid_referers none blocked server_names *.nsfocus.com http://localhost baidu.com; if ($invalid_referer) { rewrite ^/ [img]http://www.XXX.com/images/default/logo.gif[/img]; # return 403; } }
儲存,然後後重啟nginx服務。
4.6.3 補丁更新
1、軟體資訊
檢視軟體版本 nginx -v
測試配置檔案 nginx –t
2、補丁安裝
手動安裝補丁或安裝最新版本軟體
最後
歡迎關注個人微信公眾號:Bypass--,每週原創一篇技術乾貨。