Nginx安装及开启gzip压缩

安装

1、sudo dnf -y install nginx

配置自启动

2、sudo systemctl enable nginx

3、sudo systemctl start nginx

开启防火墙

4、sudo firewall-cmd –permanent –zone=public –add-service=http

5、sudo firewall-cmd –permanent –zone=public –add-service=https

6、sudo firewall-cmd –reload

开启gzip压缩

在nginx.conf中添加以下内容即可,nginx自带该压缩

#开启gzip压缩
gzip on;
#http的协议版本
gzip_http_version 1.0;
#IE版本1-6不支持gzip压缩,关闭
gzip_disable ‘MSIE[1-6].’;
#需要压缩的文件格式 text/html默认会压缩,不用添加
gzip_types text/css text/javascript application/javascript image/jpeg image/png image/gif;
#设置压缩缓冲区大小,此处设置为4个8K内存作为压缩结果流缓存
gzip_buffers 4 8k;
#压缩文件最小大小
gzip_min_length 1k;
#压缩级别1-9
gzip_comp_level 9;
#给响应头加个vary,告知客户端能否缓存
gzip_vary on;
#反向代理时使用
gzip_proxied off;

以下为安装brotli压缩,未成功

1、安装依赖

> yum groupinstall 'Development Tools'

2、安装libbrotli

cd /usr/local/src/
git clone https://github.com/bagder/libbrotli
cd libbrotli
./autogen.sh
./configure
make && make install

3、安装ngx_brotli

> cd /usr/local/src/
> git clone https://github.com/google/ngx_brotli
> cd ngx_brotli && git submodule update --init

4、下载Nginx

> cd /usr/local/src
> wget http://nginx.org/download/nginx-1.10.3.tar.gz
> tar -xvzf nginx-1.10.3.tar.gz

5、编译安装

> cd /usr/local/src/nginx-1.10.3
> ./configure --add-module=/usr/local/src/ngx_brotli

> make

# 如果是首次安装Nginx,执行make install;如果是升级,执行make upgrade
> make install

6、加入系统变量并配置自启动

> ln -s /usr/local/nginx/sbin/nginx /usr/sbin
> systemctl enable nginx
> systemctl start nginx

6、查看是否安装正常

> nginx -V
> nginx -t

7、配置brotli 在nginx.conf文件的http模块下新增以下内容

#Brotli Compression
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

8、检查是否生效 打开网页,用chrome开发者工具调试,在Network一栏会发现有content-encoding:br,同时网络耗时也会明显减少。

Leave a Reply

Your email address will not be published. Required fields are marked *

lWoHvYe 无悔,专一