Use a subfolder (path prefix) to serve Discourse with multiple servers sharing a domain

Let’s say you have a WordPress blog on http://=DOMAIN=, and you want to serve your Discourse forums (which run on a different server) from http://=DOMAIN==PATH=. How do you do that?


Note: This won’t work for serving multiple Discourse instances from different folders on the same domain. You need to use different subdomains so that each site can have different cookies.


You’re going to need to send all traffic for the domain to one place that can route traffic to the correct server. In this how-to, I’ll use Fastly. So, Discourse will be running on one server, and the other parts of your site (like WordPress) will run on one or more other servers.

Docker container changes

First, follow the instructions here to serve Discourse from a subfolder.

Fastly

Now to setup Fastly to send traffic to the right place based on the path. I’ll assume that Discourse is being served from =PATH=.

Create a new service pointing to your main website and follow the instructions for updating your DNS settings.

Go to the service and click “Configure”. Make sure you have selected Version 2 so that you can make changes. Version 1 is the currently active version and can’t be changed.

In the “Hosts” section, add your Discourse server as a second backend.

In the Settings tab, add a new entry under Request Settings named “Discourse Pass” with action “Pass”.

Finally, for each host edit the conditions to specify where to route traffic.

For your main website, non-Discourse URLs should match.

req.url !~ "^=PATH="

For the Discourse host, =PATH= URLs should match.

req.url ~ "^=PATH="

15 Likes

Any advice on other CDN services like Fastly with support this kind of “redirection”? Tks

Other services that can direct traffic to the correct places should work fine. I haven’t used any others for this setup though.

2 Likes

Is a service like Fastly required to have Discourse (running in a Docker container) served from a subfolder and have Wordpress on the host server?

I followed the directions to serve Discourse from a subfolder, and modified my host nginx configuration file, and it sort of works, but there are loose ends. Some assets are served correctly, but others aren’t (they’re missing the Discourse folder name).

Before I dig much deeper, is what I am trying to do even possible?

1 Like

Nope, you can redistribute the traffic from the host using NGINX, Varnish or HAProxy. It is an advanced setup, but totally doable.

2 Likes

Is there any sample configuration for Nginx?

I’ll try to do such an install in the next few days, and I’d love some help :slightly_smiling:

1 Like

I have the same question like @xavivars, can someone help?

Now i struggle how to configure nginx to redirect all request to example.com/forum to the discourse docker and all other (Example Domain or example.com/app) to the nginx folder for the website.

I found a solution how to run discourse with docker with subfolder installation (example.com/forum) and serve other site content (Example Domain or example.com/otherContent) with nginx on the same machine. … and it’s with https

6 Likes

I’m with @xavivars and @BenRoe, I think that more people would prefer configure this without using an external service.

This one works for me:

Principal Server

Add this block to your site configuration and restart nginx.

location /forum {
    rewrite  ^/forum/(.*)  /$1 break;
    proxy_pass https://forum.myserver.com/;
    proxy_redirect off;
    proxy_buffering off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Forum Server

You must install and configure nginx on this server, pointing to the discourse installation. Here is a good tutorial to do this: https://serversforhackers.com/video/installing-discourse-with-docker. You don’t need to divide the Discourse installation in three containers if you don’t want it, just follow the steps to expose the web container in a different port than 80.

Finally, this configuration works for me, you are free to use it if it fits for you:

upstream discourse {
    server 127.0.0.1:8080;
}

server {
    listen 80 default_server;
    server_name forum.myserver.com;
    return 301 https://forum.myserver.com$request_uri;
}

server {
    listen 443 default_server ssl;

    client_body_buffer_size     32k;
    client_header_buffer_size   8k;
    large_client_header_buffers 8 64k;

    root /var/www/discourse/public;
    index index.html index.htm;

    access_log /var/log/nginx/discourse.log;
    error_log  /var/log/nginx/discourse.log error;

    server_name forum.myserver.com;
    merge_slashes on;

    charset utf-8;

    ssl_certificate /etc/letsencrypt/live/forum.myserver.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/forum.myserver.com/privkey.pem;

    location ~ /.well-known {
       allow all;
    }

    location / {
        include proxy_params;
        proxy_pass http://discourse;

        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

This includes the SSL support, you can remove it if you don’t want it.

Cheers!

5 Likes

does this means below two ideas both does not achive
one idea is
I have a discourse instances is http://forums.example.com
make another discourse in another server use http://forums.exampe.com/en/

another idea is
one discourse is http://www.example.com/en/forums
the other is http://www.example.com/cn/forums

8 posts were merged into an existing topic: How to run discourse on subdirectory of external domain?