【中间件加固】Nginx安全加固规范
1. 適用情況
適用于使用Nginx進(jìn)行部署的Web網(wǎng)站。
2. 技能要求
熟悉Nginx配置,能夠Nginx進(jìn)行部署,并能針對(duì)站點(diǎn)使用Nginx進(jìn)行安全加固。
3. 前置條件
1、 根據(jù)站點(diǎn)開放端口,進(jìn)程ID,確認(rèn)站點(diǎn)采用Nginx進(jìn)行部署;
2、 找到Nginx安裝目錄,針對(duì)具體站點(diǎn)對(duì)配置文件進(jìn)行修改;
3、 在執(zhí)行過程中若有任何疑問或建議,應(yīng)及時(shí)反饋。
4. 詳細(xì)操作
4.1 日志配置
1、備份nginx.conf 配置文件。
修改配置,按如下設(shè)置日志記錄文件、記錄內(nèi)容、記錄格式,添加標(biāo)簽為main的log_format格式
(http標(biāo)簽內(nèi),在所有的server標(biāo)簽內(nèi)可以調(diào)用): 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標(biāo)簽內(nèi),定義日志路徑
access_log logs/host.access.log main3、保存,然后后重啟nginx服務(wù)。
4.2 禁止目錄瀏覽
備份nginx.conf配置文件。
編輯配置文件,HTTP模塊添加如下一行內(nèi)容:
autoindex off;保存,然后后重啟nginx服務(wù)。
4.3 限制目錄執(zhí)行權(quán)限
備份nginx.conf配置文件。
編輯配置文件,在server標(biāo)簽內(nèi)添加如下內(nèi)容:
#示例:去掉單個(gè)目錄的PHP執(zhí)行權(quán)限 location ~ /attachments/.*\.(php|php5)?$ { deny all; }#示例:去掉多個(gè)目錄的PHP執(zhí)行權(quán)限 location ~ /(attachments|upload)/.*\.(php|php5)?$ { deny all; }保存,然后后重啟nginx服務(wù)。
需要注意兩點(diǎn):
1、以上的配置文件代碼需要放到 location ~ .php{...}上面,如果放到下面是無效的;
2、attachments需要寫相對(duì)路徑,不能寫絕對(duì)路徑。
4.4 錯(cuò)誤頁面重定向
備份nginx.conf配置文件。
修改配置,在http{}段加入如下內(nèi)容
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; ... } 修改內(nèi)容: 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 為要指定的錯(cuò)誤提示頁面保存,重啟 nginx 服務(wù)生效
4.5 最佳經(jīng)驗(yàn)實(shí)踐
4.5.1 隱藏版本信息
備份nginx.conf配置文件。
編輯配置文件,添加http模塊中如下一行內(nèi)容:
server_tokens off;保存,然后后重啟nginx服務(wù)。
4.5.2 限制HTTP請(qǐng)求方法
備份nginx.conf配置文件。
編輯配置文件,添加如下內(nèi)容:
if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 444; }保存,然后后重啟nginx服務(wù)。
備注:只允許常用的GET和POST方法,頂多再加一個(gè)HEAD方法
4.5.3 限制IP訪問
備份nginx.conf配置文件。
編輯配置文件,在server標(biāo)簽內(nèi)添加如下內(nèi)容:
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服務(wù)。
4.5.4 限制并發(fā)和速度
備份nginx.conf配置文件。
編輯配置文件,在server標(biāo)簽內(nèi)添加如下內(nèi)容:
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服務(wù)。
4.5.5 控制超時(shí)時(shí)間
備份nginx.conf配置文件。
編輯配置文件,具體設(shè)置如下:
client_body_timeout 10; #設(shè)置客戶端請(qǐng)求主體讀取超時(shí)時(shí)間 client_header_timeout 10; #設(shè)置客戶端請(qǐng)求頭讀取超時(shí)時(shí)間 keepalive_timeout 5 5; #第一個(gè)參數(shù)指定客戶端連接保持活動(dòng)的超時(shí)時(shí)間,第二個(gè)參數(shù)是可選的,它指定了消息頭保持活動(dòng)的有效時(shí)間 send_timeout10; #指定響應(yīng)客戶端的超時(shí)時(shí)間保存,然后后重啟nginx服務(wù)。
4.6 風(fēng)險(xiǎn)操作項(xiàng)
4.6.1 Nginx降權(quán)
備份nginx.conf配置文件。
編輯配置文件,添加如下一行內(nèi)容:
user nobody;?保存,然后后重啟nginx服務(wù)。
4.6.2 防盜鏈
備份nginx.conf配置文件。
編輯配置文件,在server標(biāo)簽內(nèi)添加如下內(nèi)容:
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服務(wù)。
4.6.3 補(bǔ)丁更新
1、軟件信息
查看軟件版本 nginx -v
測(cè)試配置文件 nginx –t
2、補(bǔ)丁安裝
手動(dòng)安裝補(bǔ)丁或安裝最新版本軟件
————————————————
版權(quán)聲明:本文為CSDN博主「Bypass--」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_23936389/article/details/85013923
總結(jié)
以上是生活随笔為你收集整理的【中间件加固】Nginx安全加固规范的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【中间件加固】Weblogic安全加固规
- 下一篇: Nginx sendfile作用