一 軟體優化
優化前
[root@centos7 ~]# curl -I http://192.168.10.242
HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Sat, 11 Aug 2018 03:26:17 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 10 Aug 2018 03:58:00 GMT
Connection: keep-alive
ETag: "5b6d0d48-264"
Accept-Ranges: bytes
1.1.1 隐藏Nginx軟體版本号(http标簽内)
[root@centos7 ~]# vim /usr/local/nginx/conf/nginx.conf
server_tokens off;
驗證:
[root@centos7 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@centos7 ~]# nginx -t
nginx: the configuration file /usr/local/nginx-1.15.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.15.2/conf/nginx.conf test is successful
[root@centos7 ~]# systemctl restart nginx
[root@centos7 ~]# curl -I http://192.168.10.242
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 11 Aug 2018 03:28:36 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 10 Aug 2018 03:58:00 GMT
Connection: keep-alive
ETag: "5b6d0d48-264"
Accept-Ranges: bytes
1.1.2 優化Nginx軟體名稱
修改源碼
vim /root/nginx-1.15.2/src/core/nginx.h
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_
#define nginx_version 1015002
#define NGINX_VERSION "8.09.2" #修改版本号
#define NGINX_VER "Yan/" NGINX_VERSION
#ifdef NGX_BUILD
#define NGINX_VER_BUILD NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD NGINX_VER
#endif
#define NGINX_VAR "Yan"
#define NGX_OLDPID_EXT ".oldbin"
#endif /* _NGINX_H_INCLUDED_ */
vim /root/nginx-1.15.2/src/http/ngx_http_header_filter_module.c
49 static u_char ngx_http_server_string[] = "Server: Yan" CRLF;
vim /root/nginx-1.15.2/src/http/ngx_http_special_response.c
36 "<hr><center>Yan</center>" CRLF
[root@centos7 sbin]# curl -I http://192.168.10.247
HTTP/1.1 200 OK
Server: Yan/8.09.2
Date: Sat, 11 Aug 2018 04:10:14 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 11 Aug 2018 04:08:10 GMT
Connection: keep-alive
ETag: "5b6e612a-264"
Accept-Ranges: bytes
1.1.3 定義Nginx運作的使用者和使用者組
user nginx nginx;
二 優化Nginx性能
2.1.1 優化nginxworker程序數(初始可設定為CPU總核數)也可以設定為auto nginx會自動根據核心數為生成對應數量的worker程序
worker_processes 2;
2.1.2 CPU親和力 把不同的程序綁定在不用的cup 0001 代表cpu掩碼
worker_cpu_affinity 0001 0010 0100 1000 #四核cpu
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 1000
0000 #八核cpu
2.1.3 Nginx事件處理模型(events)事件裡配置 Nginx預設選擇最佳模型
具體模型資料可以看下官網或者:https://blog.csdn.net/ghost_leader/article/details/72902580
use epoll;
2.1.4 Nginx單個程序允許用戶端最大連結數(events)
worker_connections 4096;
2.1.5 Nginx work程序最大打開檔案數 放置主标簽段
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 20480;
}
2.1.6 開啟檔案高效傳輸,防止網絡以及磁盤I/O阻塞,提升Nginx工作效率 tcp_nopush和tcp_nodelay同時使用
官網:http://nginx.org/en/docs/http/ngx_http_core_module.html#sendfile
放置位置:http,server,location (拷貝函數實作核心零拷貝)
sendfile on;
tcp_nopush on; #可以把http response header和檔案到開始放在一個檔案裡釋出,減少網絡封包數量。
tcp_nodelay on; #提高I/o性能,
2.1.7 伺服器端連結逾時(http标簽)
keepalive_timeout 65;
2.1.8 Nginx讀取用戶端請求頭部資訊逾時(http标簽)
client_header_timeout 15s;
2.1.9 Nginx讀取用戶端請求主體逾時(http标簽)
client_body_timeout 60s;
2.2.0 Nginx指定形影用戶端逾時時間(http标簽)
send_timeout 60s;
2.2.1 用戶端上傳檔案大小控制(具體看業務)(http标簽)
client_max_body_size 8m;
2.2.2 指定字元集(server标簽)
charset utf-8;
2.2.3 404優雅提示
error_page 404 403 /404.html;
error_page 404 403 http://www.baidu.com;
三 fastcgi相關優化
官網位址:http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html
3.1.1 Nginx伺服器和後端FastCGI伺服器連接配接的逾時時間
fastcgi_connect_timeout 30;
3.1.2 Nginx允許FastCGI服務端傳回資料的逾時時間
fastcgi_send_timeout 15;
3.1.3 Nginx從FastCGI服務端讀取響應資訊的逾時時間
fastcgi_read_timeout 15;
3.1.4 Nginx讀取響應資訊的逾時時間
fastcgi_read_timeout 15;
3.1.5 讀取從FastCGI服務端收到的第一部分響應資訊的緩沖區大小
fastcgi_buffer_size 64k;
3.1.6 讀取從FastCGI服務端收到的響應資訊的緩沖區大小以及緩沖區數量
fastcgi_buffers 4 64k;
3.1.7 系統很忙時可以使用的fastcgi_buffers大小,推薦大小為fastcgi_buffers *2。
fastcgi_busy_buffers_size 128k;
3.1.8 fastcti臨時檔案的大小,可設定128-256K
fastcgi_temp_file_write_size 512k;
3.2.9 設定fastcgi_cache存儲路徑
#fastcgi_temp_path:生成fastcgi_cache臨時檔案目錄 cache的目錄,采用二級目錄hash,256*256目錄分級,設定緩存記憶體空間為100m,官方文檔給出的1m能夠容納8000個cache key,最大硬碟占用空間為10g,是所有cache檔案所占用硬碟的限制值
fasrcgi_cache_path /tmp/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:100m inactive=1d max_size=10g;
3.2.0 fastcgi_cache
location ~.*\.(php|php5)?% {
#設定fastcg的臨時目錄,cache存放的時候是首先放到這個目錄然後移動到cache目錄中,官方文檔要求這兩個目錄位于同一個分區以保證性能
#fastcgi_temp_path /tmp/ngx_fcgi_tmp 1 2;
#fscg路徑
fastcgi_pass 127.0.0.1:9000;
#指定index
fastcgi_index index.php;
#導入配置檔案
include fastcgi.conf;
#當一次請求過來需要寫一個cache key的時候,會把這個key鎖住,目的是當多個請求同時請求寫同一個key的時候隻有一個可以去寫,其它的等待該key寫成功後直接從cache中取,防止了大量請求下穿透cache給後端fastcgi造成過大的壓力
fastcgi_cache_lock on;
#設定鎖過期的時間
fastcgi_cache_lock_timeout 1s;
#用哪個緩存空間(在http層中我們定義的keys_zone
fastcgi_cache ngx_fcgi_cache;
#定義哪些http頭要緩存 示例中200 302 都是1h 緩存301d等 其他的狀态是1分鐘緩存
fastcgi_cache_valid 200 302 1h; #
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
#請求幾次之後緩存
fastcgi_cache_min_uses 1;
#定義那些情況下使用過期緩存
fastcgi_cache_use_stale error timeout invalid_header http_500;
#定義fastcgi_cache的key
fastcgi_cache_key http://$host$request_uri;
}
四 Nginx壓縮優化
功能介紹:
Nginx gzip壓縮子產品提供了壓縮檔案内容的功能,使用者請求的内容發送出去到使用者端前Nginx會根據一些政策來進行壓縮,節約出站口帶寬,加快了輸出傳輸效率,提升了使用者體驗。
傳文本壓縮比高 如html,js,css
圖檔,視訊等檔案盡量不壓縮,壓縮會消耗大量cpu,記憶體等資源。
4.1.1 開啟gzip壓縮
gzip on;
4.1.2 允許壓縮頁面的最小位元組,頁面位元組數從header頭的content-lenget中擷取
gzip_min_length 1k;
4.1.3 壓縮緩沖區
gzip_buffers 4 16k;
4.1.4 壓縮版本識别http協定版本
gzip_http_version 1.1;
4.1.5 壓縮級别 1壓縮比最小處理最快;9壓縮級别比最大,傳輸速度快但是處理慢,消耗資源
gzip_comp_level 6;
4.1.6 指定類型
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
檢視媒體類型
[root@centos7 ~]# cd /usr/local/nginx/conf/
[root@centos7 conf]# less mime.types
4.1.7 vary header支援。可以讓前端的cnd緩存伺服器可以緩存gzip頁面而不解壓
gzip_vary on;
4.1.8 對ie6浏覽器不壓縮 IE6對Gzip不怎麼友好,不給它Gzip了
gzip_disable "MSIE [1-6]\.";
五 Nginx expires緩存優化
通過使用者通路網站内容進行緩存,使用者下次通路如果沒達到過期時間就不進行更新下載下傳,使用本地緩存,也可以說是浏覽器緩存
作用:
降低網站帶寬
提高網站速速,提升使用者體驗
降低了伺服器通路量,減輕了伺服器壓力
5.1.1 對字尾名進行緩存
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 365d;
}
5.1.2 對js,css檔案進行緩存
location ~ .*\.(js|css)?$ {
expires 30d;
}
5.1.3 對目錄進行緩存
location ~ ^/(images|js|css|media|static)/ {
expires 360d;
}
六 對伺服器目錄以及檔案URL通路控制
6.1.1 禁止指定目錄
location ~ ^/images/.*\.(php|php5|.sh|.pl|.py)$ {
deny all;
}
6.1.2 禁止指定目錄
location ~* ^/data/(images|data)/.*\.(php|php5)$ {
deny all;
}
6.1.3 禁止通路目錄
location ~ ^/(static|app) {
deny all;
}
七 限制網站來源IP通路
location ~ ^/data/ {
allow 192.68.10.5;
deny all;
}
location ~ {
deny 192.168.1.1;
allow 192.168.1.0/24;
deny all;
}
if ( $remote_addr = 192.168.10.10 ) {
return 403;
}
八 禁止非法域名解析通路企業網站
server {
listen 80 default_server;
server_name_;
return 501;
}
九 圖檔以目錄防盜鍊
location ~* ^.+\.(jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked *.yanshao.com yanshao.com;
if ($invalid_referer) {
rewrite ^/ http://www.yanshao.com/img/nolink.gif;
}
root html/www;
}
作者:闫世成
出處:http://cnblogs.com/yanshicheng
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接。如有問題或建議,請聯系上述郵箱,非常感謝。