nginx常规的优化
nginx深入优化
[root@localhost conf]# curl -I http://192.168.247.190
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 24 Dec 2019 00:44:45 GMT
Content-Type: text/html
Content-Length: 16
Last-Modified: Thu, 19 Dec 2019 09:39:46 GMT
Connection: keep-alive
ETag: "5dfb4562-10"
Accept-Ranges: bytes
[root@localhost conf]#
[root@localhost conf]# vim nginx.conf
http {
‘ server_tokens off;
//增加上面参数
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
[root@localhost conf]# curl -I http://192.168.247.190
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 24 Dec 2019 00:47:48 GMT
Content-Type: text/html
Content-Length: 16
Last-Modified: Thu, 19 Dec 2019 09:39:46 GMT
Connection: keep-alive
ETag: "5dfb4562-10"
Accept-Ranges: bytes
备注:最好在安装编译之前,修改
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
server_tokens on;
//开启版本
[root@localhost conf]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@localhost nginx]# cd conf/
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf scgi_params.default
fastcgi.conf.default koi-win nginx.conf.bak uwsgi_params
fastcgi_params mime.types nginx.conf.default uwsgi_params.default
fastcgi_params.default mime.types.default scgi_params win-utf
[root@localhost conf]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@localhost nginx-1.12.2]# cd src/
[root@localhost src]# ls
core event http mail misc os stream
[root@localhost src]# cd core/
//core 代表内核
[root@localhost core]# ls
nginx.c ngx_cycle.h ngx_output_chain.c ngx_rwlock.c
nginx.h ngx_file.c ngx_palloc.c ngx_rwlock.h
ngx_array.c ngx_file.h ngx_palloc.h ngx_sha1.c
ngx_array.h ngx_hash.c ngx_parse.c ngx_sha1.h
ngx_buf.c ngx_hash.h ngx_parse.h ngx_shmtx.c
ngx_buf.h ngx_inet.c ngx_parse_time.c ngx_shmtx.h
ngx_conf_file.c ngx_inet.h ngx_parse_time.h ngx_slab.c
ngx_conf_file.h ngx_list.c ngx_proxy_protocol.c ngx_slab.h
ngx_config.h ngx_list.h ngx_proxy_protocol.h ngx_spinlock.c
ngx_connection.c ngx_log.c ngx_queue.c ngx_string.c
ngx_connection.h ngx_log.h ngx_queue.h ngx_string.h
ngx_core.h ngx_md5.c ngx_radix_tree.c ngx_syslog.c
ngx_cpuinfo.c ngx_md5.h ngx_radix_tree.h ngx_syslog.h
ngx_crc32.c ngx_module.c ngx_rbtree.c ngx_thread_pool.c
ngx_crc32.h ngx_module.h ngx_rbtree.h ngx_thread_pool.h
ngx_crc.h ngx_murmurhash.c ngx_regex.c ngx_times.c
ngx_crypt.c ngx_murmurhash.h ngx_regex.h ngx_times.h
ngx_crypt.h ngx_open_file_cache.c ngx_resolver.c
ngx_cycle.c ngx_open_file_cache.h ngx_resolver.h
[root@localhost core]# vim nginx.h
13 #define NGINX_VERSION "8.8.8"
[root@localhost core]# cd ../../
[root@localhost nginx-1.12.2]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
server_tokens on;
[root@localhost nginx-1.12.2]# service nginx stop
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# curl -I http://192.168.247.190
HTTP/1.1 200 OK
Server: nginx/8.8.8
Date: Tue, 24 Dec 2019 00:58:48 GMT
Content-Type: text/html
Content-Length: 16
Last-Modified: Thu, 19 Dec 2019 09:39:46 GMT
Connection: keep-alive
ETag: "5dfb4562-10"
Accept-Ranges: bytes
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 1024;
}
’ user nginx nginx;
//新增
http {
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
[root@localhost html]# ps aux | grep nginx
root 8560 0.0 0.0 20548 648 ? Ss 09:37 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 8562 0.0 0.0 23012 1672 ? S 09:37 0:00 nginx: worker process
root 8632 0.0 0.0 112728 972 pts/0 R+ 09:41 0:00 grep --color=auto nginx
location ~\.(gif|jpeg|jpg|ico|bmp|png)$ {
root html;
expires 1d;
}
[root@localhost abc]# cp qq.jpg /usr/local/nginx/html/
[root@localhost abc]# cd /usr/local/
[root@localhost local]# ls
bin etc games include lib lib64 libexec nginx sbin share src
[root@localhost local]# cd nginx/html/
[root@localhost html]# ls
50x.html index.html qq.jpg
[root@localhost html]# vim index.html
15
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
location ~\.(gif|jpeg|jpg|ico|bmp|png)$ {
root html;
expires 1d;
}
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
xargs 参数,是指把前面的操作结果作为后面的参数
[root@localhost html]# date
2019年 12月 24日 星期二 10:38:38 CST
[root@localhost html]# date -d "0 day" "+%Y%m%d"
20191224
[root@localhost html]# date -d "-1 day" "+%Y%m%d"
20191223
[root@localhost html]# date -d "+1 day" "+%Y%m%d"
20191225
[root@localhost html]# vim /opt/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}/tase.cpm-access.log-$d
kill -USR1 $(cat $pid_path)
find $logs_path -mtime +30 | xargs rm -rf
[root@localhost html]# cd /opt
[root@localhost opt]# chmod +x fenge.sh
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# cd /var/log/nginx/
[root@localhost nginx]# ls
tase.cpm-access.log-20191223
[root@localhost nginx]# date
2019年 12月 24日 星期二 14:31:38 CST
[root@localhost nginx]# date -s 2019-12-25
2019年 12月 25日 星期三 00:00:00 CST
[root@localhost nginx]# cd -
/opt
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# cd -
/var/log/nginx
[root@localhost nginx]# ls
tase.cpm-access.log-20191223 tase.cpm-access.log-20191224
[root@localhost nginx]# cd /usr/local/nginx/logs/
[root@localhost logs]# ls
access.log error.log kgc8080 nginx.pid
[root@localhost logs]#
[root@localhost html]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf scgi_params.default
fastcgi.conf.default koi-win nginx.conf.bak uwsgi_params
fastcgi_params mime.types nginx.conf.default uwsgi_params.default
fastcgi_params.default mime.types.default scgi_params win-utf
[root@localhost conf]#
[root@localhost conf]# vim nginx.conf
keepalive_timeout 65 180;
client_header_timeout 80;
client_body_timeout 80;
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
第一个是客户端的超时时间,第二个是服务端的超时时间,参数放在http中
以上就是进行超时时间的设置
[root@localhost conf]# ps aux |grep nginx
root 8792 0.0 0.0 20548 644 ? Ss 09:48 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 8794 0.0 0.0 23012 1420 ? S 09:48 0:00 nginx: worker process
root 8833 0.0 0.0 112728 972 pts/0 S+ 09:51 0:00 grep --color=auto nginx
[root@localhost conf]#
其中,grep的进程不用看,master是主进程,但是它不干活,干活的是worker,现在只有一个worker,原因看配置文件
[root@localhost conf]# vim nginx.conf
worker_processes 1;
此处设置为1个进程数
worker_connections 1024;
一个进程数可以完成1024个工作量
垂直扩展
关闭服务器,init 0
然后扩展服务器cpu
原本
然后重新开启服务器
[root@localhost ~]# cd /proc
[root@localhost proc]# cat cpuinfo
processor : 0
processor : 1
[root@localhost proc]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2;
worker_cpu_affinity 01 10;
//增加上面一行
[root@localhost proc]# service nginx start
[root@localhost proc]# ps aux | grep nginx
root 2647 0.0 0.0 20548 648 ? Ss 10:06 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 2649 0.0 0.0 23012 1412 ? S 10:06 0:00 nginx: worker process
nginx 2650 0.0 0.0 23012 1420 ? S 10:06 0:00 nginx: worker process
root 2665 0.0 0.0 112728 972 pts/0 S+ 10:07 0:00 grep --color=auto nginx
length 指超过1K内容的网页才进行压缩
buffers 缓存区
版本基本上可以不设
gzip_comp_level 2 :用来指定gzip压缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理速度最快,使用默认即可
gzip_types text/plain : 压缩类型,是就对哪些网页文档启用压缩功能
gzip_vary on :选项可以让前端的缓存服务器缓存经过gzip压缩的页面
[root@localhost proc]# 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 6;
gzip_types text/plain application/x-javascript text/css image/jpg image/j
peg image/png image/gif application/xml text/javascript application/x-httpd-p
hp application/javascript application/json;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
[root@localhost html]# systemctl stop firewalld.service
[root@localhost html]# setenforce 0
除非用本地的域名去访问
如果匹配的是无效的,否则就跳转一个error.png页面
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html index.html qq.jpg
[root@localhost html]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:
[root@localhost html]# cd /abc
[root@localhost abc]# ls
error.png
[root@localhost abc]# cp error.png /usr/local/nginx/html/
[root@localhost abc]# cd -
/usr/local/nginx/html
[root@localhost html]# ls
50x.html error.png index.html qq.jpg
[root@localhost html]# grep "qq.jpg" index.html
[root@localhost html]# service nginx start
[root@localhost html]# netstat -natp | grep 80
tcp 0 0 192.168.247.193:80 0.0.0.0:* LISTEN 3038/nginx: master
客户端去访问
使用域名,需要使用dns解析服务
搭建dns详见我前面的博客
然后指定盗链主机和客户机的dns服务器
开始设置盗机
在win10内打开控制面板,控制面板内打开程序和功能,里面有开启或者关闭功能,点击
然后点击internet informatoion servers
更改扩展名为html
打开internet information servers
现在已经默认开启
把网页移动到站点目录下
关闭盗机防火墙
然后客户端去访问盗机网站
盗链成功,接下来做防盗链操作
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
44 location ~*\.(jpg|gif|swf)$ {
45 valid_referers none blocked *.shl.com shl.com;
46 if ( $invalid_referer ) {
47 rewrite ^/ http://www.shl.com/error.png;
48 }
49 }
验证
重启nginx服务
[root@localhost named]# service nginx stop
[root@localhost named]# service nginx start
再次查看
备注:使用ip地址的方式去访问被盗链网站,也会显示error图片
注意两台服务器的防火墙要关掉
最小空闲进程数是在不着急的时候最少的进程数
主要看设置的空闲占用率
[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf
17 pid = run/php-fpm.pid
18 pm = dynamic
19 pm.max_children=20
20 pm.static_servers = 5
21 pm.min_spare_servers = 2
22 pm.max_spare_servers = 8
servers:动态方式下初始的fpm进程数量
最小空闲进程数是在不着急的时候最少的进程数
主要看设置的空闲占用率
[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf
17 pid = run/php-fpm.pid
18 pm = dynamic
19 pm.max_children=20
20 pm.static_servers = 5
21 pm.min_spare_servers = 2
22 pm.max_spare_servers = 8
[外链图片转存中…(img-kEwiGL1R-1577320861181)]