LNMP=Linux Nginx Mysql PHPjavascript
LNMP的調優着重體如今Nginx服務器上的調優php
Nginx 是一個高性能的 HTTP 和 反向代理 服務器,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名,其特色是佔有內存少,併發能力強,百度BWS、新浪、網易、騰訊等都在使用。css
[root@xuegod68 ~]#tar xvf nginx-1.8.0.tar.gz -C /usr/local/html
[root@xuegod68 ~]#cd /usr/local/nginx-1.8.0/java
編譯前的優化主要是用來修改程序名等node
[root@xuegod68 ~]# curl -I http://www.baidu.com
HTTP/1.1 200 OK
Server: bfe/1.0.8.18 #咱們須要的作的是將nginx修更名字,這兒默認是nginx,咱們看到的百度是隱藏後的。
Date: Thu, 19 Apr 2018 08:05:56 GMT
Content-Type: text/html
Content-Length: 277
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Connection: Keep-Alive
ETag: "575e1f60-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytesnginx
咱們須要修改一下配置文件:c++
[root@xuegod68 nginx-1.8.0]# vim src/core/nginx.h //目的更改源碼隱藏軟件名稱和版本號sql
#define NGINX_VERSION "9.9.9" #此行修改的是你想要的版本號vim
#define NGINX_VER "sandiandian/" NGINX_VERSION #此行修改的是你想修改的軟件名稱
[root@xuegod68 nginx-1.8.0]# vim src/http/ngx_http_header_filter_module.c
//修改HTTP頭信息中的connection字段,防止回顯具體版本號
通用http頭域:
通用頭域包含請求和響應消息都支持的頭域,通用頭域包含Cache-Control、 Connection、Date、Pragma、Transfer-Encoding、Upgrade、Via。對通用頭域的擴展要求通信雙方都支持此擴展,若是存在不支持的通用頭域,通常將會做爲實體頭域處理。那麼也就是說有部分設備,或者是軟件,能獲取到connection,部分不能,要隱藏就要完全!
49 static char ngx_http_server_string[] = "Server:sandiandian" CRLF;
[root@xuegod68 nginx-1.8.0]# vim src/http/ngx_http_special_response.c
//這個文件定義了http錯誤碼的返回,有時候咱們頁面程序出現錯誤,Nginx會代咱們返回相應的錯誤代碼,回顯的時候,會帶上nginx和版本號,咱們把他隱藏起來
21 static u_char ngx_http_error_full_tail[] =
22 "<hr><center>" NGINX_VER "</center>" CRLF
23 "</body>" CRLF
24 "</html>" CRLF
25 ;
26
27
28 static u_char ngx_http_error_tail[] =
29 "<hr><center>nginx</center>" CRLF
30 "</body>" CRLF
31 "</html>" CRLF
ok ,到這個時候呢咱們編譯以前的優化已經完成,接下里咱們開始編譯安裝:
[root@xuegod68 nginx-1.8.0]#[root@xuegod63 nginx-1.9.12]# yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel –y #安裝依賴包,以避免在編譯過程當中出現報錯。
[root@xuegod68 ~]# tar xvf pcre-8.37.tar.bz2 -C /usr/local/ #pcre依賴 不須要編譯,下載解壓 就能夠
[root@xuegod68 ~]#./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.37
各個模塊功能呢,各位自行找資料腦補,我這兒就不進行普及了。 可使用 ./configure --help
[root@xuegod68 ~]#make && make install
ok 到這兒咱們已經完成了nginx的編譯安裝,接下來啓動測試
[root@xuegod68 ~]# /usr/local/nginx/sbin/nginx
[root@xuegod68 ~]# /usr/local/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) #若是出現這種報錯 就說明你的80端口被佔用,執行一下命令查看端口,關閉就好,通常是apche
[root@xuegod68 ~]#netstat -antup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2286/nginx #我這邊是正常的
[root@xuegod68 ~]#curl -I 127.0.0.1
Server: sandiandian/9.9.9 #效果很明顯
錯誤代碼測試:
[root@xuegod68 ~]#useradd -M -s /sbin/nologin nginx
[root@xuegod68 nginx]#vim /usr/local/nginx/conf/nginx.conf #修改運行用戶爲nginx 默認爲noboby
user nginx;
[root@xuegod68 nginx]#vim /usr/local/nginx/conf/nginx.conf
worker_processes 4;
[root@xuegod68 ~]# /usr/local/nginx/sbin/nginx -s reload
nginx 3211 0.0 0.2 22612 1448 ? S 17:27 0:00 nginx: worker process
nginx 3212 0.0 0.2 22612 1456 ? S 17:27 0:00 nginx: worker process
nginx 3213 0.0 0.2 22612 1456 ? S 17:27 0:00 nginx: worker process
nginx 3214 0.0 0.2 22612 1392 ? S 17:27 0:00 nginx: worker process
這根據你的CPU線程數配置
好比4核4線程配置
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
4線程的CPU,跑兩個進程,以下設置
worker_processes 2;
worker_cpu_affinity 0101 1010;
worker_processes最多開啓8個,8個以上性能提高不會再提高了,並且穩定性變得更低,8個進程已經夠用了。
[root@xuegod68 ~]#ulimit -n
1024
worker_rlimit_nofile 102400;
nginx分配請求並非很均勻,因此最好與ulimit -n的值保持一致。
14 events {
15 worker_connections 1024;
16 }
Work_connections是單個進程容許客戶端最大鏈接數,這個數值通常根據服務器性能和內存來制定,也就是單個進程最大鏈接數,實際最大值就是work進程數乘以這個數
19 http {
20 include mime.types;
21 default_type application/octet-stream;
22
23 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
24 # '$status $body_bytes_sent "$http_referer" '
25 # '"$http_user_agent" "$http_x_forwarded_for"';
26
27 #access_log logs/access.log main;
28
29 sendfile on;
30 #tcp_nopush on;
Include mime.types; 媒體類型
default_type application/octet-stream; 默認媒體類型 足夠
sendfile on; 開啓高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲off,以平衡磁盤與網絡I/O處理速度,下降系統的負載。注意:若是圖片顯示不正常把這個改爲off。
tcp_nopush on; 必須在sendfile開啓模式纔有效,防止網路阻塞,積極的減小網絡報文段的數量
主要目的是保護服務器資源,CPU,內存,控制鏈接數,tcp3次握手,4次揮手,創建鏈接是須要耗費資源的,若是發出鏈接,一直沒有鏈接,一直沒有獲得迴應,處於等待的所有斷掉
keepalive_timeout 60; # 客戶端鏈接保持會話超時時間,超過這個時間,服務器斷開這個連接
tcp_nodelay on; #防止網絡阻塞
client_header_timeout 15; #客戶端請求頭讀取超時時間,若是超過設個時間沒有發送任何數據,nginx將返回request time out的錯誤
client_body_timeout 15; #客戶端求主體超時時間,超過這個時間沒有發送任何數據,和上面同樣的錯誤提示
send_timeout 15; # 響應客戶端超時時間,這個超時時間僅限於兩個活動之間的時間,若是超哥這個時間,客戶端沒有任何活動,nginx關閉鏈接
http {
……
client_max_body_size 10m;
若是自己是一臺服務器的,不配置Fastcgi 是一個失敗的nginx,若是是做爲代理的話 就不須要配置
在nginx配置文件中是沒有Fastcgi選項的
配置以前,咱們須要瞭解這兩個概念:
Cache:寫入緩存區
Buffer:讀取緩存區
[root@xuegod68 ~]#free -m
total used free shared buffers cached
Fastcgi是靜態服務和動態服務的一個接口
修改nginx.conf配置文件,在http標籤中添加以下:
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#fastcgi_temp_path /data/ngx_fcgi_tmp;
fastcgi_cache_path /data/ngx_fcgi_cache levels=1:2
keys_zone=ngx_fcgi_cache:512m
inactive=1d max_size=40g;
在server location標籤添加以下:
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache ngx_fcgi_cache;
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 http://$host$request_uri;
}
#各個標籤的做用呢,因爲太多 我就不在這兒一一講解了,能夠參考如下網站。
官方文檔:
http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache
使用gzip壓縮功能,能夠爲咱們節約帶寬,加快速度,節約成本。在前面編譯的時候,咱們已經將這個模塊編譯進去了
配置壓縮的過程:
gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/css text/xml application/javascript;
gzip_vary on;
緩存,主要針對於圖片,css,js等元素更改機會比較少的狀況下使用:
當咱們第一次打開頁面的時候,可能沒什麼感受,當咱們第二次打開同一個的時候,咱們就會發現速度快了不少,這就是緩存的功勞。咱們能夠把這些緩存設置一個時間 15天,或者是更長的時間,視狀況而定。設置以下:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 3650d;
}
location ~ .*\.(js|css)?$
{
expires 30d;
}
同時也能夠對目錄及其進行判斷:
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
expires 360d;
}
location ~(robots.txt) {
expires 7d;
break;
}
expire優勢:
節約帶寬,節約服務器成本,節約人力成本,提高用戶體驗。
expire缺點:
由於緩存可能會給客戶提供的仍是舊頁面,反而下降用戶體驗,能夠經過縮短緩存時間,對緩存的對象更名,來解決這個問題。
nginx的調優呢,已經進入尾聲了,對於MySQL的優化,我會開新篇講解。