Nginx基本安全最佳化

來源:互聯網
上載者:User

標籤:nginx基本安全最佳化

隱藏Nginx軟體版本號碼

隱藏Nginx版本號碼

1、調整參數隱藏版本號碼資訊

  在Nginx設定檔nginx.conf中的http標籤段內加入“server_tokens off;”參數如下:

http {……server_tokens off;……}

  此參數放置在http標籤內,作用是控制http response header內的web服務版本資訊的顯示,以及錯誤資訊中web服務版本資訊的顯示。

server_tokens參數說明:

syntax:   server_tokens on|off; #<=此行為參數文法,on為開啟,off為關閉default:  server_tokens on;#<=此行意思是不配置該參數,軟體預設情況的結果context:  http,server,location#<=此行為server_tokens參數可以放置的位置參數作用:啟用或禁止nginx的版本資訊顯示在報錯資訊和server的響應首部位置中

2、更改源碼隱藏版本號碼

第一步依次修改3個Nginx源碼檔案

(1)修改第一個檔案nginx-1.6.3/src/core/nginx.h如下:

[[email protected] core]# sed -n ‘13,17p‘ nginx.h #define NGINX_VERSION      "1.6.3"  #<=修改為想要顯示的版本號碼#define NGINX_VER          "nginx/" NGINX_VERSION  #<=將nginx修改為想要修改的軟體名#define NGINX_VAR          "NGINX"  #<=將nginx修改為想要修改的軟體名稱#define NGX_OLDPID_EXT     ".oldbin"

修改後的結果為:

[[email protected] core]# sed -n ‘13,17p‘ nginx.h #define NGINX_VERSION      "2.2.23"#define NGINX_VER          "OWS/" NGINX_VERSION#define NGINX_VAR          "OWS"#define NGX_OLDPID_EXT     ".oldbin"

(2)修改第二個檔案nginx-1.6.3/src/http/ngx_http_header_filter_module.c的第49行:

[[email protected] http]# grep -n ‘Server: nginx‘ ngx_http_header_filter_module.c 49:static char ngx_http_server_string[] = "Server: nginx" CRLF;

替換後的結果:

[[email protected] http]# sed -i ‘s#Server: nginx#Server: OWS#g‘ ngx_http_header_filter_module.c [[email protected] http]# grep -n ‘Server: OWS‘ ngx_http_header_filter_module.c 49:static char ngx_http_server_string[] = "Server: OWS" CRLF;

(3)修改第三個檔案nginx-1.6.3/src/http/ngx_http_special_response.c

[[email protected] http]# sed -n ‘21,30p‘ ngx_http_special_response.c static u_char ngx_http_error_full_tail[] ="<hr><center>" NGINX_VER "</center>" CRLF  #<=此行需要修改"</body>" CRLF"</html>" CRLF;static u_char ngx_http_error_tail[] ="<hr><center>nginx</center>" CRLF  #<=此行需要修改"</body>" CRLF

修改後的結果為:

[[email protected] http]# sed -n ‘21,30p‘ ngx_http_special_response.c static u_char ngx_http_error_full_tail[] ="<hr><center>" NGINX_VER "(http://zhanghongxin.blog.51cto.com)</center>" CRLF#<=此行是定義對外展示的內容"</body>" CRLF"</html>" CRLF;static u_char ngx_http_error_tail[] ="<hr><center>OWS</center>" CRLF  #<=此行將對外展示的nginx名稱更改為OWS"</body>" CRLF

第二步修改後編譯軟體,使其生效

修改後再編譯安裝軟體,如果是已安裝好的服務,需要重新編譯Nginx,配好配置,啟動服務。


更改Nginx服務的預設使用者

查看nginx對應的預設使用者:

[[email protected] conf]# grep ‘#user‘ nginx.conf.default #user  nobody;

(1)為Nginx服務建立新使用者

useradd -s /sbin/nologin -M nginx

(2)配置Nginx服務,讓其使用剛建立的nginx使用者

第一種直接更改設定檔參數,將預設的#user nobody;改為如下內容:

user  nginx nginx;

如果注釋或不設定上述參數,預設為nobody使用者,不推薦使用nobody使用者,最好採用一個普通使用者。

第二種為直接在編譯Nginx軟體時指定編譯的使用者和組,命令如下:

./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.3 --with-http_stub_status_module --with-http_ssl_module

(3)檢查更改使用者的效果

重新載入配置後,檢查Nginx服務進程的對應使用者;

[[email protected] conf]# ps -ef|grep nginx|grep -v greproot       3895      1  0 Sep08 ?        00:00:06 nginx: master process /application/nginx/sbin/nginxnginx     34007  34006  0 Sep09 ?        00:00:00 nginx: worker process            nginx     34008  34006  0 Sep09 ?        00:00:00 nginx: worker process













本文出自 “小新” 部落格,請務必保留此出處http://zhanghongxin.blog.51cto.com/11255031/1851420

Nginx基本安全最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.