Aus welchem Grund auch immer ist im offiziellen Docker Image von WordPress das Modul „headers“ nicht aktiviert. Ich wollte nun in der .htaccess das Indizieren der Bilder durch Google und Co. verhindern, konnte aber den Standard dafür nicht verwenden:
<FilesMatch "\.(gif|jpe?g|png)$">
Header set X-Robots-Tag "noindex, noarchive, nosnippet"
</FilesMatch>
Damit habe ich einen schicken Internal Server Error bekommen mit der Fehlermeldung im Log:
/var/www/html/.htaccess: Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration
Erstmal konnte ich das Problem umgehen indem ich das Modul manuell im Container aktiviert habe:
docker exec -it CONTAINERNAME bash
dann dort:
a2enmod headers
service apache2 restart
Nun konnte ich erstmal die Änderung in der .htaccess einfügen ohne einen Internal Server Error.
ABER
Das bleibt ja nicht so. Beim nächsten Restart des Containers wird die Änderung verschwunden sein und statt WordPress kommt ein Internal Server Error. Also muss ich das persistent hinbekommen.
Mein erster Gedanke war: Ich hole mir das Dockerfile vom offiziellen WordPress und baue die Änderung ein. Anschließend muss ich dann aber immer das ganze Dockerfile nachpflegen falls es Änderungen im offiziellen Dockerfile gibt….
Aber dann bin ich auf dieses Issue bei Github gestoßen in dem thomashigginbotham mir die recht einfache Lösung gegeben hat:
I know this was closed a couple years ago, but I recently had a need to add headers to disable CORS for files not managed by WordPress (i.e. fonts, style sheets, etc.). Because mod_headers is disabled in this image, that's not possible. For those that want to enable it, here's an elaboration on what @babatundebusari mentioned.
1. Stop your container with docker-compose down.
2. If you are only using a docker-compose.yml file, you will need to add a Dockerfile file in the same directory. Add the following to the Dockerfile:
FROM wordpress:latest
RUN a2enmod headers
3. Add a build option to your wordpress settings as follows:
services:
wordpress:
build: .
image: wordpress:latest
# Remaining settings...
4. Rebuild the image with docker-compose build.
5. Start your container with docker-compose up.
Now you can use mod_headers. I still believe that it should already have been enabled in the image, since that's the default in Apache, and what most people would expect.