r/selfhosted icon

Go to selfhosted

I need help setting up an Nginx reverse proxy.

Proxy

Doing this without subdomains (app1.example.com) is a pain in the but.

Just use subdomains.

How do I do that? I'm very new to this. I thought using subdirectories would be easier.

More replies More replies

Mine look something like:

location /whatever_service/ { 
proxy_set_header X-Real-IP  $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://whatever_url_or_IP:port/whatever/;
}

But it should also be said that the server that is hosting on the other end of the proxy needs to be configured to serving out of a subdirectory.

In other words, if you normally hit jellyfin at http://192.168.0.10, but want to set your reverse proxy as /jellyfin/, then you need to be able to access jellyfin it at http://192.168.0.10/jellyfin first.

I think you can use rewrites to get around that, just been too lazy to learn new things LOL

edit: Try something like this:

server {
    listen          80;
    server_name     example.com www.example.com;

    location / {
        # only if needed
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
        proxy_pass http://<internalAdress>:1100;
    }

    location /app1 {
        rewrite ^/app1(.*)$ $1 break;
        proxy_pass http://<internalAdress>:1101;
    }

    location /app2 {
        rewrite ^/app2(.*)$ $1 break;
        proxy_pass http://<internalAdress>:1102;
    }
}

While I would recommend nginx proxy manager, which will do all the things you are asking, if you want to be able to understand what you're doing and see and generate example configs, take a look at this: https://www.digitalocean.com/community/tools/nginx

I personally use swag. Its very straightforward and it already has subdomain and subfolder config samples for many services, including ones you mentioned.

There are a lot of problems with swag, there is a bunch of funky stuff in the configuration of it that you will run into eventually. It has some really odd buffering settings that break and cause problems with lots of underlying sites. I don't recommend it, used it for well over a year and all the problems I had boiled down to swag.

More replies More replies

Let me make the case for Caddy. I used swag and its precursor for a long time before eventually the problems were just so obvious I had to change. Initially I tried nginx proxy manager and while it is easier to work with than swag it is still actually quite a lot of configuration work because nginx is a lot of work. The console access also turned out to be an issue for us given this was a public facing server.

So I tried caddy out and its a lot easier to get working. Its defaults are sensible, it performs really well and it has been rock solid stable and problem free. I think its the best one especially given how well it can deal with self certificates as well.

I'd recommend Caddy since it's config is super easy, and caddy automatically handles SSL certificates.

So you have your router port forwarding 80/443 to your webserver, then you just need to setup your Caddyfile.

As others said, just use subdomains.

example.com {
root * /data/www
file_server
}

jellyfin.example.com {
reverse_proxy http://192.168.x.x:8096
}

nextcloud.example.com {
reverse_proxy http://192.168.x.x:8080
}

Done