From 26626a2b5de2625743b5319bea2647f528827b7e Mon Sep 17 00:00:00 2001 From: Joe Orton Date: Mon, 16 Dec 2024 13:48:12 +0000 Subject: [PATCH] Add systemd instantiated service nginx@.service, allowing e.g. "systemctl start nginx@foobar.service" to start an instance of nginx using /etc/nginx/foobar.conf as the configuration. --- instance.conf | 20 ++++++++++++++++++++ nginx.spec | 18 +++++++++++++++--- nginx@.service | 23 +++++++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 instance.conf create mode 100644 nginx@.service diff --git a/instance.conf b/instance.conf new file mode 100644 index 0000000..e01ed10 --- /dev/null +++ b/instance.conf @@ -0,0 +1,20 @@ +# +# This is an example instance-specific configuration file. +# +# To use this example, copy instance.conf to /etc/nginx/foobar.conf +# and replace all the references to @INSTANCE@, for example using: +# +# # sed s/@INSTANCE@/foobar/g < instance.conf > /etc/nginx/foobar.conf +# +# This new configuration file be used instead of /etc/nginx/nginx.conf +# when running: +# +# # systemctl start nginx@foobar.service +# +# This example configuration adjust the location of the PID file, +# access and error logs to be instance-specific. Further +# customisations will be required for an instance to run +# simultaneously to nginx.service under the default configuration, +# e.g. changing the port and root directory as appropriate. +# + diff --git a/nginx.spec b/nginx.spec index 34ec48e..9774725 100644 --- a/nginx.spec +++ b/nginx.spec @@ -84,9 +84,11 @@ Source14: nginx-upgrade.8 Source15: macros.nginxmods.in Source16: nginxmods.attr Source17: nginx-ssl-pass-dialog +Source18: nginx@.service Source102: nginx-logo.png Source200: README.dynamic Source210: UPGRADE-NOTES-1.6-to-1.10 +Source220: instance.conf # removes -Werror in upstream build scripts. -Werror conflicts with # -D_FORTIFY_SOURCE=2 causing warnings to turn into errors. @@ -267,10 +269,10 @@ Requires: zlib-devel cat %{S:2} %{S:3} %{S:4} %{S:5} %{S:6} > %{_builddir}/%{name}.gpg %{gpgverify} --keyring='%{_builddir}/%{name}.gpg' --signature='%{SOURCE1}' --data='%{SOURCE0}' %autosetup -p1 -cp %{SOURCE200} %{SOURCE210} %{SOURCE10} %{SOURCE12} . +cp %{SOURCE200} %{SOURCE210} %{SOURCE10} %{SOURCE12} %{SOURCE18} %{SOURCE220} . %if 0%{?rhel} > 0 && 0%{?rhel} < 8 -sed -i -e 's#KillMode=.*#KillMode=process#g' nginx.service +sed -i -e 's#KillMode=.*#KillMode=process#g' nginx.service nginx@.service sed -i -e 's#PROFILE=SYSTEM#HIGH:!aNULL:!MD5#' nginx.conf %endif @@ -281,6 +283,13 @@ sed \ -i auto/lib/openssl/conf %endif +# Prepare template config for instances +sed -e '/^error_log /s|error\.log|@INSTANCE@_error.log|' \ + -e '/^pid /s|nginx\.pid|nginx-@INSTANCE@.pid|' \ + -e '/^ *access_log/s|access\.log|@INSTANCE@_access.log|' \ + nginx.conf >> instance.conf +touch -r %{SOURCE12} instance.conf + # Prepare sources for installation cp -a ../%{name}-%{version} ../%{name}-%{version}-%{release}-src mv ../%{name}-%{version}-%{release}-src . @@ -377,6 +386,8 @@ find %{buildroot} -type f -iname '*.so' -exec chmod 0755 '{}' \; install -p -D -m 0644 ./nginx.service \ %{buildroot}%{_unitdir}/nginx.service +install -p -D -m 0644 ./nginx@.service \ + %{buildroot}%{_unitdir}/nginx@.service install -p -D -m 0644 %{SOURCE11} \ %{buildroot}%{_sysconfdir}/logrotate.d/nginx @@ -538,11 +549,12 @@ fi %{_mandir}/man8/nginx.8* %{_mandir}/man8/nginx-upgrade.8* %{_unitdir}/nginx.service +%{_unitdir}/nginx@.service %{_libexecdir}/nginx-ssl-pass-dialog %files core %license LICENSE -%doc CHANGES README README.dynamic +%doc CHANGES README README.dynamic instance.conf %{_sbindir}/nginx %config(noreplace) %{_sysconfdir}/nginx/fastcgi.conf %config(noreplace) %{_sysconfdir}/nginx/fastcgi.conf.default diff --git a/nginx@.service b/nginx@.service new file mode 100644 index 0000000..0fdd915 --- /dev/null +++ b/nginx@.service @@ -0,0 +1,23 @@ +[Unit] +Description=The nginx HTTP and reverse proxy server +After=network-online.target remote-fs.target nss-lookup.target +Wants=network-online.target + +[Service] +Type=forking +Environment=NGINX_INSTANCE=%i +PIDFile=/run/nginx-%i.pid +# Nginx will fail to start if /run/nginx.pid already exists but has the wrong +# SELinux context. This might happen when running `nginx -t` from the cmdline. +# https://bugzilla.redhat.com/show_bug.cgi?id=1268621 +ExecStartPre=/usr/bin/rm -f /run/nginx-%i.pid +ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/%i.conf +ExecStart=/usr/sbin/nginx -c /etc/nginx/%i.conf +ExecReload=/usr/sbin/nginx -s reload -c /etc/nginx/%i.conf +KillSignal=SIGQUIT +TimeoutStopSec=5 +KillMode=mixed +PrivateTmp=true + +[Install] +WantedBy=multi-user.target