原文地址:http://blog.csdn.net/qq_23598037/article/details/79505398
nginx的最佳化
1. gzip壓縮最佳化2. expires緩衝有還3. 網路IO事件模型最佳化4. 隱藏軟體名稱和版本號碼5. 防盜鏈最佳化6. 禁止惡意網域名稱解析7. 禁止通過IP地址訪問網站8. HTTP要求方法最佳化9. 防DOS攻擊單IP並發串連的控制,與串連速率控制10. 嚴格設定web網站目錄的許可權11. 將nginx進程以及網站運行於監牢模式12. 通過robot協議以及HTTP_USER_AGENT防爬蟲最佳化13. 配置錯誤頁面根據錯誤碼指定網頁反饋給使用者14. nginx日誌相關最佳化訪問日誌切割輪詢,不記錄指定元素日誌、最小化日誌目錄許可權15. 限制上傳到資來源目錄的程式被訪問,防止木馬入侵系統破壞檔案16. FastCGI參數buffer和cache設定檔的最佳化17. php.ini和php-fpm.conf設定檔的最佳化18. 有關web服務的Linux核心方面深度最佳化(網路連接、IO、記憶體等)19. nginx加密傳輸最佳化(SSL)20. web伺服器磁碟掛載及網路檔案系統的最佳化21. 使用nginx cache
1、基本安全最佳化
1.1 隱藏版本資訊
一般來說,軟體的漏洞都和版本相關,所以我們要隱藏或消除web服務對訪問使用者顯示的各種敏感資訊。
1 [root@db01 rpm]# curl -I 10.0.0.8 2 HTTP/1.1 401 Unauthorized 3 Server: nginx #隱藏版本號碼 4 Date: Thu, 21 Jul 2016 03:23:38 GMT 5 Content-Type: text/html 6 Content-Length: 188 7 Connection: keep-alive 8 WWW-Authenticate: Basic realm="oldboy training" 9 過程:10 vim /application/nginx/conf/nginx.conf11 在http模組下加入:12 server_tokens off;13 /application/nginx/sbin/nginx -t14 /application/nginx/sbin/nginx -s reload
1.2 隱藏nginx要修改原始碼
要修改內容的路徑:
第一路徑:
1 /home/oldboy/tools/nginx-1.6.3/src/core/nginx.h 第14,16行2 #define NGINX_VERSION "1.6.2" 修改為想要的版本號碼如2.4.33 #define NGINX_VER "nginx/" NGINX_VERSION 將nginx修改為想要修改的軟體名稱,如Apache。
第二路徑
1 /home/oldboy/tools/nginx-1.6.3/src/http/ngx_http_header_filter_module.c第49行2 grep 'Server:nginx' ngx_http_header_filter_module.cstatic3 sed -i 's#Server:nginx#Server:Apache#g' ngx_http_header_filter_module.c
第三路徑
/home/oldboy/tools/nginx-1.6.3/src/http/ngx_http_special_response.c第21,30行"<hr><center>"NGINX_VER "(http://oldboy.blog.51cto.com)</center>" CRLF"<hr><center>OWS</center>" CRLF
然後重新編譯 1.3 更改nginx服務的預設使用者
第一種方法:
直接更改設定檔nginx.conf.default參數,將預設的#user nobody;改為user nginx.nginx;
第二種方法:
直接在編譯nginx的時候指定使用者和使用者組命令如下:
./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module 1.4 降權啟動nginx
1 useradd inca2 cd /home/inca/3 mkdir conf logs www4 echo inca >www/index.html5 chown -R inca.inca *6 ln -s /application/nginx/conf/mime.types conf/mime.types #mime.types媒體類型檔案
egrep -v "#|^$" /application/nginx/conf/nginx.conf.default >conf/nginx.conf
nginx.conf設定檔
worker_processes 1;error_log /home/inca/logs/error.log;pid /home/inca/logs/nginx.pid;events { worker_connections 1024;}http { include mime.types; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8080; server_name localhost; location / { root /home/inca/www; index index.html index.htm; } access_log /home/inca/logs/access.log main;}}
su - inca -c "/application/nginx/sbin/nginx -c /home/inca/conf/nginx.conf" #啟動nginx服務
重點強調:
1.nginx.conf裡面的相關路徑都要更改
2.普通使用者的連接埠問題 2、 根據參數最佳化nginx服務效能 2.1 最佳化nginx進程個數的策略
在高並發、高訪問量的web服務情境,需要事先啟動好更多的nginx進程,以保證快速響應並處理大量並發使用者的請求。
worker_processes 1;一般調整到與CPU的核心數相同(如,2個四核的cpu計為8)
(1)查看LInux可查看CPU個數及總核心數
grep processor /proc/cpuinfo|wc -l
(2)查看CPU總顆數
grep 'physical id' /proc/cpuinfo|sort|uniq|wc -l
(3)通過執行top命令,然後按數字1,即可顯示所有的CPU核心數
top 按1鍵就會顯示第一個的資訊Cpu0 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0
2.2 最佳化綁定不同的nginx進程到不同的CPU上
預設情況下,nginx的進程跑在某一個CPU或CPU的某一個核上,導致nginx進程使用硬體的資源不均,本節的最佳化是不同的nginx進程給不同的CPU處理,充分有效利用有效硬體資源