Nginx 优化配置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx 优化配置相关的知识,希望对你有一定的参考价值。
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 ---------------------------------------
vim /opt/nginx-1.12.0/src/core/nginx.h
#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 优化配置的主要内容,如果未能解决你的问题,请参考以下文章