Discourse in a subfolder, multiple servers sharing a domain

subfolder

(Neil Lalonde) #1

Let’s say you have a WordPress blog on http://www.example.com, and you want to serve your Discourse forums (which run on a different server) from http://www.example.com/forum. 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 /forum.

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 !~ "^/forum"

For the Discourse host, /forum URLs should match.

req.url ~ "^/forum"


How can I change my installation from forum.website.com to www.website.com/forum?
Subfolder support with Docker
Blog on subfolder in discourse
Discourse as an SSO provider for a set of Discourse sites
Add path to cookie
Install Discourse as subfolder within Wordpress website?
How to install Wordpress and Discourse together? (Ubuntu)
#2

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


(Neil Lalonde) #3

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


(Christopher Heald) #4

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?


Category subdomains - Same Discourse instance on different subdomains
(Sam Saffron) #5

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


(Xavi Ivars) #6

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:


(Ben) #7

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.


(Ben) #8

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


Generals Subfolder recommendations and tips
(Mangel) #10

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!


502 error in subfolder installation, multiple servers sharing a domain