nginx优化
一,配置Nginx隐藏版本号
yum -y install pcre-devel zlib-devel openssl-devel
useradd -M -s /sbin/nglogin nginx
tar xf nginx-1.6.2.tar.gz -C /usr/src
cd /usr/src/nginx-1.6.2
vim src/core/nginx.h
13 #define NGLNX_VERSION "12345"
14 #define NGINX_VER "lls/" NGINX_VERSION
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module && make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
ll /usr/local/bin/nginx
nginx
netstat -anpt |grep :80
[root@localhost ~]# curl -I http://192.168.30.23
HTTP/1.1 200 OK
Server: JD/12345
Date: Mon, 18 Mar 2019 13:21:52 GMT
Content-Type: text/html
Content-Length: 632
Last-Modified: Mon, 18 Mar 2019 13:00:38 GMT
Connection: keep-alive
ETag: "5c8f9676-278"
Accept-Ranges: bytes
二,配置nginx 网页缓存时间
vim /usr/local/nginx/conf/nginx.conf
location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
expires 1d;
}
location ~.*\.(js|css)$ {
expires 1h;
}
cd /usr/local/nginx/html
我桌面创建的一个1.jpg的图片把图片移到这个目录下/usr/local/nginx/html
killall -HUP nginx
ll /usr/local/nginx/html
Vim /usr/local/nginx/html/index.png
16 <img src=”1.jpg” />
访问http:192.168.30.23
进入fiddler抓包刷新查看网页图片的缓存时间,86400也就是一天的时间
当然在下面也能看到我改动的版本号
三,实现Nginx的日志切割
Vim /opt/cut_nginx_log.sh
#/bin/bash
#cut_nginx_log.sh
datetime=$(date -d "-1 day" "+%Y%m%d")
log_path="/usr/local/nginx/logs"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $log_path/backup ] || mkdir -p $log_path/backup
if [ -f $pid_path ]
then
mv $log_path/access.log $log_path/backup/access.log-$datetime
kill -USR1 $(cat $pid_path)
find $log_path/backup -mtime +30 | xargs rm -f
else
echo "Error,Nginx is not working!" | tee -a /var/log/messages
fi
~
三,更改Nginx运行进程数
[root@localhost ~]# grep 'core id' /proc/cpuinfo | uniq |wc -l
1
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
2 worker_processes 2;
[root@localhost ~]# killall -HUP nginx
[root@localhost ~]# ps aux |grep nginx | grep -v grep
root 10544 0.0 0.2 45984 2084 ? Ss 22:04 0:00 nginx: master process nginx
nginx 10842 0.0 0.2 48496 2020 ? S 22:28 0:00 nginx: worker process
nginx 10843 0.0 0.2 48496 2020 ? S 22:28 0:00 nginx: worker process
在一台4核物理服务器上,可进行下面的配置,将进程分配
Worker_cpu_affinity 0001 0010 0100 1000
三,配置Nginx实现网页压缩功能
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;
[root@localhost ~]# killall -HUP nginx
[root@localhost ~]# chmod +x /opt/cut_nginx_log.sh
[root@localhost ~]# crontab -e
0 0 * * * /
[root@localhost ~]# /opt/cut_nginx.sh
[root@localhost ~]# ls /usr/local/nginx/logs/backup/
access.log- access.log-20190317
[root@localhost ~]# killall nginx
[root@localhost ~]# /opt/cut_nginx_log.sh
Error,Nginx is not working!
四,配置实现连接超时
Vim /usr/local/nginx/conf/nginx.conf
34 keepalive_timeout 65;
35 client_header_timeout 60;
36 client_body_timeout 60;
Killall -HUP nginx
三,对FPM 模块进行参数优化
服务器为云服务器,运行了个人论坛,内存为1.5G.fpm进程数为20,内存消耗近1G,处理比较慢
优化参数调整
Vim /usr/local/php5/etc/php-fpm.conf
Pm = dynamic
Pm = start_servers = 5
Pm .min_spare_servers = 2
Pm.max_spare_servers = 8
三,自动索引
Vim /usr/local/nginx/conf/nginx.conf
location /download {
autoindex on;
}
[root@www ~]# cd /usr/local/nginx/html
[root@www html]# mkdir download/ dir{1,2} -p
[root@www html]# touch download/1.txt
[root@www html]# touch download/2.txt
[root@www ~]# killall -HUP nginx
浏览器访问http://192.168.30.24/download
四,页面访问404页面
error_page 403 404 /404.html;
location =/404.html{
root html;
}
[root@www html]# echo "Not Find " > /usr/local/nginx/html/404.html
[root@www html]# killall -HUP nginx
浏览器访问 http://192.168.30.24/123
显示Not Find
五,目录别名功能
[root@www html]# mkdir Centos RedHat
[root@www ~]# echo "hello,students" > /usr/local/nginx/html/RedHat/index.html
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
location /Centos {
alias /usr/local/nginx/html/RedHat;
}
[root@www ~]# service nginx restart
浏览器访问http://192.168.30.24/Centos