1. 程式人生 > >Nginx安全配置參考

Nginx安全配置參考

0X01 日誌審計

① 參考配置操作
(1)編輯 nginx.conf 配置檔案
將 error_log 前的“#”去掉,記錄錯誤日誌
將 access_log 前的“#”去掉,記錄訪問日誌
(2)設定 access_log,修改配置檔案如下:
log_format formatname '$remote_addr - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent "$http_referer" '
' "$http_user_agent" "$http_x_forwarded_for"'; access_log
logs/access.log formantname;      #formatname 是設定配置檔案格式的名稱

②  備註事項
檢視 nginx.conf 配置檔案中,error_log、access_log 前的“#”是否去掉

0x02 服務

1  限制 IP 訪問

對網站或敏感目錄的訪問 IP 進行限制
① 參考配置操作
(1)修改配置檔案
#vi /usr/local/nginx/conf/nginx.conf
具體設定如下:
location / {
deny 192.168.1.1; #拒絕 IP
allow 192.168.1.0/24; #允許 IP
allow 10.1.1.0/16; #允許 IP
deny all; #拒絕其他所有 IP
(2)重新啟動 nginx 服務
② 備註事項
根據應用場景,設定合適的 IP 地址,

檢查配置檔案  #more /usr/local/nginx/conf/nginx.conf 中的 server 項

2  控制超時時間

控制超時時間,提高伺服器效能,降低客戶端的等待時間

① 建議配置
(1)修改配置檔案
#vi /usr/local/nginx/conf/nginx.conf
具體設定如下:
client_body_timeout 10; #設定客戶端請求主體讀取超時時間
client_header_timeout 10; #設定客戶端請求頭讀取超時時間
keepalive_timeout 5 5; 
#第一個引數指定客戶端連線保持活動的超時時間,第二個引數是可選的,它指定了訊息頭保持活動的有效時間

send_timeout 10; #指定響應客戶端的超時時間
(2)重新啟動 nginx 服務
② 備註事項
需要根據應用場景的需要選擇合適的引數值。
1 、符合性判定依據
超時後,伺服器返回相應的訊息。
2 、參考檢測方法
檢查配置檔案   #more /usr/local/nginx/conf/nginx.conf

3  下載限制併發和速度

限制客戶端下載速度,保證伺服器負載正常

① 建議配置
例如網站存放路徑為/usr/local/nsfocus/ ,伺服器名稱為:down.nsfocus.com
(1)修改配置檔案
#vi /usr/local/nginx/conf/nginx.conf
具體設定如下:
limit_zone one $binary_remote_addr 10m;
server
{
listen 80;
server_name down.nsfocus.com;
index index.html index.htm index.php;

oot /usr/local/nsfocus;
#Zone limit;
location / {
limit_conn one 1;
limit_rate 20k;
}
………
}

(2)重新啟動 nginx 服務

下載時,不會超過設計的併發連線數和速度限制,同時檢查 nginx.conf 檔案中的配置

0x03 其他事項

1  解除安裝不需要的模組

解除安裝不需要的 nginx 模組, 最大限度地將 nginx 載入的模組最小化

① 建議配置
(1)檢查需要禁用的模組
在編譯 nginx 伺服器時,使用下面的命令檢視哪些模組應該啟用,哪些模應
該禁用:
# ./configure --help | less
一旦處選了要禁用的模組,需要與相關人員溝通確認,並經過測試不影響業
務執行。
(2)例如,要禁用 autoindex 和 SSI 模組,命令如下:
# ./configure --without-http_autoindex_module --without-http_ssi_module
# make
# make install

② 備註事項

Nginx 不包含不必要的模組或者輸入 ./configure --help | less 進行檢查

2  防盜鏈設定

防止其他網站盜鏈本網站資源

① 建議配置

(1)修改配置檔案
#vi /usr/local/nginx/conf/nginx.conf
具體設定如下:
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.nsfocus.com/images/default/logo.gif[/img];
# return 403;
}
}
根據應用場景,設定合適的域名
(2)重新啟動 nginx 服務

② 備註事項

從非法網站訪問所保護的資源,出現設定的頁面。同時檢查配置檔案  #more /usr/local/nginx/conf/nginx.conf

3  自定義錯誤資訊

1)修改 src/http/ngx_http_special_response.c,自己定製錯誤資訊
## messages with just a carriage return.
static char ngx_http_error_400_page[] = CRLF;
static char ngx_http_error_404_page[] = CRLF;
static char ngx_http_error_413_page[] = CRLF;
static char ngx_http_error_502_page[] = CRLF;
static char ngx_http_error_504_page[] = CRLF;
常見錯誤:
400 bad request
404 NOT FOUND
413 Request Entity Too Large
502 Bad Gateway
504 Gateway Time-out
(2)重新啟動 nginx 服務
② 備註事項
URL 位址列中輸入 http://ip:8800/manager12345,訪問出錯時,返回自定義的錯誤頁面

4  隱藏 nginx 服務資訊頭

① 建議配置
修改 nginx解壓路徑/src/http/ngx_http_header_filter_module.c檔案的第48
和 49 行內容,自定義頭資訊:
static char ngx_http_server_string[] = “Server:XXXXX.com” CRLF;
static char ngx_http_server_full_string[] = “Server:XXXXX.com” CRLF;
新增如下程式碼到 nginx.conf 配置檔案,禁止錯誤頁面中顯示 nginx 版本號:
server_tokens off

② 備註事項

服務資訊頭顯示設定的內容,檢查 http 服務資訊頭內容

5  補丁更新

安裝系統補丁,修補漏洞
1 、參考配置操作
手動安裝補丁或安裝最新版本軟體,所安裝的補丁,應首先在經過測試驗證;安裝前,要做好資料備份。
2檢視版本和編譯器資訊

歡迎大家分享更好的思路,熱切期待^^_^^ !