Skip to content

Commit

Permalink
try installing brotli module and nginx from ppa
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodlz committed Feb 9, 2024
1 parent 3d31ba3 commit 7913d51
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
18 changes: 4 additions & 14 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,10 @@ jobs:
# if nginx is already installed, remove it
sudo apt remove -y nginx nginx-common nginx-core
# install nginx from source so we can add the brotli module
git clone --recursive https://github.com/google/ngx_brotli.git
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar zxf nginx-1.24.0.tar.gz
cd ngx_brotli/deps/brotli
mkdir out && cd out
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_INSTALL_PREFIX=./installed ..
cmake --build . --config Release --target brotlienc
cd ../../../..
export CURRENT_DIR=$(pwd)
cd nginx-1.24.0
./configure --sbin-path=/usr/bin/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-stream --with-mail=dynamic --with-http_realip_module --with-compat --add-module=${CURRENT_DIR}/ngx_brotli
sudo make && sudo make install
sudo nginx
sudo add-apt-repository ppa:ondrej/nginx-mainline -y
sudo apt update -y
sudo apt install -y nginx libnginx-mod-brotli
pip install --upgrade pip
pip install wheel
Expand Down
6 changes: 3 additions & 3 deletions services/nginx/nginx.conf.template
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
error_log log/error.log error;
pid run/nginx.pid;

{% if fill_config_feature.nginx_brotli.dynamic and fill_config_feature.nginx_brotli.modules_dir %}
load_module {{ fill_config_feature.nginx_brotli.modules_dir }}/ngx_http_brotli_filter_module.so; # for compressing responses on-the-fly
load_module {{ fill_config_feature.nginx_brotli.modules_dir }}/ngx_http_brotli_static_module.so; # for serving pre-compressed files
{% if fill_config_feature.nginx_brotli.dynamic and fill_config_feature.nginx_brotli.modules_path %}
load_module {{ fill_config_feature.nginx_brotli.modules_path }}/ngx_http_brotli_filter_module.so; # for compressing responses on-the-fly
load_module {{ fill_config_feature.nginx_brotli.modules_path }}/ngx_http_brotli_static_module.so; # for serving pre-compressed files
{% endif %}


Expand Down
69 changes: 40 additions & 29 deletions tools/fill_conf_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,51 +46,62 @@ def nginx_brotli_installed():
True if the nginx brotli module is installed, False otherwise
dynamic : bool
True if the module is dynamically loaded, False otherwise
modules_dir : str
modules_path : str
The directory where the nginx modules are located if dynamic loading is used
"""

installed = False
dynamic = False
dir = None
modules_path = None

try:
output = subprocess.check_output(
["nginx", "-V"], stderr=subprocess.STDOUT
).decode("utf-8")
# Option 1: installed at compilation: always loaded
if (
"--add-module" in output
and "brotli" in output.split("--add-module")[1].strip()
):
installed = True
elif (
"--add-dynamic-module" in output
and "brotli" in output.split("--add-dynamic-module")[1].strip()
):
installed = True
dynamic = True
# we try to figure out where the modules are, there are 2 possibilities
# 1. the --modules-path directive is used
# 2. the default directory is used, which is where the configuration file is located so we look for it
if "--modules-path" in output:
dir = output.split("--modules-path=")[1].split(" ")[0]
elif "--conf-path" in output:
dir = os.path.dirname(
output.split("--conf-path=")[1].split(" ")[0]
).replace("nginx.conf", "modules")
if dir is not None and not os.path.isdir(dir):
dir = None
if dir is None:
print(
"Brotli is installed dynamically, but couldn't find the nginx modules directory. Skipping."
# Option 2: installed dynamically at compilation or later: has to be loaded
else:
# a. find the modules path
config_path = (
output.split("--conf-path=")[1].split(" ")[0]
if "--conf-path" in output
else None
)
modules_path = (
output.split("--modules-path=")[1].split(" ")[0]
if "--modules-path" in output
else None
)
if not modules_path and config_path:
modules_path = os.path.dirname(config_path).replace(
"nginx.conf", "modules"
)
installed = False
dynamic = False
else:
dir = dir.rstrip("/")
if not modules_path or not os.path.isdir(modules_path):
modules_path = None

# b. check if there is a brotli module in the modules path
if modules_path:
if all(
os.path.isfile(os.path.join(modules_path, f))
for f in [
"ngx_http_brotli_filter_module.so",
"ngx_http_brotli_static_module.so",
]
):
installed = True
dynamic = True
else:
installed = False
dynamic = False
modules_path = None
except subprocess.CalledProcessError:
pass
return installed, dynamic, dir
return installed, dynamic, modules_path


custom_filters = {"md5sum": md5sum, "version": version, "hash": hash_filter}
Expand All @@ -99,12 +110,12 @@ def nginx_brotli_installed():
def fill_config_file_values(template_paths):
log("Compiling configuration templates")
env, cfg = load_env()
installed, dynamic, modules_dir = nginx_brotli_installed()
installed, dynamic, modules_path = nginx_brotli_installed()
cfg["fill_config_feature"] = {
"nginx_brotli": {
"installed": installed,
"dynamic": dynamic,
"modules_dir": modules_dir,
"modules_path": modules_path,
}
}

Expand Down

0 comments on commit 7913d51

Please sign in to comment.