How to add X-Real-IP header?

1. Caddy version (caddy version):

2.3

2. How I run Caddy:

systemctl reload caddy

a. System environment:

Debian

b. Command:

systemctl reload caddy

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:

mydomain.com, www.mydomain.com {
reverse_proxy 11.22.33.44
import wordpress
}

3. The problem I’m having:

How can I add a X-Real-IP header to the above config?

4. Error messages and/or full log output:

Jan 19 20:18:52 caddy caddy[2393]: reload: adapting config using caddyfile: /etc/caddy/Caddyfile:82: unrecognized directive: header_up
Jan 19 20:18:52 caddy systemd[1]: caddy.service: Control process exited, code=exited, status=1/FAILURE

5. What I already tried:

mydomain.com, www.mydomain.com {
reverse_proxy 11.22.33.44 {
header_up X-Real-IP {remote_host}
}
import wordpress
}

6. Links to relevant resources:

Are you sure you need to add X-Real-IP? Caddy already provides it via X-Forwarded-For, automatically:

It seems like you have at least 82 lines in your Caddyfile, so you clearly haven’t shared the whole thing. Please don’t redact your Caddyfile, it’s difficult to talk about incomplete information.

1 Like

Hi sorry but its various sites repeated like this in the config:
mydomain.com, www.mydomain.com {
reverse_proxy 11.22.33.44
import wordpress
}
And on one of the sites I added header_up config as show on my example above and i got this error.

Well, what you quoted adapts just fine for me (after defining a wordpress snippet, which you didn’t include):

$ cat Caddyfile
(wordpress) {
    log
}

mydomain.com, www.mydomain.com {
reverse_proxy 11.22.33.44 {
header_up X-Real-IP {remote_host}
}
import wordpress
}

$ caddy adapt
2021/01/19 21:58:27.184	INFO	using adjacent Caddyfile
{"apps":{"http":{"servers":{"srv0":{"listen":[":443"],"routes":[{"match":[{"host":["mydomain.com","www.mydomain.com"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","headers":{"request":{"set":{"X-Real-Ip":["{http.request.remote.host}"]}}},"upstreams":[{"dial":"11.22.33.44:80"}]}]}]}],"terminal":true}],"logs":{}}}}}}

So clearly you have an issue somewhere in your config that you haven’t shared.

One of our forum rules is that we ask users not to redact their config. It only makes it harder to help. It wastes both our time, and yours.

Also, please run caddy fmt (with -overwrite to fix in-place) on your config to clean up the indentation, it makes it much easier to read.

2 Likes

I see you marked this as solved @dRENi. What was the solution?

Hi Francis, this is the wordpress part:

(wordpress) {
# Redirect to remove “www.” since wordpress doesn’t
# like multi-domain visits.
@strip_www {
header_regexp www Host ^www.(.*)$
}
redir @strip_www https://{http.regexp.www.1}{uri}
# Some static files Cache-Control.
@static {
path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff .json
}
header @static Cache-Control max-age=2592000
@cache {
not header_regexp Cookie “comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in”
not path_regexp "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.
.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(index)?.xml|[a-z0-9-]±sitemap([0-9]+)?.xml)"
not method POST
not expression {query} != ‘’
}
route @cache {
try_files /wp-content/cache/supercache/{host}{uri}/index-https.html /wp-content/cache/supercache/{host}{uri}/index.html {path} {path}/index.php?{query}
}
# Change the path here according to your server
#php_fastcgi unix//run/php/php7.4-fpm.sock
# Use this header if you use something like WPML
# If you are running a single domain simple site you can remove this.
header Access-Control-Allow-Origin *
encode zstd gzip
file_server
}

After looking at your example and comparing to my config i noticed that I did not have a space on this part
reverse_proxy 11.22.33.44 {
mine was
reverse_proxy 11.22.33.44{

Thanks once again for great support.

Now that you see my wordpress part, do you see any issues with it?

1 Like

Thanks for following up.

For next time, please use ``` on the lines before and after your config/logs, to use code formatting. Configs should not be in quote blocks, but in code blocks, to preserve whitespace. It’s very difficult to read otherwise.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.