docker nginx - 404 not found

Viewed 43

I followed these steps to setup nginx in docker at my server:

I create a nginx/ folder and put all the docker-compose.yml and conf.d/ with conf.d/default.conf accordingly.

docker-compose.yml:

version: "3.8"
services:
    web: 
        image: nginx:latest
        restart: always
        volumes:
            - ./public:/var/www/html
            - ./conf.d:/etc/nginx/conf.d
            - ./certbot/conf:/etc/nginx/ssl
            - ./certbot/data:/var/www/certbot
        ports:
            - 80:80
            - 443:443

    certbot:
        image: certbot/certbot:latest
        command: certonly --webroot --webroot-path=/var/www/certbot --email abc@xyz.com --agree-tos --no-eff-email -d example.com -d www.example.com
        volumes:
            - ./certbot/conf:/etc/letsencrypt
            - ./certbot/logs:/var/log/letsencrypt
            - ./certbot/data:/var/www/certbot

and my cond.f/default.conf:

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

    server_name example.com www.example.com;

    location ~ /.well-known/acme-challenge {
        allow all;
        root /var/www/certbot;
    }

    # redirect http to https www
    return 301 https://www.example.com$request_uri;
}

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name example.com;

    # SSL code
    ssl_certificate /etc/nginx/ssl/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/live/example.com/privkey.pem;

    root /var/www/html;

    location / {
        index index.html;
    }

    return 301 https://www.example.com$request_uri;
}

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name www.example.com;

    # SSL code
    ssl_certificate /etc/nginx/ssl/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/live/example.com/privkey.pem;

    root /var/www/html/example/public;

    location / {
        index index.html;
    }
}

I am sure the SSL is working because I can access https://example.com with no problem. But I always get 404 not found. I do have a public/ folder in nginx/ folder with index.html. But somehow I always get 404.

I am using Ubuntu 20.

How can I resolve this?

1 Answers

Ok, after @Amin's comments, I read carefully on my config file, and found out that with all the rerouting of the port 80 to port 443, the root folder actually change to /var/www/html/example/public. so I just have to change my docker-compose volume binding from:

volumes:
    - ./public:/var/www/html

to

volumes:
    - ./public:/var/www/html/example/public

Now it works.

Related