[SOLVED] How to configure Lemmy instance nginx proxy for websockets?

3 years ago by penguin_ex_machina to c/selfhosted

I'm playing around with my own instance of Lemmy but I keep getting a "websocket connection failed" error in my console. I'm having a really hard time understanding how to set up nginx for websockets - I'm more used to Apache and not familiar with WS at all. Is there documentation hiding somewhere that will help me set up my proxy forwarding properly?

EDIT: Solved! Check out my solution here: https://lemmy.world/comment/141648. Thanks everyone!

nulldev 3 points 3 years ago

The default Lemmy nginx config should handle websockets properly. Are you putting it behind a second nginx layer?

path: 0 101403, hotness: undefined, score: 3, children: 3
penguin_ex_machina 1 point 3 years ago path: 0 101403 101747, hotness: undefined, score: 1, children: 2
nulldev 2 points 3 years ago

I would recommend you ditch the second nginx layer. It's a waste of resources and it can cause a multitude of issues if the configuration isn't done correctly.

  • If you are hosting multiple domains on the same server, disable the nginx container in the docker-compose.yml file and copy Lemmy's nginx config into your system's nginx config (e.g. /etc/nginx/).
    • If you go this route you should also delete the lemmyexternalproxy network, delete internal: true on the lemmyinternal network (required to enable port forwarding) and add port forwards to the lemmy and lemmy-ui docker services. Here's what that would look like: https://www.diffchecker.com/vjfEFuz6/
  • If you are not hosting multiple domains on the same server, simply edit the port forwards in the docker-compose.yml file for the proxy service to bind to whatever your external facing IP is.
path: 0 101403 101747 104844, hotness: undefined, score: 2, children: 1
penguin_ex_machina 1 point 3 years ago

AHA. That did it. Getting rid of the host's nginx fixed the issue. Now websockets work as expected and I can log in!

path: 0 101403 101747 104844 104983, hotness: undefined, score: 1, children: 0
ubergeek77 3 points 3 years ago

If you're willing to use Caddy instead, it's infinitely easier. Websockets is just enabled by default, no shenanigans. Here is an example Caddyfile to use in a Docker deployment (but you can change those http urls to point to localhost for a non Docker deployment):

(caddy-common) {
    encode gzip
    header {
        -Server
        Strict-Transport-Security "max-age=31536000; include-subdomains;"
        X-XSS-Protection "1; mode=block"
        X-Frame-Options "DENY"
        X-Content-Type-Options nosniff
        Referrer-Policy  no-referrer-when-downgrade
        X-Robots-Tag "none"
    }
}

your.lemmy.url {
    import caddy-common
    reverse_proxy   http://lemmy-ui:1234

    @lemmy {
        path    /api/*
        path    /pictrs/*
        path    /feeds/*
        path    /nodeinfo/*
        path    /.well-known/*
    }

    @lemmy-hdr {
        header Accept application/*
    }

    handle @lemmy {
        reverse_proxy   http://lemmy:8536
    }

    handle @lemmy-hdr {
        reverse_proxy   http://lemmy:8536
    }

    @lemmy-post {
        method POST
    }

    handle @lemmy-post {
        reverse_proxy   http://lemmy:8536
    }
}

Caddy has some great plugins that allow you to automate https certificate renewal too, easy to add to any config.

I know you asked about nginx and I'm just telling you "haha just switch," but I had similar headaches with my own deployment when I tried using nginx, and I eventually just gave up and used Caddy. Saved me at least a few hours of headache.

path: 0 101334, hotness: undefined, score: 3, children: 2
falcon15500 2 points 3 years ago

A +1 from me for Caddy. It works quite well.

path: 0 101334 111845, hotness: undefined, score: 2, children: 0
penguin_ex_machina 1 point 3 years ago

Hmmm I might consider this. I'll need to look into it more though, I've never heard of Caddy before. Thanks for the heads up :D

path: 0 101334 101373, hotness: undefined, score: 1, children: 0
aucubin 1 point 3 years ago

The nginx config provided in the Docker installation part contains everything needed for nginx. If you are installing lemmy directly on the machine you may need to use different upstreams.

The websocket part is basically the

            # proxy common stuff
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

part in the nginx config on that page.

path: 0 101084, hotness: undefined, score: 1, children: 9
penguin_ex_machina 1 point 3 years ago

I seem to be having a lot of lag at the moment, and my post was created twice so I'm just going to delete the other one and start from here...

So I have this set up per the instructions. My instance is on a Digital Ocean instance, and I'm using nginx on the host to point to localhost:1235, but that's about all that conf file is doing. Is there something else I need to do?

path: 0 101084 101186, hotness: undefined, score: 1, children: 8
aucubin 1 point 3 years ago

Ok, just to understand what you did. You got an Digital Ocean droplet with Docker and used the instructions in the link I posted or different ones?

If you are using the instructions from my link nginx will also run in a docker container, which means that your upstream will not be on localhost, but rather the lemmy and lemmy-ui containers.

If you did install it locally then localhost:1235 could be correct.

path: 0 101084 101186 101425, hotness: undefined, score: 1, children: 7
penguin_ex_machina 2 points 3 years ago

I think this is where my lack of experience with Docker is showing.

I spun up a DO droplet and installed nginx, Docker CE, and Docker Compose. Then I went through the instructions on the page you linked to and it set it up just fine but when I went to my droplets IP address it wouldn't connect. I had to add a config file that pointed traffic coming into the droplet on port 80 to redirect to the Docker container instead. Am I overcomplicating it?

path: 0 101084 101186 101425 101500, hotness: undefined, score: 2, children: 6
aucubin 1 point 3 years ago

No, you are right. If you are using the nginx container from the docker installation guide then you will also need to add port 80 atleast in order to see anything, as nginx will otherwise not listen on the port 80 of the droplet.

How does your nginx.conf look now?

path: 0 101084 101186 101425 101500 101787, hotness: undefined, score: 1, children: 5
pcouy 1 point 3 years ago

Got it working on my instance by using the following config for the nginx outside of the docker container (you'll need to change the server_name from the server sections and the path to the ssl keys) :

upstream lemmy {
    # depending on your setup, you may want to update this
    server 127.0.0.1:1380;
}

server {
    listen 80;
    listen [::]:80;
    server_name lemmy.pierre-couy.fr;
    location / { return 301 https://$host$request_uri; }
}
server {
    listen      443 ssl;
    listen [::]:443 ssl;
    server_name lemmy.pierre-couy.fr;

    # TLS
    ssl_protocols TLSv1.2;
    ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_certificate     /etc/ssl/certs/pcouy.pem;
    ssl_certificate_key /etc/ssl/private/pcouy.key;

    # HSTS
    add_header Strict-Transport-Security "max-age=31536000";

    location / {
        proxy_pass http://lemmy;
        proxy_set_header Host $host;
        # include proxy_params;
    }

    location ~ ^/(api|pictrs|feeds|nodeinfo|.well-known) {
        proxy_pass "http://lemmy";
        # proxy common stuff
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # Send actual client IP upstream
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
path: 0 102008, hotness: undefined, score: 1, children: 0
thorfinn 1 point 3 years ago

You can try add these to your nginx conf inside location / {:

proxy_set_header Connection "keep-alive, Upgrade";
proxy_set_header Upgrade websocket;

like this:

location / {
         .... 
         proxy_set_header Connection "keep-alive, Upgrade";
         proxy_set_header Upgrade websocket;
}

I launched my instance last weekend and this was my issue as too.

path: 0 141648, hotness: undefined, score: 1, children: 1
penguin_ex_machina 2 points 3 years ago

It's turns out, my issue was that I had nginx on both the host and Docker container, so the host wasn't forwarding the websocket requests like this. I just removed the host nginx and configured the Docker conf to look at port 80 and it worked!

path: 0 141648 141913, hotness: undefined, score: 2, children: 0
selfhosted
selfhosted

@lemmy.world

login for more options
63102
6834
6313

A place to share alternatives to popular online services that can be self-hosted without giving up privacy or locking you into a service you don't control.

Rules:

Detailed Rules Post

  1. Be civil.

  2. No spam.

  3. Posts are to be related to self-hosting.

  4. Don't duplicate the full text of your blog or readme if you're providing a link.

  5. Submission headline should match the article title.

  6. No trolling.

  7. Promotion posts require active participation, with an account that is at least 30 days old. F/LOSS without a paywall has exceptions, with requirements. See the rules link for details. Tags [CBH] or [AIP] are required, see the links in Rule 8 for details.

  8. AI-related discussions and AI-involved promotional posts have additional requirements for tagging, as noted in Rule 7 and the AI & Promotional Post Expanded Rules post, and find example disclosures here.

Resources:

Any issues on the community? Report it using the report flag.

Questions? DM the mods!

go to feed...