Skip to content

Commit 1fdcb22

Browse files
authored
Merge pull request #986 from linuxserver/swag_auto_proxy_preset
add support for new label swag_preset_conf
2 parents f15c45c + 70268ef commit 1fdcb22

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This mod gives SWAG the ability to auto-detect running containers via labels and
1717
- `swag_auth=authelia` - *optional* - enables auth methods (options are `authelia`, `authentik`, `ldap` and `http` for basic http auth)
1818
- `swag_auth_bypass=/api,/othersubfolder` - *optional* - bypasses auth for selected subfolders. Comma separated, no spaces.
1919
- `swag_server_custom_directive=custom_directive;` - *optional* - injects the label value as is into the server block of the generated conf. Must be a valid nginx directive, ending with a semi colon.
20+
- `swag_preset_conf=confname` - *optional* - allows defining a preset conf to use if the container name does not match one (if conf name is `radarr.subdomain.conf`, set this value to `radarr`). If container name matches an existing conf, this var will be ignored.
2021

2122

2223
In SWAG docker arguments, set an environment variable `DOCKER_MODS=linuxserver/mods:universal-docker|linuxserver/mods:swag-auto-proxy` and either add a volume mapping for `/var/run/docker.sock:/var/run/docker.sock:ro`, or set an environment var `DOCKER_HOST=remoteaddress`.

root/app/auto-proxy.sh

+19-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ else
1313
AUTO_GEN="${CONTAINER} ${AUTO_GEN}"
1414
else
1515
INSPECTION=$(docker inspect ${CONTAINER})
16-
for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass swag_server_custom_directive; do
16+
for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass swag_server_custom_directive swag_preset_conf; do
1717
VAR_VALUE=$(echo ${INSPECTION} | jq -r ".[0].Config.Labels[\"${VAR}\"]")
1818
if [ "${VAR_VALUE}" == "null" ]; then
1919
VAR_VALUE=""
@@ -40,23 +40,31 @@ fi
4040
for CONTAINER in ${AUTO_GEN}; do
4141
INSPECTION=$(docker inspect ${CONTAINER})
4242
rm -rf "/auto-proxy/${CONTAINER}.conf"
43-
for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass swag_server_custom_directive; do
43+
for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass swag_server_custom_directive swag_preset_conf; do
4444
VAR_VALUE=$(echo ${INSPECTION} | jq -r ".[0].Config.Labels[\"${VAR}\"]")
4545
if [ "${VAR_VALUE}" == "null" ]; then
4646
VAR_VALUE=""
4747
fi
4848
echo "${VAR}=\"${VAR_VALUE}\"" >> "/auto-proxy/${CONTAINER}.conf"
4949
done
5050
. /auto-proxy/${CONTAINER}.conf
51-
if [ -f "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" ]; then
52-
cp "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
53-
echo "**** Using preset proxy conf for ${CONTAINER} ****"
51+
if [ -f "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" ] || [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then
52+
if [ -f "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" ]; then
53+
echo "**** Container name matches a preset proxy conf, using ${CONTAINER}.subdomain.conf for container ${CONTAINER} ****"
54+
cp "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
55+
elif [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then
56+
echo "**** Label swag_preset_conf matches a preset proxy conf, using ${swag_preset_conf}.subdomain.conf for container ${CONTAINER} ****"
57+
cp "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
58+
fi
5459
if [ -n "${swag_auth_bypass}" ]; then
5560
echo "**** Swag auth bypass is auto managed via preset confs and cannot be overridden via env vars ****"
5661
fi
5762
if [ -n "${swag_address}" ]; then
5863
sed -i "s|set \$upstream_app .*|set \$upstream_app ${swag_address};|g" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
5964
echo "**** Overriding address as ${swag_address} for ${CONTAINER} ****"
65+
elif [ ! -f "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" ] && [ -n "${swag_preset_conf}" ] && [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then
66+
sed -i "s|set \$upstream_app .*|set \$upstream_app ${CONTAINER};|g" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
67+
echo "**** Overriding address as ${CONTAINER} for ${CONTAINER} ****"
6068
fi
6169
if [ -n "${swag_port}" ]; then
6270
sed -i "s|set \$upstream_port .*|set \$upstream_port ${swag_port};|g" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
@@ -70,6 +78,9 @@ for CONTAINER in ${AUTO_GEN}; do
7078
SED_swag_url=$(sed -e 's/[&\\|]/\\&/g; s|$|\\|; $s|\\$||' <<<"${swag_url}")
7179
sed -i "s|server_name .*|server_name ${SED_swag_url};|" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
7280
echo "**** Overriding url as ${swag_url} for ${CONTAINER} ****"
81+
elif [ ! -f "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" ] && [ -n "${swag_preset_conf}" ] && [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then
82+
sed -i "s|server_name .*|server_name ${CONTAINER}.*;|" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
83+
echo "**** Overriding url as ${CONTAINER}.* for ${CONTAINER} ****"
7384
fi
7485
if [ -n "${swag_server_custom_directive}" ]; then
7586
SED_swag_server_custom_directive=$(sed -e 's/[&\\|]/\\&/g; s|$|\\|; $s|\\$||' <<<"${swag_server_custom_directive}")
@@ -96,6 +107,9 @@ for CONTAINER in ${AUTO_GEN}; do
96107
echo "**** Enabling basic http auth for ${CONTAINER} ****"
97108
fi
98109
else
110+
if [ -n "${swag_preset_conf}" ] && [ ! "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then
111+
echo "**** Label swag_preset_conf is set, but no preset proxy conf found with the name ${swag_preset_conf}.subdomain.conf.sample ****"
112+
fi
99113
echo "**** No preset proxy conf found for ${CONTAINER}, generating from scratch ****"
100114
cp "/config/nginx/proxy-confs/_template.subdomain.conf.sample" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
101115
if [ -n "${swag_auth_bypass}" ]; then

0 commit comments

Comments
 (0)