目录
一、连接超时
vim /usr/local/nginx/conf/nginx.conf
#keepalive_timeout 0;
keepalive_timeout 100;
client_header_timeout 80; #等待客户端发送请求头的超时时间,超时会发送408错误
client_body_timeout 80; #等待客户端发送请求体的超时时间
cd /usr/local/nginx/
cd conf
vim nginx.conf
二、更改进程数
cd /sys/fs/cgroup
cd cpu
cat cpu.cfs_quota_us
cd cpu
cd /proc/
cat cpuinfo
cat meminfo 或者 cat /proc/meminfo
cat /proc/cpuinfo | grep -c "physical"
ps aux | grep nginx
ps aux | grep nginx | grep -v grep
ps aux | grep nginx | grep -v grep | wc -l
cd /usr/local/nginx/
cd conf/
vim nginx.conf
worker_processes 2; 修改与CPU核数相同或2倍(cgroup)
worker_cpu_affinity 01 10; 设置每个进程由不同的cpu处理、进程数配为4时,为0001 0010 0100 1000
三、网页压缩
cd /usr/local/nginx/
cd conf/
vim nginx.conf
gzip on; 取消注释,开启gzip压缩功能
gzip_min_length 1k; 最小压缩文件大小
gzip_buffers 4 16k; 压缩缓冲区,大小为4个16k缓冲区
gzip_http_version 1.1; 压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 6; 压缩比率
gzip_vary on; 支持前端缓存服务器存储压缩页面
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; 压缩类型,表示哪些网页文档启用压缩功能
nginx -t
systemctl restart nginx
四、防盗链
打开另外一台虚拟机(server)
yum -y install gcc gcc-c++ pcre-devel zlib-devel make
useradd -M -s /sbin/nologin nginx
cd /opt
tar xzvf nginx-1.15.9.tar.gz
cd nginx-1.15.9/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
curl -I 192.168.22.228
cd /usr/local/nginx/
cd html
vim index.html
vim /etc/hosts
cp /opt/a.jpg ./
切换到原来的虚拟机(盗链端)
cd ..
cd html/
vim index.html
vim /etc/hosts
cd /usr/local/nginx/
cd conf/
vim nginx.conf
location ~* \.(jpg|gif|swf)$ {
valid_referers *.dog.com dog.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.dog.com/error.png;
}
}
nginx -t
systemctl restart nginx
~* .(jpg|gif|jepg|bmp|ico)$ 表示匹配不区分大小写,以.jpg 或.gif 或.swf 结尾的文件
五、fpm优化
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
pid = run/php-fpm.pid
vim /usr/local/php/etc/php-fpm.d/www.conf
96行
pm = dynamic fpm进程启动方式,动态的
107行
pm.max_children=20 fpm进程启动的最大进程数
112行
pm.start_servers = 5 动态方式下启动时默认开启的进程数,在最小和最大之间
117行
pm.min_spare_servers = 2 动态方式下最小空闲进程数
122行
pm.max_spare_servers = 8 动态方式下最大空闲进程数
启动php-fpm,不可用于重启
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
执行第一个命令后,就可以使用下面这条命令查看pid号重启php-fpm
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
netstat -anpt | grep 9000