You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Dockerfile修改Nginx默认首页后,运行容器仍显示默认页的问题

Troubleshooting: Nginx Showing Default Page Instead of Your Custom HTML

Let's break down the most likely issues and fixes here — this is a super common snag with Nginx Docker setups, so we'll get it sorted quickly!

1. Nginx Doesn't Know to Load welcome.html by Default

This is the #1 reason for this problem. The official Nginx image is configured to look for index.html (or index.htm) as the default landing page — it has no idea about your welcome.html file unless you explicitly tell it to prioritize it.

Fixes for this:

  • Simplest fix: Rename your file to index.html
    Just rename your local HTML file to index.html, then update your Dockerfile to copy it:

    FROM nginx:latest
    WORKDIR /usr/share/nginx/html
    COPY index.html index.html
    

    Rebuild your image (docker image build -t html_nginx .) and rerun the container — this should immediately load your custom page.

  • Customize Nginx's configuration to use welcome.html
    If you want to keep the filename as welcome.html, create a custom Nginx config file (let's call it default.conf) with the following content:

    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;
    
        location / {
            root   /usr/share/nginx/html;
            index  welcome.html index.html index.htm; # Add welcome.html as the first priority
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
    

    Then update your Dockerfile to copy this config into the Nginx config directory:

    FROM nginx:latest
    WORKDIR /usr/share/nginx/html
    COPY welcome.html welcome.html
    COPY default.conf /etc/nginx/conf.d/default.conf
    

    Rebuild and rerun the container — Nginx will now prioritize your welcome.html over the default index.html.

  • Temporary fix: Override Nginx's index at runtime
    If you don't want to rebuild the image, you can pass a custom Nginx command when starting the container:

    docker container run -p 80:80 --rm html_nginx nginx -g "daemon off; index welcome.html;"
    

2. Verify welcome.html Was Actually Copied to the Image

Sometimes the issue is that your welcome.html file wasn't copied into the image at all. Let's check:

  • Run a temporary container to inspect the image's contents:
    docker run --rm -it html_nginx /bin/bash
    
    Once inside, navigate to /usr/share/nginx/html and list the files with ls. If welcome.html isn't there:
    • Double-check that you're running docker build from the same directory where welcome.html is stored.
    • Make sure there's no typo in the filename (case sensitivity matters! e.g., Welcome.html vs welcome.html).
    • Check your build logs for errors like COPY failed: no source files were specified — this means Docker couldn't find the file.

3. (Less Likely) Port Conflict or Cached Image

  • Port conflict: If another service is using port 80 on your host, you might be hitting a different Nginx instance. Check with netstat -tulpn | grep 80 (Linux/macOS) or netstat -ano | findstr ":80" (Windows) to see what's listening on port 80.
  • Cached image: Docker might be using an old cached version of your image. Rebuild with the --no-cache flag to force a fresh build:
    docker image build --no-cache -t html_nginx .
    

内容的提问来源于stack exchange,提问作者Venkat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:17:59