?查看版本號
修改配置文件的版本號
1 )[root@xuegod1 nginx-1.8.1]# vim src/core/nginx.h
2 )
3 ) vim src/http/ngx_http_header_filter_module.c?
?? 新半杯不用修改這裡否則出錯
4 )[root@xuegod1 nginx-1.8.1]# vim src/http/ngx_http_special_response.c
? 16.1.2?? Nginx? 正式安裝
?1 ) 安裝依賴??? yum? install gcc gcc-c++? autoconf? automake zlib?? zlib-devel??? openssl openssl-devel? pcre pcre-devel ??? -y?
預編譯?
2 )./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
3 )? make -j 4 && make install
[root@xuegod1 nginx-1.8.1]# echo $?
0
4 ) [root@xuegod1 local]# cd nginx/
[root@xuegod1 nginx]# ls
conf? html? logs? sbin
[root@xuegod1 nginx]# ./sbin/nginx? ?? 啟動nginx?
[root@xuegod1 nginx]# lsof -i:80
COMMAND? PID? USER? FD? TYPE DEVICE SIZE/OFF NODE NAME
nginx? 14167? root? ? 6u? IPv4? 80803? ? ? 0t0? TCP *:http (LISTEN)
nginx? 14168 nobody? ? 6u? IPv4? 80803? ? ? 0t0? TCP *:http (LISTEN)
修改運行用戶?
[root@xuegod1 nginx]# useradd -M -s /sbin/nologin nginx
[root@xuegod1 nginx]# id nginx
uid=1001(nginx) gid=1001(nginx) 組=1001(nginx)
[root@xuegod1 nginx]# vim conf/nginx.conf
重啟查看
[root@xuegod1 nginx]# vim conf/nginx.conf
再次重啟
[root@xuegod1 nginx]# ./sbin/nginx -s reload
[root@xuegod1 nginx]# lsof -i:80?? 已經(jīng)看到有四個? nginx? 運行了
COMMAND? PID? USER? FD? TYPE DEVICE SIZE/OFF NODE NAME
nginx? 14167? root? ? 6u? IPv4? 80803? ? ? 0t0? TCP *:http (LISTEN)
nginx? 14450 nginx? ? 6u? IPv4? 80803? ? ? 0t0? TCP *:http (LISTEN)
nginx? 14451 nginx? ? 6u? IPv4? 80803? ? ? 0t0? TCP *:http (LISTEN)
nginx? 14452 nginx? ? 6u? IPv4? 80803? ? ? 0t0? TCP *:http (LISTEN)
nginx? 14453 nginx? ? 6u? IPv4? 80803? ? ? 0t0? TCP *:http (LISTEN)
16.1.3? Nginx? 運行CPU親和力?
Nginx?? 最多可以打開文件數(shù)
查看內(nèi)存
[root@xuegod1 nginx]# cat /proc/sys/fs/file-max
196609
16.1.4? 單個進程允許客戶端最大并發(fā)連接數(shù)
top? -u? nginx??
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
14450 nginx? ? 20? 0? 27352? 1548? ? 644 S? 0.0? 0.1? 0:00.19 nginx? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
14451 nginx? ? 20? 0? 27352? 1540? ? 644 S? 0.0? 0.1? 0:00.19 nginx? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
14452 nginx? ? 20? 0? 27352? 1540? ? 644 S? 0.0? 0.1? 0:00.19 nginx? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
14453 nginx? ? 20? 0? 27352? 1516? ? 644 S? 0.0? 0.1? 0:00.00 nginx
1.5M? 左右
[root@xuegod1 nginx]# vim conf/mime.types
在配置文件中添加
自定義連接時間
29 sendfile on;
30? ? #tcp_nopush? ? on;
31
32? ? #keepalive_timeout? 0;
33? ? keepalive_timeout? 65;
34? ? tcp_nodelay on;
35? ? client_header_timeout 15;
36? ? client_body_timeout 15;
37? ? send_timeout 15;
限制文件大小?? 上傳文件大小
在40行左右
16.2.1? Fastigi?? 調(diào)優(yōu)?
Nginx? 沒有配置factcgi??? 。你使用nginx? 是一個失敗的方法
Nginx沒有配置factcgi,你使用nginx是一個失敗的方法
配置之前。了解幾個概念:
Cache:寫入緩存區(qū)
Buffer:讀取緩存區(qū)
Fastcgi是靜態(tài)服務和動態(tài)服務的一個接口
fastcgi_connect_timeout?300;????#指定鏈接到后端FastCGI的超時時間雀监。
fastcgi_send_timeout?300;???????#向FastCGI傳送請求的超時時間敢靡,這個值是指已經(jīng)完成兩次握手后向FastCGI傳送請求的超時時間彩扔。
fastcgi_read_timeout?300;???????#指定接收FastCGI應答的超時時間址晕,這個值是指已經(jīng)完成兩次握手后接收FastCGI應答的超時時間痹愚。
fastcgi_buffer_size?64k;????????#指定讀取FastCGI應答第一部分需要用多大的緩沖區(qū),這個值表示將使用1個64KB的緩沖區(qū)讀取應答的第一部分(應答頭)入篮,可以設置為fastcgi_buffers選項指定的緩沖區(qū)大小陈瘦。
fastcgi_buffers?4?64k;??#指定本地需要用多少和多大的緩沖區(qū)來緩沖FastCGI的應答請求,如果一個php腳本所產(chǎn)生的頁面大小為256KB崎弃,那么會分配4個64KB的緩沖區(qū)來緩存,如果頁面大小大于256KB含潘,那么大于256KB的部分會緩存到fastcgi_temp指定的路徑中饲做,但是這并不是好方法,因為內(nèi)存中的數(shù)據(jù)處理速度要快于磁盤遏弱。一般這個值應該為站點中php腳本所產(chǎn)生的頁面大小的中間值盆均,如果站點大部分腳本所產(chǎn)生的頁面大小為256KB,那么可以把這個值設置為“8 16K”漱逸、“4 64k”等泪姨。
fastcgi_busy_buffers_size?128k;?#建議設置為fastcgi_buffer的兩倍,繁忙時候的buffer
fastcgi_temp_file_write_size?128k;??#在寫入fastcgi_temp_path時將用多大的數(shù)據(jù)庫饰抒,默認值是fastcgi_buffers的兩倍肮砾,設置上述數(shù)值設置小時若負載上來時可能報502Bad Gateway
fastcgi_cache?gnix;?#表示開啟FastCGI緩存并為其指定一個名稱。開啟緩存非常有用袋坑,可以有效降低CPU的負載仗处,并且防止502的錯誤發(fā)生,但是開啟緩存也可能會引起其他問題枣宫,要很據(jù)具體情況選擇
fastcgi_cache_valid?200?302?1h;?#用來指定應答代碼的緩存時間婆誓,實例中的值表示將200和302應答緩存一小時,要和fastcgi_cache配合使用
fastcgi_cache_valid?301?1d;?????#將301應答緩存一天
fastcgi_cache_valid?any?1m;?????#將其他應答緩存為1分鐘
fastcgi_cache_min_uses?1;???????#請求的數(shù)量
fastcgi_cache_path??????????????#定義緩存的路徑
配置文件中
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=2:2???#緩存路徑也颤,levels目錄層次2級
keys_zone=ngx_fcgi_cache:512m ?#定義了一個存儲區(qū)域名字洋幻,緩存大小
inactive=1d max_size=40g; ?????#不活動的數(shù)據(jù)在緩存中多長時間,目錄總大小
再次重啟
?# ./sbin/nginx -s reload
在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
在server? 字段里面?
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功能優(yōu)點
(1)expires可以降低網(wǎng)站購買的帶寬翅娶,節(jié)約成本
(2)同時提升用戶訪問體驗
(3)減輕服務的壓力文留,節(jié)約服務器成本,甚至可以節(jié)約人力成本竭沫,是web服務非常重要的功能厂庇。
expire功能缺點:
被緩存的頁面或數(shù)據(jù)更新了,用戶看到的可能還是舊的內(nèi)容输吏,反而影響用戶體驗权旷。
解決辦法:
第一個縮短緩存時間,例如:1天,不徹底拄氯,除非更新頻率大于1天
第二個對緩存的對象改名
a.圖片躲查,附件一般不會被用戶修改,如果用戶修改了译柏,實際上也是更改文件名重新傳了而已
b.網(wǎng)站升級對于js镣煮,css元素,一般可以改名鄙麦,把css典唇,js,推送到CDN胯府。
網(wǎng)站不希望被緩存的內(nèi)容
1)廣告圖片
2)網(wǎng)站流量統(tǒng)計工具
3)更新頻繁的文件(google的logo)
? nginx? 的日志
? 做日志切割?
16.5.1 ?日志切割優(yōu)化
[root@xuegod63 ~]# cd /usr/local/nginx/logs/
日志優(yōu)化的目的介衔,是為了一天日志一壓縮,按天存放骂因,超過10天的刪除
[root@xuegod63 logs]# vim cut_nginx_log.sh //每天日志分割腳本
(tar ?tvf 2017-011-08.tar.bz2 顯示文件內(nèi)容)
#!/bin/bash
date=$(date +%F -d -1day)
cd?/usr/local/nginx/logs
if?[?!?-d?cut ]?;?then
????????mkdir?cut
fi
mv?access.log cut/access_$(date +%F -d -1day).log
mv?error.log cut/error_$(date +%F -d -1day).log
/usr/local/nginx/sbin/nginx -s reload
tar?-jcvf cut/$date.tar.bz2 cut/*
rm?-rf cut/access*?&&?rm?-rf cut/error*
cat?>>/var/spool/cron/root<<eof
00 00 * * * /bin/sh /usr/local/nginx/logs/cut_nginx_log.sh >/dev/null 2>&1
eof
find?-type f -mtime +10?|?xargs?rm -rf
創(chuàng)建目錄
[root@xuegod1 nginx]# mkdir /xuegod/logs -pv
mkdir: 已創(chuàng)建目錄 "/xuegod"
mkdir: 已創(chuàng)建目錄 "/xuegod/logs"
[root@xuegod1 nginx]#? chown -R? root.root? /xuegod/logs/?
修稿權限
禁止訪問images? 下的php?? 文件
location ~?^/images/.*\.(php|php5|.sh|.py|.pl)$ {
????????????deny all;
????????}
創(chuàng)建PHP? 文件
[root@xuegod1 nginx]# mkdir /usr/local/nginx/html/images
[root@xuegod1 nginx]#? echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/images/index.php
開始測試訪問
防治DDOS 工具?
[root@xuegod1 nginx]# yum install httpd-tools -y
[root@xuegod1 nginx]# ab -c 200 -t 1 http://192.168.24.68/index.html