# How can I modify the Nginx configuration directly within discourse/docker?

**URL:** <https://meta.discourse.org/t/how-can-i-modify-the-nginx-configuration-directly-within-discourse-docker/406863>\
**Category:** Self-hosting\
**Tags:** nginx\
**Created:** [July 5, 2026, 8:51am UTC](https://meta.discourse.org/t/how-can-i-modify-the-nginx-configuration-directly-within-discourse-docker/406863 "2026-07-05T08:51:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boubou](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/boubou/32/569633_2.png) [@Boubou](https://meta.discourse.org/u/Boubou)\
**Post date:** [July 5, 2026, 8:51am UTC](https://meta.discourse.org/t/how-can-i-modify-the-nginx-configuration-directly-within-discourse-docker/406863/1 "2026-07-05T08:51:52Z")

</div>

I would like to modify the Nginx/Docker configuration because my Discourse installation is behind HAProxy. The issue I am facing is as follows:  
All IPs connecting to my Discourse installation appear as the HAProxy IP address.  
I need to add the following lines to the Nginx configuration file:

`set_real_ip_from 127.0.0.1;`  
`real_ip_header proxy_protocol;`

…in order to pass the public IPs connecting to my Discourse installation through to Nginx.  
Thank you for your help.

---

<div class="post-metadata">

**Author:** ![Ethsim2](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ethsim2/32/522255_2.png) [@Ethsim2](https://meta.discourse.org/u/Ethsim2)\
**Post date:** [July 5, 2026, 9:42am UTC](https://meta.discourse.org/t/how-can-i-modify-the-nginx-configuration-directly-within-discourse-docker/406863/2 "2026-07-05T09:42:50Z")

</div>

You probably do **not** want only:

```nginx
set_real_ip_from 127.0.0.1;
real_ip_header proxy_protocol;

```

That is only correct if **both** of these are true:

1. HAProxy is actually sending the PROXY protocol to the Discourse container.
2. Nginx inside the Discourse container sees HAProxy as `127.0.0.1`.

In many HAProxy → Discourse Docker setups, the simpler and more common approach is to use `X-Forwarded-For`, not PROXY protocol.

For HAProxy, make sure it sends the forwarded IP header:

```haproxy
defaults
    mode http
    option httplog
    option forwardfor

```

or in the backend:

```haproxy
backend be_discourse
    option forwardfor
    server app 127.0.0.1:8080 check

```

Then in the Discourse container config, persist the Nginx real-IP changes through `app.yml`. Do not edit files directly inside the running container, because they will be lost on rebuild.

Add something like this to `/var/discourse/containers/app.yml` under the existing `run:` section:

```yml
run:
  - replace:
      filename: /etc/nginx/conf.d/discourse.conf
      from: "types {"
      to: |
        set_real_ip_from 127.0.0.1;
        # adjust this to the actual Docker bridge/network range HAProxy connects from
        set_real_ip_from 172.17.0.0/16;
        real_ip_header X-Forwarded-For;
        real_ip_recursive on;
        types {

```

Then rebuild:

```bash
cd /var/discourse
./launcher rebuild app

```

You may need to adjust `set_real_ip_from` to match the actual source IP/range that Nginx inside the Discourse container sees when HAProxy connects to it. With Docker, this is often not `127.0.0.1`; it may be a Docker bridge address such as `172.17.0.1` or a user-defined Docker network range.

If you really want to use the PROXY protocol instead, HAProxy must explicitly send it:

```haproxy
backend be_discourse
    server app 127.0.0.1:8080 check send-proxy

```

and Nginx must listen with `proxy_protocol`, not just read `real_ip_header proxy_protocol`:

```yml
run:
  - replace:
      filename: /etc/nginx/conf.d/discourse.conf
      from: "listen 80;"
      to: "listen 80 proxy_protocol;"

  - replace:
      filename: /etc/nginx/conf.d/discourse.conf
      from: "types {"
      to: |
        set_real_ip_from 127.0.0.1;
        set_real_ip_from 172.17.0.0/16;
        real_ip_header proxy_protocol;
        types {

```

The important correction is: **`real_ip_header proxy_protocol` alone is incomplete**. Nginx also needs `listen 80 proxy_protocol;`, and HAProxy needs `send-proxy`. Otherwise, use `X-Forwarded-For`, which is the usual HAProxy HTTP-mode setup. In HAProxy, `option forwardfor` is the standard way to add the `X-Forwarded-For` client IP header. With Nginx PROXY protocol, the `listen` directive must include `proxy_protocol` before `real_ip_header proxy_protocol` can work.

So the short version is:

- Use `X-Forwarded-For` unless you have a specific reason to use PROXY protocol.
- Do not mix the two modes.
- If HAProxy is not using `send-proxy`, then `real_ip_header proxy_protocol` will not work.
- If using Discourse Docker, make the change in `app.yml`, not directly inside the running container.

---

<div class="post-metadata">

**Author:** ![Boubou](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/boubou/32/569633_2.png) [@Boubou](https://meta.discourse.org/u/Boubou)\
**Post date:** [July 5, 2026, 4:53pm UTC](https://meta.discourse.org/t/how-can-i-modify-the-nginx-configuration-directly-within-discourse-docker/406863/3 "2026-07-05T16:53:27Z")

</div>

Thank you for your help.

Here is the solution that worked for me:

1. I disabled Let’s Encrypt and HTTPS:

```plaintext
#- templates/web.ssl.template.yml

#- templates/web.letsencrypt.ssl.template.yml

```

1. HAProxy now handles SSL certificates for Discourse
2. I used this configuration in the `/var/discourse/containers/app.yml` file

```plaintext
run:
  - replace:
      filename: /etc/nginx/conf.d/discourse.conf
      from: "types {"
      to: |
        set_real_ip_from 192.168.1.8; # HAProxy IP ADDRESS
        real_ip_header X-Forwarded-For;
        real_ip_recursive on;
        types {

```

and now I do have the public IPs in the Discourse admin interface:

 ![Screenshot_20260705_184755_Chrome](https://global.discourse-cdn.com/meta/original/4X/e/a/9/ea938b0613839a29ff076f2a205bd9a6fb52d1ac.jpeg)

@Ethsim2

Thanks again for your help. 😁

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [August 4, 2026, 4:54pm UTC](https://meta.discourse.org/t/how-can-i-modify-the-nginx-configuration-directly-within-discourse-docker/406863/4 "2026-08-04T16:54:07Z")

</div>

Closed on August 4, 2026, 4:54pm UTC

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