skip to Main Content

I have a Next.js static export site that I deployed with Docker and Nginx. I am using Cloudflare for DNS and SSL. I can access my site using https://mansourlotfi.ir, but when I try to access https://www.mansourlotfi.ir, I get a "404 page not found".

Dockerfile

FROM hub.hamdocker.ir/nginx:1.22.0
COPY ./out ./project
RUN rm ../etc/nginx/conf.d/default.conf
COPY ./nginx-configs ../etc/nginx/conf.d

settings.conf inside nginx-configs folder

server
{
    listen 80;
    server_name www.mansourlotfi.ir;
    return 301 https://mansourlotfi.ir$request_uri;
}

server
{
    listen 80;
    server_name c13.hamravesh.hamserver.ir;
    return 301 https://mansourlotfi.ir$request_uri;
}

server
{
    listen 80;
    server_name mansourlotfi.ir;
    root /project;

    location /
    {
        index index.html;
        try_files $uri $uri/ /index.html =404;
    }

    error_page 404 /404.html;
    location = /404.html
    {
        internal;
    }

}

cloudflare DNS config

enter image description here

Note: I have already added CNAME records for both example.com and www.example.com in Cloudflare, pointing to the same IP address of my server.

also when I visit a non-existing route such as example.com/asd, instead of showing the Next.js 404 page, it shows me the homepage.
I have read all the topics related to this issue in the community, but unfortunately, I was unsuccessful. Thank you for your help

2

Answers


  1. Chosen as BEST ANSWER

    All settings for the www version in Dockerfile, Cloudflare, and nginx were correct. Another thing was missing: I had to add my domain with www to the host settings that run my containerized app. enter image description here

    this solved my problem and now i can see my site with www.


  2. It seems like you have not included server block properly

    here you can change your setting.conf

       server {
            listen 80;
            server_name www.mansourlotfi.ir c13.hamravesh.hamserver.ir;
            return 301 https://mansourlotfi.ir$request_uri;
        }
        
        server {
            listen 80;
            server_name mansourlotfi.ir;
            root /project;
        
            location / {
                index index.html;
                try_files $uri $uri/ /index.html =404;
            }
        
            error_page 404 /404.html;
            location = /404.html {
                internal;
            }
        }
    

    Make sure you settings.conf file inside the nginx-configs folder and rebuild and redeploy your Docker container to apply the changes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search