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

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