Report this

What is the reason for this report?

Can access through IP but can't with domain

Posted on July 23, 2020

I am using a Docker droplet and I am trying to configure Nginx as a reverse proxy. When I type my IP in the browser I can see the HTML my page, but when I use my domain (mydomain.com) it doesn’t work. I notice that with Postman I can make a GET request HTTP and it loads the HTML page, on browser always try to use an HTTPS connection.

Here is my nginx.conf:

server {
    listen 80;
    listen [::]:80;
    listen 443;
    listen [::]:443;

    server_name iamgonzales.dev www.iamgonzales.dev;

    location ~ /.well-known/acme-challenge {
        allow all;
        root /usr/share/nginx/html;
    }

    root /usr/share/nginx/html;
    index index.html;
}

Here is my docker-compose.yml

version: '3.1'

services:

  letsencrypt-nginx-container:
    container_name: 'letsencrypt-nginx-container'
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
      - ./letsencrypt-site:/usr/share/nginx/html
    networks:
      - docker-network

networks:
  docker-network:
    driver: bridge


Here are all my droplet open doors:

root@docker:~# netstat -tulpn | grep LISTEN
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      740/systemd-resolve
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      951/sshd
tcp6       0      0 :::80                   :::*                    LISTEN      5412/docker-proxy
tcp6       0      0 :::22                   :::*                    LISTEN      951/sshd
tcp6       0      0 :::443                  :::*                    LISTEN      5400/docker-proxy

Is there any conf that I missing??

Appreciate any help as I lost half day on this.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi, @stgonzales

Before we deep dive into troubleshooting this, can you confirm that the domain name is pointed to your droplet’s IP address?

You can use our DNS lookup tool in order to check if the domain name is resolving from your droplet:

https://www.digitalocean.com/community/tools/dns

Let me know how it goes,

Regards, Alex

Heya,

here is an update on an older topic in case someone stumbles upon it

Based on the configuration and description, there are a few key points to consider:

  1. SSL Configuration for Nginx:

    • Your Nginx configuration listens on both ports 80 (HTTP) and 443 (HTTPS), but it doesn’t include the SSL certificate and key for handling HTTPS traffic.
    • If you want to serve your site over HTTPS (which seems to be the case since browsers are defaulting to HTTPS), you need to specify the SSL certificate and key in your Nginx configuration.
  2. Let’s Encrypt SSL Certificate:

    • If you don’t already have an SSL certificate, you can obtain one from Let’s Encrypt. You mentioned a container name letsencrypt-nginx-container, but it’s unclear if you’re actually using a tool like Certbot to obtain an SSL certificate.
    • If you have already obtained an SSL certificate from Let’s Encrypt, make sure it is correctly referenced in your Nginx configuration.
  3. Nginx Configuration:

    • You should separate your server blocks for HTTP and HTTPS.
    • The HTTP server block (port 80) should ideally only handle HTTP traffic and potentially redirect it to HTTPS.
    • The HTTPS server block (port 443) should handle SSL termination and serve your content over HTTPS.
  4. Docker Configuration:

    • Ensure that the SSL certificates (if obtained) are correctly mapped into your Docker container.

Here’s an example of how you might adjust your Nginx configuration:

server {
    listen 80;
    listen [::]:80;
    server_name iamgonzales.dev www.iamgonzales.dev;

    location ~ /.well-known/acme-challenge {
        allow all;
        root /usr/share/nginx/html;
    }

    # Redirect all HTTP traffic to HTTPS
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name iamgonzales.dev www.iamgonzales.dev;

    ssl_certificate /path/to/fullchain.pem; # Adjust with your SSL certificate path
    ssl_certificate_key /path/to/privkey.pem; # Adjust with your SSL key path

    root /usr/share/nginx/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

In your docker-compose.yml, ensure the SSL certificates are correctly mapped:

volumes:
  - ./nginx.conf:/etc/nginx/conf.d/default.conf
  - ./letsencrypt-site:/usr/share/nginx/html
  - /path/to/letsencrypt:/etc/letsencrypt # Map the directory containing your SSL certificates

After making these changes, restart your Docker container:

docker-compose down
docker-compose up -d

Remember:

  • The SSL certificate (fullchain.pem) and key (privkey.pem) paths must be correctly set in your Nginx configuration.
  • If you haven’t obtained an SSL certificate yet, you’ll need to do so. Tools like Certbot can automate this process. You may need a separate Docker container for managing Let’s Encrypt certificates or run Certbot on the host.
  • DNS should be properly configured for iamgonzales.dev and www.iamgonzales.dev to point to your server’s IP.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.