Nginx 優化配置
查看隱藏版本號:
curl -I http://192.168.106.138
HTTP/1.1 200 ok
Server:nginx/1.12.0
--------------------------------隱藏版本號方法 1-----------------------------------------
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
....
}
----------------------------------------隱藏版本號方法 2 ---------------------------------------
#define NGINX_VERSION "1.12.0" 在編譯安裝之前就修改
--------------------------------------------緩存時間-------------------------------------
vim /usr/local/nginx/conf/nginx.conf
location ~.(gif|jpg|jepg|png|bmp|ico)$ {
root html;
expires 1d;
}
---------------------------------------------日誌切割-----------------------------------
vim /opt/fenge.sh
chmod +x fenge.sh
腳本內容:結合周期 性計劃任務
#!/bin/bash
#Filename:fenge.sh
d=$(date -d "-1 day" "+%y%m%d") #只顯示一天前時間
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
kill -USR1 $(cat $pid_path)
find $logs_path -mtime +30 | xargs rm -rf
crontab -e
0 1 /opt/fenge.sh
---------------------------------超時設置------------------------------------------------
vim /usr/local/nginx/conf/nginx.conf
keepalive_timeout 65 180;
#180:客戶端超時時間
#65:服務器超時時間
client_header_timeout 80;
#請求頭部超時時間
client_body_timeout 80;
#body主體超時時間
#默認單位 “秒”
------------------------------------------更改進程數------------------------------------
cat /proc/cpuinfo | grep -c "physical" #CPU 核數
vim /usr/local/nginx/conf/nginx.conf
worker_processes 2
events {
worker_connections 4096;
}
ps aux | grep nginx #查看進程數量
--------------------------------網頁壓縮-----------------------------------------
vim /usr/local/nginx/conf/nginx.conf
添加如下內容:
gzip on;
gzip_buffers 4 64k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_min_length 1k;
gzip_vary on;
gzip_types text/plain text/javascript application/x-javascript text/csstext/xml application/xml application/xml+rss test/jpg test/png;
------------------------------------防盜鏈-------------------------------------------
vim /usr/local/nginx/conf/nginx.conf
添加如下內容:
location ~.(jpg|gif|swf)$ {
valid_referers none blocked *.baidu.com baidu.com;
if ( $invalid_referer ) {
rewrite / http://www.baidu.com/error.png;
}
}
---------------------------------------fpm參數優化---------------------------------------------
pid = run/php-fpm.pid
pm = dynamic
pm.max_children=20
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
Nginx 優化配置