ubuntu20下安装nginx请求合并插件: concat

原创
2021/12/27 15:42
阅读数 195

ubuntu20下安装nginx请求合并插件: concat

该插件用于合并多个静态文件的请求为一个, 使用两个问号("??")来识别.

插件地址: https://github.com/alibaba/nginx-http-concat

apt安装nginx并获取原安装参数

安装:

sudo apt update
sudo apt install nginx

执行nginx -V:

nginx version: nginx/1.18.0
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-KTLRnK/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module

卸载:

sudo apt autoremove --purge nginx

或者:

# 移除nginx, 但保留配置文件
apt remove nginx nginx-common

# 或者, 移除nginx及配置
apt purge nginx nginx-common
apt remove nginx-full nginx-common

下载及安装

# 安装依赖
sudo apt update
sudo apt install libtool libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev


# ubuntu下使用账号 `www-data`


cd /home
# 下载nginx-concat插件
wget -O nginx-http-concat-master.zip https://github.com/alibaba/nginx-http-concat/archive/refs/heads/master.zip

unzip nginx-http-concat-master.zip


# 下载对应版本的nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz    

tar zxvf nginx-1.18.0.tar.gz


cd nginx-1.18.0

# 编译nginx, 添加的插件concat在/home/nginx-http-concat-master目录
./configure --group=www-data --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-KTLRnK/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-module=/home/nginx-http-concat-master

make && make install

find / -name nginx -type f搜索bin的目录, 结果是: /usr/share/nginx/sbin/nginx

创建启动软链接

ln -s /usr/share/nginx/sbin/nginx /bin/nginx

添加到系统服务: 执行vim /lib/systemd/system/nginx.service, 写入:

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/share/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/share/nginx/sbin/nginx -s reload
ExecStop=/usr/share/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target

重新加载配置文件:

systemctl daemon-reload

启动nginx服务:

systemctl restart nginx

启动失败! 报错: nginx: [emerg] mkdir() "/var/lib/nginx/body" failed (2: No such file or directory)

nginx -t提示:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/lib/nginx/body" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

创建文件夹即可:

mkdir -p /var/lib/nginx/body

启用concat插件:

修改nginx.conf配置的http节点:

可配置段: http, server, location

添加以下设置:

#开启concat. 默认是off状态
concat on;

# 修正MIME
concat_types text/css application/javascript;

#给文件之间加空行
concat_delimiter "\n";

#(默认)只允许同类型文件合并(比如, css和js不能合并). 关闭请设置为off. 建议使用默认值.
#concat_unique on;

#最大合并文件数量, 默认10
#concat_max_files 20;

# 是否忽略文件不存在/无法访问时的错误. 默认是不忽略(off),只要有一个文件不存在就抛出404状态
#concat_ignore_file_error on;

访问js文件时报错"400 Bad Request"的修正. 组件默认支持的类型(参数concat_types)是text/css application/x-javascript, 而合并后的js文件的content-typeapplication/x-javascript, 所以要配置concat_types

注意

1. 文件拼接符号使用英文逗号, 且不能有多余的字符(包括空格)

比如:

http://xxx/path/??1.js,2.js是正确的

http://xxx/path/??1.js,2.js?v=20211227是正确的

但是http://xxx/path/??1.js, 2.js是错误的.

2. 文件不存在/无法访问时, 默认直接抛出404/403错误.

3. 默认css和js不能合并

比如: http://xxx/path/??1.css,2.js是错误的.

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部