Home > Mobile >  nginx reversre proxy not returning the correct app endpoint
nginx reversre proxy not returning the correct app endpoint

Time:12-05

I am installing a nginx reverse proxy along with a few applications (netdata, filebrowser, etc) on a docker compose file.

my idea is from computers outside of my network to call an url like http://netdata.myserver.com and point to netdata. At the moment it works when I do http://myserver.com:19999/ (The end goal is to remove the ports for subdomains).

I have the next configuration in the hosts file:

127.0.0.1 myserver.com # thats not the ip, but you get the idea

So i have netdata, filebrowser and nginx on the docker compose file, like the next:

version: '3'
services:
  netdata:
    image: netdata/netdata
    ports:
      - 19999:19999
    cap_add:
      - SYS_PTRACE
    security_opt:
      - apparmor:unconfined
  filebrowser:
    image: filebrowser/filebrowser
    user: 501:501
    ports:
      - 20001:80
    volumes:
      - volumes and things go here
    restart: unless-stopped

  nginx:
    image: nginx
    ports:
      - 80:80
    volumes:
      - default.conf:/etc/nginx/conf.d/default.conf:ro
    environment:
      - NGINX_HOST=myserver.com
      

and then in the default.conf I have the next configuration

server {
    server_name netdata.myserver.com;
    location / {
        proxy_pass         http://netdata:19999;
       }
    listen 80;
}

But it is not working as expected, when i go to http://netdata.myserver.com i get a "cant reach this site" but, if i go to http://myserver.com it does return netdata for me; which I do not understand why either.

Can someone help me with this. Thanks.

CodePudding user response:

I think there may be a couple of problems here. First, if you expect to be host two name-based virtual hosts behind Nginx, you'll need two server blocks (one for each). So your default.conf file should probably look like this:

server {
    listen 80;
    server_name myserver.com;
    location / {
        proxy_pass         http://filebrowser:2001
       }
}
server {
    listen 80;
    server_name netdata.myserver.com;
    location / {
        proxy_pass         http://netdata:19999;
       }
}

You will also need to be able to resolve both host names, which means you're missing an entry from your example hosts file. You would need something like:

127.0.0.1 myserver.com netdata.myserver.com

With this configuration, a request for anything other than netdata.myserver.com would get served by your filebrowser container. Requests for netdata.myserver.com would get served by the netdata container.

I've put together a runnable example here if that helps. That would require a local hosts file that looks something like:

127.0.0.1 myserver.com netdata.myserver.com
  • Related