Last active
January 9, 2023 11:02
-
-
Save reitzig/4e3f10a77d77e2474f20d426a00d2869 to your computer and use it in GitHub Desktop.
Minimal nginx Docker image with antora redirects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# snip | |
urls: | |
latest_version_segment_strategy: redirect:from | |
latest_version_segment: latest # your choice, doesn't impact the script below | |
redirect_facility: nginx | |
# snip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM nginxinc/nginx-unprivileged:alpine | |
COPY build/site /usr/share/nginx/html/ | |
# Inject the rewrite rules antora generates into the nginx config: | |
# 1. Add \ to the end of every line (to make the multi-line sed command in 3. work), and | |
# indent all lines (cosmetics). | |
# 2. Append another ("escaped") empty line (cosmetics). | |
# 3. Insert what we got before the final closing brace in the default config file. | |
# NB: Using % as separator for the replace command because rewrite.conf contains many / | |
# 4. Clean up | |
RUN sed -e 's/$/\\/' \ | |
-e 's/^/ /' \ | |
< /usr/share/nginx/html/.etc/nginx/rewrite.conf \ | |
> /tmp/rewrite.conf \ | |
&& echo -e "\\" >> /tmp/rewrite.conf \ | |
&& sed -i -e "s%^}%\n$(cat /tmp/rewrite.conf)}\n%" \ | |
/etc/nginx/conf.d/default.conf \ | |
&& rm -rf /tmp/rewrite.conf |
Route
, in my case), further steps are needed (cf. antora/antora#1035). In some cases, it may be enough to extend the above script; add -r
and
-e 's% (/[^ ]*)( redirect)?;% \$scheme://\$http_host\1\2;%'
to the sed
command in step 1.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When shipping an antora-generated page with nginx as container image, you may not want to mess with the default nginx configuration, and yet import the redirect rules antora may generate. As they say:
Unfortunately, the official nginx image does not, at the time of this writing, include facilities to include conf files inside the (default)
server
block.Thus, hackery.
Note that I leave
/usr/share/nginx/html/.etc
in the final image; you may want to add removing that folder to the cleanup step.