1、Nginx基本安全優化
1.調整參數隱藏Nginx版本號信息
通常來講,軟件的漏洞和版本號有關,咱們應該儘可能隱藏或消除Web服務對訪問用戶顯示各種敏感信息,
增強Web服務的安全性。
查詢Nginx的版本號:curl -I URL
HTTP/1.1 401 Unauthorized
Server: nginx/1.6.3
Date: Sun, 30 Sep 2018 01:01:30 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="hty training"
編輯配置文件增長參數,實現隱藏版本號
在Nginx配置文件下nginx.conf中的http標籤段加入「server_tokens off;」
http{
...
server_tokens off;
...
}
重啓Nginx並查看
/application/nginx/sbin/nginx -s reload
curl -I 182.61.48.89
結果以下:
HTTP/1.1 401 Unauthorized
Server: nginx ##隱藏了版本號
Date: Sun, 30 Sep 2018 01:18:49 GMT
Content-Type: text/html
Content-Length: 188
Connection: keep-alive
WWW-Authenticate: Basic realm="hty training"
2.更改源碼隱藏Nginx軟件名及版本號
1)一次修改3個Nginx源碼文件
第一個文件爲nginx-1.6.3/src/core/nginx.h
操做命令集:
cd /home/hty/tools/nginx-1.6.3/src/core
#define NGINX_VERSION "1.6.3" #<==修改成想要顯示的版本號,如2.2.23
#define NGINX_VER "nginx/" NGINX_VERSION #<==將nginx修改成想要修改的軟件名稱,如「OWS」
#define NGINX_VAR "NGINX" #<==將nginx修改成想要修改的軟件名稱,如「OWS」
#define NGX_OLDPID_EXT ".oldbin"
第二個文件是nginx-1.6.3/src/http/ngx_http_header_filter_mudule.c的第49行
cd /home/hty/tools/nginx-1.6.3/src/http
grep -n 'Server:nginx' ngx_http_header_filter_mudule.c #顯示第49行內容
sed -i 's#Server: nginx#Server: OWS#g' ngx_http_header_filter_mudule.c #<==將Server:nginx 替換爲Server: OWS
第三個文件nginx-1.6.3/src/http/ngx_http_special_response.c
cd /home/hty/tools/nginx-1.6.3/src/http
sed -n '21,30p' ngx_http_special_response.c
將其中的內容"<hr><center>NGINX_VER</center>"CRLF修改成"<hr><center>NGINX_VER(http://oldboy.blog.51cto.com)</center>"CRLF,
"<hr><center>Nginx</center>"CRLF修改"<hr><center>OWS</center>"CRLF
2)修改編譯後,使其生效
修改後,編譯安裝軟件
3.更改Nginx的默認用戶
爲了軟件安全,儘量改到全部配置,包括端口、用戶
1)更改默認用戶
在conf目錄下,查看默認配置文件
grep '#user' nginx.conf.default
(1)爲Nginx服務創建新用戶
useradd nginx -s /sbin/nologin -M #不須要有系統登陸權限,禁止其登陸能力
id nginx
(2)配置Nginx服務,讓其使用剛創建的nginx用戶
方法1、直接更改配置文件參數,將默認#user nobody;改成
user nignx nginx
方法2、直接在編譯Nginx軟件時指定編譯的用戶和組
./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.3 --with-http_stub_status_module --with-http_ssl_module
(3)檢查更改用戶的效果
ps -ef|grep nginx|grep -v grep
結果以下:
[root@instance-yf0xzby9 conf]# ps -ef|grep nginx|grep -v grep
root 1768 1 0 Sep26 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx 2396 1768 0 Sep26 ? 00:00:00 nginx: worker process
root 2428 1 0 Sep26 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx 87285 87157 0 Sep29 ? 00:00:02 php-fpm: pool www
nginx 87308 87157 0 Sep29 ? 00:00:01 php-fpm: pool www
nginx 87309 87157 0 Sep29 ? 00:00:01 php-fpm: pool www
nginx 88530 2428 0 09:17 ? 00:00:00 nginx: worker process
php