nginx优化服务
一、隱藏版本號
隱藏Nginx版本號,避免安全漏洞泄漏
Nginx隱藏版本號的方法
修改配置文件法
修改源碼法
1、查看版本號
可以使用 Fiddler 工具抓取數(shù)據(jù)包,查看 Nginx版本
在 CentOS 中使用命令 curl -I http://192.168.172.20 顯示響應(yīng)報文首部信息。
curl -I http://192.168.172.20
2、隱藏版本號
方法一: vim /usr/local/nginx/conf/nginx.conf http {include mime.types;default_type application/octet-stream;#20行左右;添加;關(guān)閉版本號server_tokens off;...... }systemctl restart nginx curl -I http://192.168.172.20方法二:
vim /opt/nginx-1.12.0/src/core/nginx.h
#13行;修改版本號
#define NGINX_VERSION “1.1.1”
#14行;修改服務(wù)器類型
#define NGINX_VER “IIS” NGINX_VERSION
cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens on;
…
}
systemctl restart nginx
curl -I http://192.168.172.20
二、修改用戶與組
vim /usr/local/nginx/conf/nginx.conf
#取消注釋,修改用戶為nginx,組為 nginx
user nginx nginx;
systemctl restart nginx
#主進程由root創(chuàng)建,子進程由nginx創(chuàng)建
ps aux | grep nginx
三、緩存時間
vim /usr/local/nginx/conf/nginx.conf
http {
…
server {
…
location / {
root html;
index index.html index.htm;
}
…
}
}
systemctl restart nginx.service
在Linux系統(tǒng)中,打開火狐瀏覽器,右擊點查看元素 選擇 網(wǎng)絡(luò) —> 選擇 HTML、WS、其他 訪問http://192.168.172.20/,雙擊200響應(yīng)消息查看響應(yīng)頭中包含 Cahce-Control:max-age=86400 表示緩存時間是 86400 秒。也就是緩存一天的時間,一天之內(nèi)瀏覽器訪問這個頁面,都是用緩存中的數(shù)據(jù),而不需要向 Nginx 服務(wù)器重新發(fā)出請求,減少了服務(wù)器的使用帶寬。
四、日志分割
vim /opt/fenge.sh
#!/bin/bash
#設(shè)置變量
#設(shè)置顯示前一天的時間的變量
d=$(date -d “-1 day” “+%F”)
logs_path="/var/log/nginx"
pid_path=cat /usr/local/nginx/logs/nginx.pid
#創(chuàng)建日志文件目錄
[ -d $logs_path ] || mkdir -p $logs_path
#移動并重命名日志文件
mv /usr/local/nginx/logs/access.log KaTeX parse error: Expected '}', got 'EOF' at end of input: …om-access.log-{d}
#重建日志文件
kill -USR1 $pid_path
#刪除30天前的日志文件
find $logs_path -mtime +30 -exec rm -rf {} ;
#find $logs_path -mtime +30 |xargs rm -rf
source fenge.sh
ls /var/log/nginx
ls /usr/local/nginx/logs/
crontab -e
0 1 * * * /opt/fenge.sh
小知識
在linux操作系統(tǒng)中,每個文件都有很多的時間參數(shù),其中有三個比較主要,分別是ctime,atime,mtime ctime(status time): 當修改文件的權(quán)限或者屬性的時候,就會更新這個時間,ctime并不是create time,更像是change time, 只有當更新文件的屬性或者權(quán)限的時候才會更新這個時間,但是更改內(nèi)容的話是不會更新這個時間。
atime(accesstime): 當使用這個文件的時候就會更新這個時間。
mtime(modification time): 當修改文件的內(nèi)容數(shù)據(jù)的時候,就會更新這個時間,而更改權(quán)限或者屬性,mtime不會改變,這就是和ctime的區(qū)別。
五、連接超時
HTTP有一個KeepAlive模式,它告訴web服務(wù)器在處理完一個請求后保持這個TCP連接的打開狀態(tài)。若接收到來自客戶端的其它請求,服務(wù)端會利用這個未被關(guān)閉的連接,而不需要再建立一個連接。 KeepAlive 在一段時間內(nèi)保持打開狀態(tài),它們會在這段時間內(nèi)占用資源。占用過多就會影響性能。
vim /usr/local/nginx/conf/nginx.conf
http {
…
keepalive_timeout 65 180;
client_header_timeout 80;
client_body_timeout 80;
…
}
systemctl restart nginx.service
keepalive_timeout
指定KeepAlive的超時時間(timeout)。指定每個TCP連接最多可以保持多長時間,服務(wù)器將會在這個時間后關(guān)閉連接。 Nginx的默認值是65秒,有些瀏覽器最多只保持 60 秒,所以可以設(shè)定為 60 秒。若將它設(shè)置為0,就禁止了keepalive 連接。
第二個參數(shù)(可選的)指定了在響應(yīng)頭Keep-Alive:timeout=time中的time值。這個頭能夠讓一些瀏覽器主動關(guān)閉連接,這樣服務(wù)器就不必去關(guān)閉連接了。沒有這個參數(shù),Nginx 不會發(fā)送 Keep-Alive 響應(yīng)頭。
client_header_timeout
客戶端向服務(wù)端發(fā)送一個完整的 request header 的超時時間。如果客戶端在指定時間內(nèi)沒有發(fā)送一個完整的 request header,Nginx 返回 HTTP 408(Request Timed Out)。
client_body_timeout
指定客戶端與服務(wù)端建立連接后發(fā)送 request body 的超時時間。如果客戶端在指定時間內(nèi)沒有發(fā)送任何內(nèi)容,Nginx 返回 HTTP 408(Request Timed Out)。
六、更改進程數(shù)
在高并發(fā)場景,需要啟動更多的Nginx進程以保證快速響應(yīng),以處理用戶的請求,避免造成堵塞。
#查看cpu核數(shù)
cat /proc/cpuinfo | grep -c “physical id”
#查看nginx主進程中包含幾個子進程
ps aux | grep nginx
vim /usr/local/nginx/conf/nginx.conf
worker_processes 2; 修改為核數(shù)相同或者2倍
worker_cpu_affinity 01 10; 設(shè)置每個進程由不同cpu處理,進程數(shù)配2 4 6 8分別為0001 0010 0100 1000
systemctl restart nginx
七、配置網(wǎng)頁壓縮
vim /usr/local/nginx/conf/nginx.conf
http {
…
gzip on; #取消注釋,開啟gzip壓縮功能
gzip_min_length 1k; #最小壓縮文件大小
gzip_buffers 4 16k; #壓縮緩沖區(qū),大小為4個16k緩沖區(qū)
gzip_http_version 1.1; #壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)
gzip_comp_level 6; #壓縮比率
gzip_vary on; #支持前端緩存服務(wù)器存儲壓縮頁面
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json; #壓縮類型,表示哪些網(wǎng)頁文檔啟用壓縮功能
…
}
cd /usr/local/nginx/html
先將haoren.jpg文件傳到/usr/local/nginx/html目錄下
vim index.html
…
#網(wǎng)頁中插入圖片
systemctl restart nginx
在Linux系統(tǒng)中,打開火狐瀏覽器,右擊點查看元素
選擇 網(wǎng)絡(luò) —> 選擇 HTML、WS、其他
訪問 http://192.168.172.20 ,雙擊200響應(yīng)消息查看響應(yīng)頭中包含 Content-Encoding: gzip
八、配置防盜鏈
vim /usr/local/nginx/conf/nginx.conf
http {
…
server {
…
location ~*.(jpg|gif|swf)$ {
valid_referers *.zhangsan.com zhangsan.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.zhangsan.com/error.png;
#return 403;
}
}
…
}
}
~* .(jpg|gif|swf)$ :這段正則表達式表示匹配不區(qū)分大小寫,以.jpg 或.gif 或.swf 結(jié)尾的文件;
valid_referers :設(shè)置信任的網(wǎng)站,可以正常使用圖片;
后面的網(wǎng)址或者域名 :referer 中包含相關(guān)字符串的網(wǎng)址;
if語句:如果鏈接的來源域名不在valid_referers所列出的列表中,$invalid_referer為1,則執(zhí)行后面的操作,即進行重寫或返回 403 頁面。
網(wǎng)頁準備:
Web源主機(192.168.172.20)配置:
cd /usr/local/nginx/html
將game.jpg、error.png文件傳到/usr/local/nginx/html目錄下
vim index.html
…
echo “192.168.172.20 www.zhangsan.com” >> /etc/hosts
盜鏈網(wǎng)站主機(192.168.172.10):
cd /usr/local/nginx/html
vim index.html
…
echo “192.168.172.10 www.lisi.com” >> /etc/hosts
echo “192.168.172.20 www.zhangsan.com” >> /etc/hosts
在盜圖網(wǎng)站主機上進行瀏覽器驗證
http://www.lisi.com
九、fpm參數(shù)優(yōu)化
vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
vim /usr/local/php/etc/php-fpm.d/www.conf
–96行–
pm = dynamic #fpm進程啟動方式,動態(tài)的
–107行–
pm.max_children=20 #fpm進程啟動的最大進程數(shù)
–112行–
pm.start_servers = 5 #動態(tài)方式下啟動時默認開啟的進程數(shù),在最小和最大之間
–117行–
pm.min_spare_servers = 2 #動態(tài)方式下最小空閑進程數(shù)
–122行–
pm.max_spare_servers = 8 #動態(tài)方式下最大空閑進程數(shù)
kill -USR2 cat /usr/local/php/var/run/php-fpm.pid #重啟php-fpm
netstat -anpt | grep 9000
總結(jié)
- 上一篇: Nginx网页服务
- 下一篇: LVS-DR+Keepalived 高可