Home > Back-end >  Domain name not working on Digital Ocean droplet with Docker and Nginx
Domain name not working on Digital Ocean droplet with Docker and Nginx

Time:03-13

I've search a lot of online materials, but I wasn't able to find a solution for my problem. I'll try to make it as clear as possible. I think I'm missing something and maybe someone with more experience on the server configuration side may have the answer.

I have MERN stack app and I'm trying to deploy it on a DigitalOcean droplet, using Docker. All good so far, everything runs as it should should, except de fact that I'm not able to access my app by the domain. It works perfectly if I'm using the IP of the droplet.

What I've checked so far:

  • checked my ufw status and I have both HTTP and HTTPS enabled
  • the domain is from GoDaddy and it's live, linked with the proper namespaces from Digital Ocean.
  • in the domains sections from Digital Ocean everything it's set as it should. I have the proper CNAME records pointing to the IP of my droplet
  • a direct ping to my domain works fine (it returns the correct IP)
  • also checked DNS LookUp tools and everything seems to be linked just fine

When it comes to the Docker containers, I have 3 of them: client, backend and nginx.

This is how my docker-compose looks like:

version: '3'
services: 
    nginx:
        container_name: jtg-nginx
        depends_on:
            - backend
            - client
        restart: always
        image: host-of-my-image-nginx:latest
        networks:
            - local-net
        ports: 
            - '80:80'
    backend:
        container_name: jtg-backend
        image: host-of-my-image-backend:latest
        ports:
            - "5000:5000"
        volumes: 
            - logs:/app/logs
            - uploads:/app/uploads
        networks:
            - local-net
        env_file:
            - .env
    client:
        container_name: jtg-client
        stdin_open: true
        depends_on:
            - backend
        image: host-of-my-image-client:latest
        networks:
            - local-net
        env_file:
            - .env
networks:
    local-net:
        driver: bridge
volumes:
    logs:
        driver: local
    uploads:
        driver: local

I have two instances of Nginx. One is used inside the client container and the other one is used in it's own container.

This is the default.conf from the client:

server {
  listen 3000;
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
}

Now it comes the most important part. This is the default.conf used inside the main Nginx container:

upstream client {
    server client:3000;
}

upstream backend {
    server backend:5000;
}

server{
    listen 80;
    server_name my-domain.com www.my-domain.com;
    location / {
        proxy_pass http://client;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    location /backend {
        rewrite /backend/(.*) /$1 break;
        proxy_pass http://backend;
    }
}

I really don't understand what's wrong with this configuration and I think it's something very small that I'm missing out.

Thank you!

CodePudding user response:

If you want to setup a domain name in front, you'll need to have a webserver instance that allows you to proxy_pass your hostname to your container
So this is what you may wanna do :

server{
    listen 80;
    server_name my-domain.com www.my-domain.com;
    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    location /backend {
        rewrite /backend/(.*) /$1 break;
        proxy_pass http://backend;
    }
}

CodePudding user response:

The mistery was solved. After adding SSL certificate everything works as it should.

  • Related