How to install Discourse behind Apache mod_proxy?

Hi all.

I am trying to set up Discourse in a Docker container. The host environment (Debian) is running an Apache httpd server (I can’t run nginx because I’m hosting other sites on Apache). I want to use mod_proxy to reverse-proxy to the container, which is listening on port 3000.

Apache’s current config for this vhost is as follows:

<VirtualHost *:80>
  ServerAdmin webmaster@example.com
  ServerName example.com
  ServerAlias www.example.com

  ErrorLog ${APACHE_LOG_DIR}/vhosts/example.com/error.log
  CustomLog ${APACHE_LOG_DIR}/vhosts/example.com/access.log combined

  <IfModule proxy_module>
  ## <https://meta.discourse.org/t/running-other-websites-on-the-same-machine-as-discourse/17247>
    ProxyPreserveHost on
    RequestHeader set X-Forwarded-Proto expr=%{REQUEST_SCHEME}
    RequestHeader set X-Real-IP expr=%{REMOTE_ADDR}
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
  </IfModule>
</VirtualHost>

I started with just the ProxyPass and ProxyPassReverse directives and added ProxyPreserveHost and the RequestHeader directives one at time. No matter how I configure the proxy using these directives, certain files below the document root return 404 when accessed (for example stuff in /images and /assets).

Note the comment containing the Discourse Meta URL that I converted from nginx syntax to Apache.

Any thoughts on how to get this to work would be much appreciated!

Have a look at How to set up Discourse on a server with existing Apache sites

「いいね!」 4

So the solution you’re proposing is to run Discourse behind HAProxy behind Apache?

As I said, I have to use Apache at the front because I am hosting other vhosts on this machine.

Why not nginx? I’m not sure of the performance on discourse with apache.

I don’t have the time or resources to switch to nginx at the moment.

To be clear, Discourse itself would be running on nginx but it needs to be proxied through Apache.

「いいね!」 1

I’ve done a bit more looking into this and I think I can accomplish what I’m after because it seems HAProxy can choose backends based on vhost.

Sorry. I did the wrong search. Perhaps you can try again and find a howto for Apache.

「いいね!」 1

HAProxy does what I’m after and looks like a very polished and useful tool. Thanks for the pointer.

However, it seems that editing templates/web*.yml to listen on ports other than the defaults of 80 and 443 was where the trouble was. I restored the default configs then edited containser/app.yml to have lines similar to the following, and the 404 errors went away:

expose:
  - "127.0.0.1:1234:80"    # http

I’ve been really happy with traefik lately, and it’ll do let’s encrypt certificates automatically (you can do that with HA-Proxy too, it’s just a bit more work.)

Glad you figured it out!

「いいね!」 1