Nginx 优化之压缩
配置 nginx
- [[email protected] ~]# yum install pcre-devel zlib-devel gcc gcc-c++ -y ## 安装环境包
- [[email protected] ~]# useradd -M -s /sbin/nologin nginx ## 创建程序性用户
- [[email protected] ~]# mkdir /chen ## 创建挂载点
- [[email protected] ~]# mount.cifs //192.168.100.23/LNMP /chen ## 挂载
- Password for [email protected]//192.168.100.23/LNMP:
- [[email protected] chen]# tar zxvf nginx-1.12.2.tar.gz -C /opt/ ## 解压
- [[email protected] chen]# cd /opt/
- [[email protected] opt]# ls
- nginx-1.12.2 rh
- [[email protected] opt]# cd nginx-1.12.2/
- [[email protected] nginx-1.12.2]# ls
- auto CHANGES.ru configure html man src
- CHANGES conf contrib LICENSE README
- ./configure \ ## 安装 nginx 组件
- --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
- [[email protected] nginx-1.12.2]# make && make install ## 编译
- [[email protected] nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ## 做软链接让系统能识别 nginx 的所有人命令
- [[email protected] nginx-1.12.2]# nginx -t ## 检查语法错误
- nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
写 nginx 脚本放在系统启动脚本中方便 service 管理器管理
- [[email protected] nginx-1.12.2]# cd /etc/init.d/ ## 到系统启动脚本
- [[email protected] init.d]# VIM nginx ## 写一个 nginx 脚本
- #!/bin/bash
- #chkconfig: - 99 20 #注释信息
- #description: Nginx Service Control Script
- PROG="/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 1
- esac
- exit 0
- [[email protected] init.d]# chmod +x nginx ## 给 Nginx 提升权限
- [[email protected] init.d]# chkconfig --add nginx ## 添加 nginx
- [[email protected] init.d]# service nginx start
- [[email protected] init.d]# netstat -ntap | grep nginx
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17544/nginx: master
- [[email protected] init.d]# systemctl stop firewalld.service
- [[email protected] init.d]# setenforce 0
nginx 优化之压缩配置
- [[email protected] ~]# VIM /usr/local/nginx/conf/nginx.conf
- gzip on; ## 开启压缩功能
- gzip_min_length 1k; ## 超过 1kb 就会压缩
- gzip_buffers 4 16k; ## 缓存空间, 大小为 4 个 16k
- gzip_http_version 1.1; ## 压缩版本
- gzip_comp_level 6; ## 压缩比率, 最小为 1, 处理速度快, 传输慢, 9 最大压缩比, 处理速度慢, 传输快, 适中我们选 5 或 6
- gzip_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 浏览器以下不启用 gzip
- gzip_vary on; ## 对压缩的页面可以缓存到前端的服务器当中
我们找一张图片来测试压缩功能
- [[email protected] ~]# cd /usr/local/nginx/HTML/ ## 到 nginx 站点中
- [[email protected] HTML]# ls
50x.HTML index.HTML
- [[email protected] HTML]# mount.cifs //192.168.100.23/LNMP /mnt ## 挂载
- Password for [email protected]//192.168.100.23/LNMP:
- [[email protected] HTML]# cd /mnt/
- [[email protected] mnt]# ls
- Discuz_X3.4_SC_UTF8.zip nginx-1.12.0.tar.gz PHP-7.1.20.tar.gz
- fang.PNG nginx-1.12.2.tar.gz shu.jpg
- MySQL-boost-5.7.20.tar.gz PHP-7.1.10.tar.bz2
- [[email protected] HTML]# cp /mnt/shu.jpg ./ ## 到挂载目录
- [[email protected] HTML]# ls
- 50x.HTML index.HTML shu.jpg
- [[email protected] HTML]# VIM index.HTML ## 到站点网页, 写入图片路径
- 15 <img src="shu.jpg"/>
- [[email protected] HTML]# service nginx stop ## 关闭 nginx
- [[email protected] HTML]# service nginx start ## 开启 nginx
- [[email protected] HTML]# systemctl stop firewalld.service ## 关闭防火墙
- [[email protected] HTML]# setenforce 0 ## 关闭增强功能
Nginx 优化之防盗链
实验环境, Nginx 服务器, win10-1 作为测试访问, win10-2 作为盗链网址
先将 win10-2 做一个网站, 写一个默认网页, 其中图片的路径是盗链 Nginx 的图片
文件扩展名改成 index.HTML
win10-2 安装网站程序
打开网站程序, 默认是开启的
把你刚才写的页面放到站点中
回到 Nginx 服务器安装 DNS 服务并配置
- [[email protected] HTML]yum install bind -y ## 安装 DNS 软件包
- [[email protected] 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; }; ## 允许所有人可以访问
- [[email protected] HTML]# VIM /etc/named.rfc1912.zones ## 配置区域配置文件
- zone "kgc.com" IN { type master; ## 定义 kgc.com 域名
- file "kgc.com.zone"; ## 定义区域数据配置文件
- allow-update { none; };
- };
- [[email protected] HTML]# cd /var/named/
- [[email protected] named]# cp -p named.localhost kgc.com.zone ## 复制模板到创建的新区域数据配置文件中
- [[email protected] 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.1
- www IN A 192.168.136.163 ## 主机名, 地址
- [[email protected] named]# systemctl start named ## 开启 dns 服务
win10-1 和 win10-2 都要选择 DNS 去解析
win10-2 测试解析
win10-1 去访问 win10-2 的网址
访问域名还是这个图片
回到 Nginx 做防盗链配置
- [[email protected] 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 }
- [[email protected] HTML]# service nginx stop
- [[email protected] HTML]# service nginx start
再去 win10-1 测试访问一下
以上就是我们全部的内容了, 谢谢收看
来源: http://www.bubuko.com/infodetail-3289328.html