-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Modify apache config to record forwarded IPs #383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Most of this configuration comes directly from Debian's default configuration -- we only make minor changes to it, if at all (and all of that comes from the |
Understood. This is simply a replacement config file to handle proxied communications with the container, which I would expect is a pretty common use case, and still provide the client IPs for logging purposes. |
Also interested in a solution like this. But the /etc/apache2/apache2.conf says:
Also the snippet suggested by @XenoPhage and another form @brainlid on this issue didn't work for me. |
Also facing this problem using https://github.com/jwilder/nginx-proxy and wordpress in docker. The headers ( |
@gingerlime have you solved your issue yet? I'm running into a similar situation using Traefik. All the wordpress containers behind traffic are showing the internal IP of traefik rather than the external IP. This makes plugins useless that work by IP, aka whitelisting, security-based plugs etc. Still looking for a solution as I'm not convinced that the problem lies at the proxy level but rather the wordpress container level (apache). |
@chrisbloemker It definitely looks like it's due to the way apache is set up for wordpress in docker. I haven't solved it yet (didn't find the time and it's not urgent for me, but I would definitely hope to find a simple solution). |
@gingerlime I agree, I'm running a handful of wordpress containers and just realized this issue. I need to fix asap, but I'm not experienced with nginx and not so much apache :/ I'll keep looking. I'm surprised there's not much else talk on this problem. If I find a solution I'll keep you updated. |
I think the changes that @XenoPhage originally suggested should work, but didn't get a chance to look at what it means to apply them in practice or test it. |
I am also looking for a solution on it. Still facing this and didn't found a clever solution. Not a big problem here but should be better if the logged IP address was the external rather than the internal. |
FYI : This is pretty easy to test live without rebuilding a container if you want. Take the config file I provided above and docker cp it into your container :
And then just restart the container. Be careful not to remove ( If you want to make the change "permanent" you can use a docker volume to replace the file on start, or you can rebuild the container. The former solution works better if you want to continue getting updates from upstream, otherwise you have to monitor and rebuild on your own. |
Thanks for the suggestion @XenoPhage. I managed to get the volume solution working for me. In case it helps others, I'm using version: '3'
services:
...
my_wordpress:
image: wordpress:5
environment:
...
volumes:
- /path/to/wordpress:/var/www/html
- /path/to/000-default.conf:/etc/apache2/sites-enabled/000-default.conf |
For completeness, here's what I use for my wordpress containers. Note: This works for multisite as well if that's your thing. version: '3.6'
services:
wordpress:
image: wordpress:latest
container_name: wordpress
restart: always
volumes:
- /srv/wordpress/wp-content:/var/www/html/wp-content:Z
- /srv/wordpress/docker-php-upload-size.ini:/usr/local/etc/php/conf.d/docker-php-upload-size.ini:Z
- /srv/wordpress/wp-config.php:/var/www/html/wp-config.php:Z
- /srv/wordpress/.htaccess:/var/www/html/.htaccess:Z
- /srv/wordpress/000-default.conf:/etc/apache2/sites-enabled/000-default.conf:Z
labels:
traefik.docker.network: "web"
traefik.enable: true
traefik.port: 80
traefik.protocol: "http"
traefik.frontend.rule: "Host:myblog.example.com"
traefik.frontend.headers.customResponseHeaders: "X-Clacks-Overhead: GNU Terry Pratchett"
networks:
web:
aliases:
- wordpress
database:
networks:
web:
external:
name: web
database:
external:
name: database |
I will have to try that, @XenoPhage good suggestion. Slightly off topic, out of curiosity, what are the benefits to bind mounting the |
/srv is just where I decided docker volume mounts should go. I think when I first started playing with docker, the examples I saw did this and it just stuck. But yes, it could just as well be a shared filesystem. As for the individual mounts I have listed, each one has a purpose. wp-content is the themes, plugins, and any uploaded files, the php.ini file just ups the max upload size allowed, the config file is self explanatory, .htaccess for security, and the apache config to fix the IP issue we've been discussing. It was suggested to me that I should just do the entire wordpress directory, but I wanted to avoid putting the core wordpress files on a volume mount since that's what the container is for to begin with. Otherwise I could just as easily use an php apache container and point it at a wordpress volume mount. I try to ensure only the state is stored in volumes and not the immutable bits. |
@XenoPhage your solution seems to have worked! Thanks a bunch. The access logs show the correct external IP as well as the plugins are showing the same IP based on what apache is logging. Will soon test more plugins just to make sure.
Where as before I was only getting the 10.255.0.3 (traefik's internal IP) I have now made the
and then added this to my Dockerfile:
All seems to be well with the world now. To be clear, I built locally and pushed to docker hub and then just used Anisble to re-deploy my WordPress stack with the newer tagged image and the site was all intact, no issues :) |
Well, after a few days, sadly the fix I posted above does not work anymore. I thought maybe the WordPress plugins used the apache logs. I implemented that edit into my WordPress image, and now after a few days, the internal IP's are back in the container logs and the plugins are not working now as it's reading the 10.X.X.X addresses instead of the real IP's :( |
Looking at this again, I think this does make sense for us to do something with. I think the best approach here is the one recommended by both Apache and Debian, namely Their configuration is similar to what I was considering -- trust the non-public IPv4 ranges by default. I think the only real controversial points are whether to use |
@tianon is that
I'm also more confused as I've used the same WordPress image built from the dockerfile above for months and have not run into this issue until very recently.. I thought maybe originally it was an issue with traefik/nginx but it seems that's not the case. |
You can use Nextcloud's Dockerfile as an example for incorporating it into your own |
@wglambert this looks much more elegant, and more secure (whitelisting the proxy IP range), but for some reason I can't get it to work. At least not on its own (i.e. without also changing the apache default config)... Any tips on how to make it work? |
I submit PR #411 for this purpose. |
@haozhou I think your PR is a bit less secure though than the nextcloud version, because it doesn't whitelist the trusted internal IP range (this means that anyone can add a Nevertheless, I still wonder about the changes you mention to the @tianon you also mentioned Nextcloud, can you shed some light on this? |
@gingerlime The proxy internal IP/range varies between different system. I can make it an environment variable but a fixed value is nondeterministic. Nextcloud hardcoded the RemoteIPTrustedProxy which is not a good idea. Per my understanding, after you enable mod_remoteip, %h is still the IP of the proxy, you need to use %a for the actual client IP. You can refer to https://httpd.apache.org/docs/2.4/mod/mod_remoteip.html |
Nextcloud's implementation includes an IP range (subnets) which are reserved for internal IPs only, and therefore usually safe. ( |
ok. I added them in the remoteip.conf |
@gingerlime @tianon Can you please review the PR and raise your concern? If you have no concern, can you please approve it so that I can merge it? |
My reading of https://httpd.apache.org/docs/2.4/mod/mod_log_config.html implies that |
I was also hoping it would work this way, but without also changing the |
@tianon Enabling Luckily, both If both of you are fine with the PR, can you please merge it? |
If you run this container behind a proxy, the access logs show the IP of the proxy and not the client. This can be corrected by updating the /etc/apache2/sites-enabled/000-default.conf config as follows:
Can this container be updated to do this by default instead of having to either rebuild it or add a volume?
The text was updated successfully, but these errors were encountered: