电子商务网站推广方法和技巧,网站管理公司 优帮云,电子商务网站建设投资预算,怎样推广app别人才愿意下载Nginx优化服务和防盗链一、长连接1、修改主配置文件2、测试3、在主配置文件添加4、验证二、Nginx第三方模块1、开源的echo模块2、查看是否成功3、加echo模块步骤4、网页测试验证三、搭建虚拟主机1、编译安装好nginx后#xff0c;对主配置文件进行修改2、创建文件3、验证四、防…
Nginx优化服务和防盗链一、长连接1、修改主配置文件2、测试3、在主配置文件添加4、验证二、Nginx第三方模块1、开源的echo模块2、查看是否成功3、加echo模块步骤4、网页测试验证三、搭建虚拟主机1、编译安装好nginx后对主配置文件进行修改2、创建文件3、验证四、防盗链1、先yum安装nginx两台机子都要装2、网页准备3、Web源主机配置防盗链五、配置网页压缩六、日志分割七、修改Nginx运行的用户和组一、长连接
长连接是指在一个连接上可以连续发送多个数据包在连接保持期间如果没有数据包发送需 … 短连接是指通讯双方有数据交互时就建立一个连接数据发送完成后则断开此连接即每次连接只完成一项业务的发送。
1、修改主配置文件
[rootwww ~]#vim /apps/nginx/conf/nginx.conf
http {include mime.types;include /apps/nginx/conf.d/*.conf; ##添加这行内容default_type application/octet-stream;
[rootwww ~]#cd /apps/nginx/ ##切换到/apps/nginx/目录
[rootwww nginx]#mkdir conf.d ##创建conf.d文件
[rootwww nginx]#cd conf.d/ ##切换到conf.d目录里
[rootwww conf.d]#vim pc.conf ##编辑pc。conf文件
server{listen 80;server_name www.zhantai.com;root /data/nginx/pc/;[rootwww conf.d]#mkdir -pv /data/nginx/pc/ ##递归创建文件夹
mkdir: 已创建目录 /data
mkdir: 已创建目录 /data/nginx
mkdir: 已创建目录 /data/nginx/pc/[rootwww conf.d]#echo pc /data/nginx/pc/index.html
[rootwww conf.d]#tree /data/
/data/
└── nginx└── pc└── index.html2 directories, 1 file
[rootwww conf.d]#systemctl restart nginx ##重启服务2、测试
在另一台机子
[rootlocalhost ~]# vi /etc/hosts ##将前面一台机子的IP地址和域名需要写在hosts文件里面
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.130 www.zhantai.com
[rootlocalhost ~]# curl www.zhantai.com ##验证出现pc就OK了
pc3、在主配置文件添加
[rootwww conf.d]#vi pc.confserver{listen 80;server_name www.zhantai.com;root /data/nginx/pc/;keepalive_requests 3;keepalive_timeout 60 65;
}
[rootwww conf.d]#systemctl restart nginx ##重启服务
[rootwww conf.d]#nginx -s reload ##重新加载配置4、验证
在另一台机子验证
[rootlocalhost ~]# curl www.zhantai.com -I
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Tue, 21 Feb 2023 14:26:36 GMT
Content-Type: text/html
Content-Length: 3
Last-Modified: Tue, 21 Feb 2023 13:50:18 GMT
Connection: keep-alive
Keep-Alive: timeout65 ###超时时间
ETag: 63f4cc1a-3
Accept-Ranges: bytes二、Nginx第三方模块
1、开源的echo模块
现将代码包上传到/opt目录下解压
[rootwww opt]#cd /opt/
[rootwww opt]#unzip echo-nginx-module-master.zip #解压
[rootwww opt]#cd nginx-1.18.0/
[rootwww nginx-1.18.0]#systemctl stop nginx #编译前先停掉服务在编译./configure --prefix/apps/nginx --usernginx --groupnginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module/opt/echo-nginx-module-master #第三方模块make make install #重新编译之后就有第三方模块编译时就可以使用此代码包下载需要翻墙方便期间可以直接到网盘下载
链接https://pan.baidu.com/s/1ULiMf3Kc3MWjmrozZu6fQA
提取码q94p2、查看是否成功 3、加echo模块步骤
[rootwww nginx-1.18.0]#systemctl start nginx
[rootwww nginx-1.18.0]#cd /apps/nginx/conf.d/
[rootwww conf.d]#vim pc.conf #进入配置文件添加一下内容location /hello {default_type text/html;echo hello world ;}
}
[rootwww conf.d]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[rootwww conf.d]#nginx -s reload4、网页测试验证 三、搭建虚拟主机
1、编译安装好nginx后对主配置文件进行修改
[rootlocalhost]# cd /apps/nginx/conf/
[rootwww conf]#vim nginx.conf ##在http模块添加这个内容在这只能写http子模块要在主配置文件
include /apps/nginx/conf.d/*.conf;
[rootwww nginx]#mkdir conf.d2、创建文件
[rootwww nginx]#cd conf.d/
[rootwww conf.d]#vi pc.conf
server{listen 80;server_name www.pc.com;root /data/nginx/pc/;}
[rootwww conf.d]#vi m.conf
server{listen 80;server_name www.m.com;root /data/nginx/m/;}3、验证
在第二台机子
[rootlocalhost ~]# vi /etc/hosts
192.168.10.130 www.pc.com www.m.com
[rootlocalhost ~]# curl www.pc.com
pc
[rootlocalhost ~]# curl www.m.com
mobile
[rootlocalhost ~]#四、防盗链
1、先yum安装nginx两台机子都要装
[rootlocalhost ~]#yum install -y epel-release #yum安装nginx
[rootlocalhost ~]#yum info nginx
[rootlocalhost ~]#yum install -y nginx
[rootlocalhost ~]#systemctl restart nginx.service #重启服务
[rootlocalhost ~]#cd /usr/share/nginx/html/
[rootlocalhost html]#vim index.htmlhtml
body
h1ZZT is boy/h1
img srca.jpg/
/body
/html
[rootlocalhost html]#systemctl restart nginx.service 2、网页准备
Web源主机配置192.168.10.130www.zztshiboy.com
1.将a.jpg b.png文件传到/usr/local/nginx/html目录下注意编译安装html文件位置就在/usr/local/nginx/html要是yum安装的话就在/usr/share/nginx/html
[rootlocalhost ~]# cd /apps/nginx/html/
[rootlocalhost html]# ls
50x.html a.jpg index.html
[rootlocalhost html]#2.配置首页文件
vim index.html
htmlbody
h1ZZT is boy/h1
img srca.jpg//body
/html3.添加IP和域名的映射关系
[rootlocalhost html]#echo 192.168.10.130 www.zztshiboy.com /etc/hosts
[rootlocalhost html]#echo 192.168.10.132 www.zzthenshuai.com /etc/hosts 盗链网站主机配置192.168.10.132www.zzthenshuai.com
1.切换到站点目录
[rootbogon ~]# cd /usr/share/nginx/html/2.配置首页文件图片盗用Web源主机中的图片资源
vim index.htmlhtmlbodytitletest/titleimg srchttp://www.zztshiboy.com/a.jpg//body/html3.添加IP和域名的映射关系
[rootbogon html]# echo 192.168.10.130 www.zztshiboy.com /etc/hosts
[rootbogon html]# echo 192.168.10.132 www.zzthenshuai.com /etc/hosts4.在盗图网站主机上进行浏览器查看
http://www.zztshiboy.com #Web源主机
http://www.zzthenshuai.com #盗链网站3、Web源主机配置防盗链
当不是由web主机的域名请求图片资源时统一将图片重写到b.png。
[rootlocalhost ~]#vim /usr/local/nginx/conf/nginx.conflocation ~* \.(jpg|gif|swf)$ {root html;expires 1d;valid_referers none blocked *.zztshiboy.com zztshiboy.com;if ( $invalid_referer ) {rewrite ^/ http://www.zztshiboy.com/b.png;}}
[rootlocalhost ~]#systemctl restart nginx##################注释#########################~* .(jpg|gif|swf)$ 这段正则表达式表示匹配不区分大小写以.jpg 或.gif 或.swf 结尾的文件valid_referers 设置信任的网站可以正常使用图片none允许没有http_refer的请求访问资源根据Referer的定义它的作用是指示一个请求是从哪里链接过来的如果直接在浏览器的地址栏中输入一个资源的URL地址那么这种请求是不会包含 Referer 字段的如 http://www.zzthenshuai.com/a.jpg。我们使用 http://www.zzthenshuai.com 访问显示的图片可以理解成 http://www.zzthenshuai.com/a.jpg 这个请求是从 http://www.zzthenshuai.com 这个链接过来的。blocked允许不是http://开头的不带协议的请求访问资源 *.zzthenshuai.com只允许来自指定域名的请求访问资源如 http://www.zzthenshuai.comif语句如果链接的来源域名不在valid_referers所列出的列表中$invalid_referer为true则执行后面的操作即进行重写或返回 403 页面。五、配置网页压缩
Nginx的ngx_http_gzip_module压缩模块提供对文件内容压缩的功能。
允许Nginx服务器将输出内容在发送客户端之前进行压缩以节约网站带宽提升用户的访问体验默认已经安装。
可在配置文件中加入相应的压缩功能参数对压缩性能进行优化。 #1、修改配置文件vim /usr/local/nginx/conf/nginx.confhttp {...... gzip on; #取消注释开启gzip压缩功能gzip_min_length 1k; #最小压缩文件大小gzip_buffers 4 64k; #压缩缓冲区大小为4个64k缓冲区gzip_http_version 1.1; #压缩版本默认1.1前端如果是squid2.5请使用1.0gzip_comp_level 6; #压缩比率gzip_vary on; #支持前端缓存服务器存储压缩页面#压缩类型表示哪些网页文档启用压缩功能gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xmlrss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json;...... }#2、重启服务systemctl restart nginx#3、浏览器访问测试在Linux系统中打开火狐浏览器右击点查看元素选择 网络 --- 选择 HTML、WS、其他 访问 http://www.zztshiboy.com 双击响应消息查看响应头中包含 Content-Encoding: gzip六、日志分割
随着Nginx运行时间的增加产生的日志也会逐渐增加为了方便掌握Nginx的运行状态需要时刻关注Nginx日志文件。太大的日志文件对监控是一个大灾难不便于分析排查需要定期的进行日志文件的切割。
操作步骤
1、创建旧日志存放目录
2、通过mv命令将原有日志移动到日志目录中
3、kill -USR1 PID 重新生成日志文件
4、删除30天之前的日志文件 1. #写脚本[rootlocalhost ~]# vim /opt/log.sh#!/bin/bash# Filename: log.sh# nginx日志分割按时间分割#显示前一天的时间day$(date -d -1 day %Y%m%d)#旧日志文件目录logs_path/var/log/nginx#nginx进程的PIDpid_path/usr/local/nginx/logs/nginx.pid#如果旧日志目录不存在则创建日志文件目录[ -d $logs_path ] || mkdir -p $logs_path#将日志移动到旧日志目录并重命名日志文件mv /usr/local/nginx/logs/access.log ${logs_path}/zztshiboy.com-access.log-$day#重建新日志文件kill -USR1 $(cat $pid_path) #删除30天之前的日志文件find $logs_path -mtime 30 -exec rm -rf {} ; 2. #赋予执行权限执行脚本。查看日志文件目录。[rootlocalhost ~]# chmod x /usr/local/nginx/nginx_log.sh [rootlocalhost ~]# /opt/log.sh[rootlocalhost ~]# ls /var/log/nginx/ //旧日志文件已被移动到设置好的目录zztshiboy.com-access.log-202200608[rootlocalhost ~]# ls /usr/local/nginx/logs/ //已重建新日志文件access.log error.log nginx.pid3. #编写计划任务每天定点执行[rootlocalhost nginx]#crontab -e0 1 * * * /opt/fenge.sh七、修改Nginx运行的用户和组
方法一在编译安装时指定运行用户和组该方法仅在编译安装时使用 [rootlocalhost opt]# cd nginx-1.18.0/[rootlocalhost nginx-1.18.0]# ./configure \--prefix/usr/local/nginx \ #指定nginx的安装路径--usernginx \ #指定用户名运行用户--groupnginx \ #指定组名--with-http_stub_status_module #启用http_stub_status_module模块以支持状态统计方法二修改配置文件nginx.conf #修改配置文件文件vim /usr/local/nginx/conf/nginx.confuser nginx nginx; #取消注释修改用户为nginx组为nginx#重启服务systemctl restart nginx#查看是否修改成功。可以看到主进程由root创建子进程由nginx创建ps aux | grep nginx