Skip to content

Commit f614686

Browse files
authored
Merge pull request docker-library#411 from haozhou/master
enable mod_remoteip and add config for X-Forwarded-For IP address
2 parents a7a0a52 + a4eef48 commit f614686

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

apache-extras.template

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
RUN set -eux; \
2+
a2enmod rewrite expires; \
3+
\
4+
# https://httpd.apache.org/docs/2.4/mod/mod_remoteip.html
5+
a2enmod remoteip; \
6+
{ \
7+
echo 'RemoteIPHeader X-Forwarded-For'; \
8+
# these IP ranges are reserved for "private" use and should thus *usually* be safe inside Docker
9+
echo 'RemoteIPTrustedProxy 10.0.0.0/8'; \
10+
echo 'RemoteIPTrustedProxy 172.16.0.0/12'; \
11+
echo 'RemoteIPTrustedProxy 192.168.0.0/16'; \
12+
echo 'RemoteIPTrustedProxy 169.254.0.0/16'; \
13+
echo 'RemoteIPTrustedProxy 127.0.0.0/8'; \
14+
} > /etc/apache2/conf-available/remoteip.conf; \
15+
a2enconf remoteip; \
16+
# https://github.com/docker-library/wordpress/issues/383#issuecomment-507886512
17+
# (replace all instances of "%h" with "%a" in LogFormat)
18+
find /etc/apache2 -name '*.conf' -exec sed -ri 's/([[:space:]]*LogFormat[[:space:]]+"[^"]*)%h([^"]*")/\1%a\2/g' '{}' +

php7.1/apache/Dockerfile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,24 @@ RUN { \
6262
echo 'html_errors = Off'; \
6363
} > /usr/local/etc/php/conf.d/error-logging.ini
6464

65-
RUN a2enmod rewrite expires
65+
RUN set -eux; \
66+
a2enmod rewrite expires; \
67+
\
68+
# https://httpd.apache.org/docs/2.4/mod/mod_remoteip.html
69+
a2enmod remoteip; \
70+
{ \
71+
echo 'RemoteIPHeader X-Forwarded-For'; \
72+
# these IP ranges are reserved for "private" use and should thus *usually* be safe inside Docker
73+
echo 'RemoteIPTrustedProxy 10.0.0.0/8'; \
74+
echo 'RemoteIPTrustedProxy 172.16.0.0/12'; \
75+
echo 'RemoteIPTrustedProxy 192.168.0.0/16'; \
76+
echo 'RemoteIPTrustedProxy 169.254.0.0/16'; \
77+
echo 'RemoteIPTrustedProxy 127.0.0.0/8'; \
78+
} > /etc/apache2/conf-available/remoteip.conf; \
79+
a2enconf remoteip; \
80+
# https://github.com/docker-library/wordpress/issues/383#issuecomment-507886512
81+
# (replace all instances of "%h" with "%a" in LogFormat)
82+
find /etc/apache2 -name '*.conf' -exec sed -ri 's/([[:space:]]*LogFormat[[:space:]]+"[^"]*)%h([^"]*")/\1%a\2/g' '{}' +
6683

6784
VOLUME /var/www/html
6885

php7.2/apache/Dockerfile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,24 @@ RUN { \
6262
echo 'html_errors = Off'; \
6363
} > /usr/local/etc/php/conf.d/error-logging.ini
6464

65-
RUN a2enmod rewrite expires
65+
RUN set -eux; \
66+
a2enmod rewrite expires; \
67+
\
68+
# https://httpd.apache.org/docs/2.4/mod/mod_remoteip.html
69+
a2enmod remoteip; \
70+
{ \
71+
echo 'RemoteIPHeader X-Forwarded-For'; \
72+
# these IP ranges are reserved for "private" use and should thus *usually* be safe inside Docker
73+
echo 'RemoteIPTrustedProxy 10.0.0.0/8'; \
74+
echo 'RemoteIPTrustedProxy 172.16.0.0/12'; \
75+
echo 'RemoteIPTrustedProxy 192.168.0.0/16'; \
76+
echo 'RemoteIPTrustedProxy 169.254.0.0/16'; \
77+
echo 'RemoteIPTrustedProxy 127.0.0.0/8'; \
78+
} > /etc/apache2/conf-available/remoteip.conf; \
79+
a2enconf remoteip; \
80+
# https://github.com/docker-library/wordpress/issues/383#issuecomment-507886512
81+
# (replace all instances of "%h" with "%a" in LogFormat)
82+
find /etc/apache2 -name '*.conf' -exec sed -ri 's/([[:space:]]*LogFormat[[:space:]]+"[^"]*)%h([^"]*")/\1%a\2/g' '{}' +
6683

6784
VOLUME /var/www/html
6885

php7.3/apache/Dockerfile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,24 @@ RUN { \
6363
echo 'html_errors = Off'; \
6464
} > /usr/local/etc/php/conf.d/error-logging.ini
6565

66-
RUN a2enmod rewrite expires
66+
RUN set -eux; \
67+
a2enmod rewrite expires; \
68+
\
69+
# https://httpd.apache.org/docs/2.4/mod/mod_remoteip.html
70+
a2enmod remoteip; \
71+
{ \
72+
echo 'RemoteIPHeader X-Forwarded-For'; \
73+
# these IP ranges are reserved for "private" use and should thus *usually* be safe inside Docker
74+
echo 'RemoteIPTrustedProxy 10.0.0.0/8'; \
75+
echo 'RemoteIPTrustedProxy 172.16.0.0/12'; \
76+
echo 'RemoteIPTrustedProxy 192.168.0.0/16'; \
77+
echo 'RemoteIPTrustedProxy 169.254.0.0/16'; \
78+
echo 'RemoteIPTrustedProxy 127.0.0.0/8'; \
79+
} > /etc/apache2/conf-available/remoteip.conf; \
80+
a2enconf remoteip; \
81+
# https://github.com/docker-library/wordpress/issues/383#issuecomment-507886512
82+
# (replace all instances of "%h" with "%a" in LogFormat)
83+
find /etc/apache2 -name '*.conf' -exec sed -ri 's/([[:space:]]*LogFormat[[:space:]]+"[^"]*)%h([^"]*")/\1%a\2/g' '{}' +
6784

6885
VOLUME /var/www/html
6986

update.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ cliSha512="$(curl -fsSL "https://github.com/wp-cli/wp-cli/releases/download/v${c
2222
echo "$current (CLI $cliVersion)"
2323

2424
declare -A variantExtras=(
25-
[apache]='\nRUN a2enmod rewrite expires\n'
26-
[fpm]=''
27-
[fpm-alpine]=''
28-
[cli]='' # unused
25+
[apache]="$(< apache-extras.template)"
2926
)
3027
declare -A variantCmds=(
3128
[apache]='apache2-foreground'
@@ -40,6 +37,10 @@ declare -A variantBases=(
4037
[cli]='cli'
4138
)
4239

40+
sed_escape_rhs() {
41+
sed -e 's/[\/&]/\\&/g; $!a\'$'\n''\\n' <<<"$*" | tr -d '\n'
42+
}
43+
4344
travisEnv=
4445
for phpVersion in "${phpVersions[@]}"; do
4546
phpVersionDir="$phpVersion"
@@ -49,7 +50,10 @@ for phpVersion in "${phpVersions[@]}"; do
4950
dir="$phpVersionDir/$variant"
5051
mkdir -p "$dir"
5152

52-
extras="${variantExtras[$variant]}"
53+
extras="${variantExtras[$variant]:-}"
54+
if [ -n "$extras" ]; then
55+
extras=$'\n'"$extras"$'\n'
56+
fi
5357
cmd="${variantCmds[$variant]}"
5458
base="${variantBases[$variant]}"
5559

@@ -65,7 +69,7 @@ for phpVersion in "${phpVersions[@]}"; do
6569
-e 's!%%VARIANT%%!'"$variant"'!g' \
6670
-e 's!%%WORDPRESS_CLI_VERSION%%!'"$cliVersion"'!g' \
6771
-e 's!%%WORDPRESS_CLI_SHA512%%!'"$cliSha512"'!g' \
68-
-e 's!%%VARIANT_EXTRAS%%!'"$extras"'!g' \
72+
-e 's!%%VARIANT_EXTRAS%%!'"$(sed_escape_rhs "$extras")"'!g' \
6973
-e 's!%%CMD%%!'"$cmd"'!g' \
7074
"Dockerfile-${base}.template" > "$dir/Dockerfile"
7175

0 commit comments

Comments
 (0)