Skip to content

Commit 89c178f

Browse files
committed
add webp auto serving
1 parent 4b083e1 commit 89c178f

29 files changed

+484
-28
lines changed

8.1-nginx-prod/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FROM wordpress:cli-php8.1 as wordpress-cli
2-
FROM wordpress:php8.1-fpm-alpine as wordpress
1+
FROM wordpress:cli-php8.1 AS wordpress-cli
2+
FROM wordpress:php8.1-fpm-alpine AS wordpress
33
FROM kooldev/php:8.1-nginx-prod
44

55
ENV NGINX_ROOT=/app
@@ -8,6 +8,7 @@ COPY --from=wordpress-cli /usr/local/bin/wp /usr/local/bin/wp
88
COPY --from=wordpress --chown=kool:kool /usr/src/wordpress /kool/wordpress
99
COPY --from=wordpress --chown=kool:kool /var/www/html/wp-content /app/wp-content
1010
COPY entrypoint /kool/wordpress-entrypoint
11+
COPY wordpress-webp.conf /etc/nginx/wordpress-webp.conf
1112

1213
RUN chmod -R 777 wp-content && chmod +x /kool/wordpress-entrypoint
1314

8.1-nginx-prod/entrypoint

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/bin/bash
22
set -eo pipefail
33

4-
# Nginx server config
5-
dockerize -template /kool/default.tmpl:/etc/nginx/conf.d/default.conf
4+
# Add WebP support for WordPress by injecting include directive
5+
# Insert the include directive before the location / block
6+
sed -i 's/\( location \/ {\)/ # Include WordPress WebP support\n include \/etc\/nginx\/wordpress-webp.conf;\n\n\1/' /kool/default.tmpl
67

78
# Run as current user
89
CURRENT_USER=${ASUSER:-${UID:-0}}

8.1-nginx-prod/wordpress-webp.conf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# WordPress WebP support: automatically serve WebP images when available and supported
2+
location ~* ^(.+)\.(jpe?g|png)$ {
3+
set $webp_suffix "";
4+
5+
# Check if WebP is supported by the client
6+
if ($http_accept ~* "image/webp") {
7+
set $webp_suffix ".webp";
8+
}
9+
10+
# Try to serve the WebP version first, then fallback to original
11+
try_files $uri$webp_suffix $uri /index.php?$query_string;
12+
13+
# Set appropriate cache headers for images
14+
expires 30d;
15+
add_header Cache-Control "public, immutable";
16+
add_header Vary "Accept";
17+
}

8.1-nginx/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FROM wordpress:cli-php8.1 as wordpress-cli
2-
FROM wordpress:php8.1-fpm-alpine as wordpress
1+
FROM wordpress:cli-php8.1 AS wordpress-cli
2+
FROM wordpress:php8.1-fpm-alpine AS wordpress
33
FROM kooldev/php:8.1-nginx
44

55
ENV NGINX_ROOT=/app
@@ -8,6 +8,7 @@ COPY --from=wordpress-cli /usr/local/bin/wp /usr/local/bin/wp
88
COPY --from=wordpress --chown=kool:kool /usr/src/wordpress /kool/wordpress
99
COPY --from=wordpress --chown=kool:kool /var/www/html/wp-content /app/wp-content
1010
COPY entrypoint /kool/wordpress-entrypoint
11+
COPY wordpress-webp.conf /etc/nginx/wordpress-webp.conf
1112

1213
RUN chmod -R 777 wp-content && chmod +x /kool/wordpress-entrypoint
1314

8.1-nginx/entrypoint

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/bin/bash
22
set -eo pipefail
33

4-
# Nginx server config
5-
dockerize -template /kool/default.tmpl:/etc/nginx/conf.d/default.conf
4+
# Add WebP support for WordPress by injecting include directive
5+
# Insert the include directive before the location / block
6+
sed -i 's/\( location \/ {\)/ # Include WordPress WebP support\n include \/etc\/nginx\/wordpress-webp.conf;\n\n\1/' /kool/default.tmpl
67

78
# Run as current user
89
CURRENT_USER=${ASUSER:-${UID:-0}}

8.1-nginx/wordpress-webp.conf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# WordPress WebP support: automatically serve WebP images when available and supported
2+
location ~* ^(.+)\.(jpe?g|png)$ {
3+
set $webp_suffix "";
4+
5+
# Check if WebP is supported by the client
6+
if ($http_accept ~* "image/webp") {
7+
set $webp_suffix ".webp";
8+
}
9+
10+
# Try to serve the WebP version first, then fallback to original
11+
try_files $uri$webp_suffix $uri /index.php?$query_string;
12+
13+
# Set appropriate cache headers for images
14+
expires 30d;
15+
add_header Cache-Control "public, immutable";
16+
add_header Vary "Accept";
17+
}

8.2-nginx-prod/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FROM wordpress:cli-php8.2 as wordpress-cli
2-
FROM wordpress:php8.2-fpm-alpine as wordpress
1+
FROM wordpress:cli-php8.2 AS wordpress-cli
2+
FROM wordpress:php8.2-fpm-alpine AS wordpress
33
FROM kooldev/php:8.2-nginx-prod
44

55
ENV NGINX_ROOT=/app
@@ -8,6 +8,7 @@ COPY --from=wordpress-cli /usr/local/bin/wp /usr/local/bin/wp
88
COPY --from=wordpress --chown=kool:kool /usr/src/wordpress /kool/wordpress
99
COPY --from=wordpress --chown=kool:kool /var/www/html/wp-content /app/wp-content
1010
COPY entrypoint /kool/wordpress-entrypoint
11+
COPY wordpress-webp.conf /etc/nginx/wordpress-webp.conf
1112

1213
RUN chmod -R 777 wp-content && chmod +x /kool/wordpress-entrypoint
1314

8.2-nginx-prod/entrypoint

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/bin/bash
22
set -eo pipefail
33

4-
# Nginx server config
5-
dockerize -template /kool/default.tmpl:/etc/nginx/conf.d/default.conf
4+
# Add WebP support for WordPress by injecting include directive
5+
# Insert the include directive before the location / block
6+
sed -i 's/\( location \/ {\)/ # Include WordPress WebP support\n include \/etc\/nginx\/wordpress-webp.conf;\n\n\1/' /kool/default.tmpl
67

78
# Run as current user
89
CURRENT_USER=${ASUSER:-${UID:-0}}

8.2-nginx-prod/wordpress-webp.conf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# WordPress WebP support: automatically serve WebP images when available and supported
2+
location ~* ^(.+)\.(jpe?g|png)$ {
3+
set $webp_suffix "";
4+
5+
# Check if WebP is supported by the client
6+
if ($http_accept ~* "image/webp") {
7+
set $webp_suffix ".webp";
8+
}
9+
10+
# Try to serve the WebP version first, then fallback to original
11+
try_files $uri$webp_suffix $uri /index.php?$query_string;
12+
13+
# Set appropriate cache headers for images
14+
expires 30d;
15+
add_header Cache-Control "public, immutable";
16+
add_header Vary "Accept";
17+
}

8.2-nginx/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FROM wordpress:cli-php8.2 as wordpress-cli
2-
FROM wordpress:php8.2-fpm-alpine as wordpress
1+
FROM wordpress:cli-php8.2 AS wordpress-cli
2+
FROM wordpress:php8.2-fpm-alpine AS wordpress
33
FROM kooldev/php:8.2-nginx
44

55
ENV NGINX_ROOT=/app
@@ -8,6 +8,7 @@ COPY --from=wordpress-cli /usr/local/bin/wp /usr/local/bin/wp
88
COPY --from=wordpress --chown=kool:kool /usr/src/wordpress /kool/wordpress
99
COPY --from=wordpress --chown=kool:kool /var/www/html/wp-content /app/wp-content
1010
COPY entrypoint /kool/wordpress-entrypoint
11+
COPY wordpress-webp.conf /etc/nginx/wordpress-webp.conf
1112

1213
RUN chmod -R 777 wp-content && chmod +x /kool/wordpress-entrypoint
1314

0 commit comments

Comments
 (0)