Nginx优化之压缩和防盗链
发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,Nginx优化之压缩配置nginx[root@localhost ~]# yum install pcre-devel zlib-devel gcc gcc-c++ -y ##安装环境包[root@l
千家信息网最后更新 2025年02月04日Nginx优化之压缩和防盗链
Nginx优化之压缩
配置nginx
[root@localhost ~]# yum install pcre-devel zlib-devel gcc gcc-c++ -y ##安装环境包[root@localhost ~]# useradd -M -s /sbin/nologin nginx ##创建程序性用户[root@localhost ~]# mkdir /chen ##创建挂载点[root@localhost ~]# mount.cifs //192.168.100.23/LNMP /chen ##挂载Password for root@//192.168.100.23/LNMP: [root@localhost chen]# tar zxvf nginx-1.12.2.tar.gz -C /opt/ ##解压[root@localhost chen]# cd /opt/[root@localhost opt]# lsnginx-1.12.2 rh[root@localhost opt]# cd nginx-1.12.2/[root@localhost nginx-1.12.2]# lsauto CHANGES.ru configure html man srcCHANGES conf contrib LICENSE README./configure \ ##安装nginx组件--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]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##做软链接让系统能识别nginx的所有人命令[root@localhost nginx-1.12.2]# nginx -t ##检查语法错误nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
写nginx脚本放在系统启动脚本中方便service管理器管理
[root@localhost nginx-1.12.2]# cd /etc/init.d/ ##到系统启动脚本[root@localhost init.d]# vim nginx ##写一个nginx脚本#!/bin/bash#chkconfig: - 99 20 #注释信息#description: Nginx Service Control ScriptPROG="/usr/local/nginx/sbin/nginx" #这个变量,指向我的命令文件PIDF="/usr/local/nginx/logs/nginx.pid" #这个变量,指向nginx的进程号case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1esacexit 0[root@localhost init.d]# chmod +x nginx ##给Nginx提升权限[root@localhost init.d]# chkconfig --add nginx ##添加nginx[root@localhost init.d]# service nginx start [root@localhost init.d]# netstat -ntap | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17544/nginx: master [root@localhost init.d]# systemctl stop firewalld.service[root@localhost init.d]# setenforce 0
nginx优化之压缩配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.confgzip on; ##开启压缩功能gzip_min_length 1k; ##超过1kb就会压缩gzip_buffers 4 16k; ##缓存空间,大小为4个16kgzip_http_version 1.1; ##压缩版本gzip_comp_level 6; ##压缩比率,最小为1,处理速度快,传输慢,9最大压缩比,处理速度慢,传输快,适中我们选5或6gzip_types test/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml test/javascript application/x-httpd-php application/javascript application/json;##支持这些格式压缩gzip_disable "MSIE [1-6]\."; ##配置禁用gzip条件,支持正则,ie6浏览器以下不启用gzipgzip_vary on; ##对压缩的页面可以缓存到前端的服务器当中
我们找一张图片来测试压缩功能
[root@localhost ~]# cd /usr/local/nginx/html/ ##到nginx站点中[root@localhost html]# ls50x.html index.html[root@localhost html]# mount.cifs //192.168.100.23/LNMP /mnt ##挂载Password for root@//192.168.100.23/LNMP: [root@localhost html]# cd /mnt/[root@localhost mnt]# lsDiscuz_X3.4_SC_UTF8.zip nginx-1.12.0.tar.gz php-7.1.20.tar.gzfang.png nginx-1.12.2.tar.gz shu.jpgmysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2[root@localhost html]# cp /mnt/shu.jpg ./ ##到挂载目录[root@localhost html]# ls50x.html index.html shu.jpg[root@localhost html]# vim index.html ##到站点网页,写入图片路径15 [root@localhost html]# service nginx stop ##关闭nginx[root@localhost html]# service nginx start ##开启nginx[root@localhost html]# systemctl stop firewalld.service ##关闭防火墙[root@localhost html]# setenforce 0 ##关闭增强功能
Nginx优化之防盗链
实验环境,Nginx服务器,win10-1作为测试访问,win10-2作为盗链网址
先将win10-2做一个网站,写一个默认网页,其中图片的路径是盗链Nginx的图片
文件扩展名改成index.html
win10-2安装网站程序
打开网站程序,默认是开启的
把你刚才写的页面放到站点中
回到Nginx服务器安装DNS服务并配置
[root@localhost html]yum install bind -y ##安装DNS软件包[root@localhost html]# vim /etc/named.conf ##配置主配置文件options { listen-on port 53 { any; }; ##监听所有地址 listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; recursing-file "/var/named/data/named.recursing"; secroots-file "/var/named/data/named.secroots"; allow-query { any; }; ##允许所有人可以访问[root@localhost html]# vim /etc/named.rfc1912.zones ##配置区域配置文件zone "kgc.com" IN { type master; ##定义kgc.com域名 file "kgc.com.zone"; ##定义区域数据配置文件 allow-update { none; };};[root@localhost html]# cd /var/named/[root@localhost named]# cp -p named.localhost kgc.com.zone ##复制模板到创建的新区域数据配置文件中[root@localhost named]#vim kgc.com.zone ##编辑数据区域数据配置文件$TTL 1D@ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1www IN A 192.168.136.163 ##主机名,地址[root@localhost named]# systemctl start named ##开启dns服务
win10-1和win10-2都要选择DNS去解析
win10-2测试解析
win10-1去访问win10-2的网址
访问域名还是这个图片
回到Nginx做防盗链配置
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf 66 location ~*\.(jpg|gif|swf)$ { ##带有这些格式为结尾的图片信息 67 valid_referers none blocked *.kgc.com kgc.com; ##本地解析的就会去跳转相应的图片 68 if ( $invalid_referer ) { ##如果你不是本地解析的,以盗链访问 69 rewrite ^/ http://www.kgc.com/fang.png; ##就会跳转防盗链的图片给你 70 } 71 }[root@localhost html]# service nginx stop[root@localhost html]# service nginx start
再去win10-1测试访问一下
以上就是我们全部的内容了,谢谢收看
配置
图片
文件
服务
区域
数据
脚本
测试
防盗
功能
服务器
程序
系统
网站
信息
变量
命令
地址
域名
所有人
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全交互稿视觉稿
网络安全服务24小时
如何将数据写入数据库
天涯服务器在哪里
软件开发总结经验
网络安全在线访谈
高职学软件开发有前途吗
树立网络安全意识绿色文明上网
c实现ftp服务器
校园网络安全防护设计
网络安全和信息科技
网络安全是国家安全的一部分
网络安全宣传周自查报告
软件开发如何写好文档
网络安全类投资
厦门 互联网与生活科技
互联网行业网络安全审查
数据库上云怎么选规格
软件开发工程师3一4k培训
健康码服务器是阿里云吗
新罗区其群静网络技术工作室
上海富友金融网络技术6
巅峰坦克获取服务器失败
服务器坏了怎么判断
中兴校招软件开发笔试题
软件开发公司避坑
oracle数据库的命令
河北手机软件开发价钱
收款码服务器连接失败怎么办
租服务器价钱大概