ubuntu下安裝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;
#(默認)只允許同類型文件合併(比如, css和js不能合併). 關閉請設置爲off. 建議使用默認值.
#concat_unique on;
#最大合併文件數量, 默認10
#concat_max_files 20;
# 是否忽略文件不存在/無法訪問時的錯誤. 默認是不忽略(off),只要有一個文件不存在就拋出404狀態
#concat_ignore_file_error on;
2. 修改mime.types
:
訪問js文件時報錯"400 Bad Request"的修正. 組件默認支持的類型(參數
concat_types
)是text/css application/x-javascript
, 而合併後的js文件的content-type
是application/x-javascript
把
application/javascript js;
修改爲
application/x-javascript js;
注意
1. 文件連接符號concat_delimiter
默認是英文逗號, 不可以使用其他符號, 否則會出現404
比如:
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
是錯誤的.