Score:0

Nginx - redirect subdirectory to a different domain without changing URL

jp flag

We've set up a Matrix-Synapse chat server at the root of one domain, and we'd like to redirect the /element subdirectory to the Element client hosted at another domain, but without changing the browser's URL.

We've previously used an nginx configuration like this to achieve it, but for some reason it's not working with this setup. The redirect works, but it's changing the browser URL. Here's the nginx config file on the Matrix domain; example.matrix is the Matrix server's domain, example.element is the Element client's domain.

upstream element {
    server example.element;
}

server {
    listen 80;
    listen [::]:80;
    server_name example.matrix;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.matrix;

    ssl_certificate /etc/letsencrypt/live/example.matrix/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.matrix/privkey.pem;

    location /element/ {
        proxy_pass http://element/;
        proxy_set_header Host $host;
    }

    location / {
        proxy_pass http://localhost:8008;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

server {
    listen 8448 ssl default_server;
    listen [::]:8448 ssl default_server;
    server_name example.matrix;
 
    ssl_certificate /etc/letsencrypt/live/example.matrix/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.matrix/privkey.pem;

    location ~ ^(/_matrix|/_synapse/client) {
        proxy_pass http://localhost:8008;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        client_max_body_size 50M;
    }
}

Can anyone spot what we're missing in this config?

us flag
Have you set the root URL properly in the chat server?
Charles Johnson avatar
jp flag
Yes, we tried setting the root path and it had no effect. Also tried setting the proxy_pass to ```https://element/``` (instead of http:), still no effect.
us flag
What is the chat server and what was the setting name for root URL there that you set?
Charles Johnson avatar
jp flag
It's the Matrix-Synapse server - our nginx config follows their recommendation from this page: https://matrix-org.github.io/synapse/latest/reverse_proxy.html They don't specify a root, probably because port 443 is being forwarded. Browsing to the domain root shows a dynamically generated page.
us flag
Actually I didn't see the question clearly in the first read. You need to check the root URL setting in the Element application here.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.