【中间件安全】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 main3、保存,然后后重啟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 –t2、補丁安裝
手動安裝補丁或安裝最新版本軟件
最后
歡迎關注個人微信公眾號:Bypass--,每周原創一篇技術干貨。?
?
總結
以上是生活随笔為你收集整理的【中间件安全】Nginx 安全加固规范的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何保护地球不挨撞?
- 下一篇: [旧文]图解nlite精简XP全过程