Nginx优化之压缩和防盗链
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,Nginx优化之压缩配置nginx[root@localhost ~]# yum install pcre-devel zlib-devel gcc gcc-c++ -y ##安装环境包[root@l
千家信息网最后更新 2026年11月03日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 0nginx优化之压缩配置
[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安全错误
数据库的锁怎样保障安全
保险科技 互联网保险
互联网是第几次科技革命的成果
湖南永感互联网科技有限公司
高科技互联网
新华互联网科技学校
国智互联网科技
南京欣网互联网科技
致聘互联网科技
互联网科技股有哪些
青岛中康互联网医疗科技有限公司
团飞科技与伯堂互联网服务平台
互联网金融中的现代科技
互联网5g黑科技有哪些
山东有哪些互联网科技公司
互联网科技资金流入
宇信科技在杭州互联网排第几
郑州欣桑互联网科技有限公司
玉溪新华互联网科技怎么选
互联网科技大佬云集
科技发展第三阶段互联网时代
中资互联网科技
互联网 科技创新
互联网 科技创业
丰台科技园 互联网
多迪互联网科技有限公司
互联网时代科技改变生活
荆州市昕露互联网络科技有限公司
湖南一路畅行互联网科技
广东工业互联网大赛深圳学泰科技
四川互联网展示科技馆设计