【问题标题】:how to fix 404 not found error nginx on docker?如何在 docker 上修复 404 not found 错误 nginx?
【发布时间】:2022-04-21 23:13:03
【问题描述】:

我正在尝试让 docker 文件运行以响应 app.react 应用程序都已准备好成功部署,但页面重新加载出现错误 404 nginx 错误。

这是我的 docker 文件

FROM nginx:1.19.4-alpine
RUN rm -rf /usr/share/nginx/html/*
COPY build-qa /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

【问题讨论】:

  • 取决于你的应用,但你很可能需要一个 nginx.conf。
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: reactjs docker nginx http-status-code-404


【解决方案1】:

可能是 nginx 的 React Router DOM 问题。可能会检查您是否有 nginx.conf(本地)

# nginx.conf
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
  worker_connections  1024;
}
http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  /var/log/nginx/access.log  main;
  sendfile        on;
  #tcp_nopush     on;
  
  keepalive_timeout  65;
  #gzip  on;
  #include /etc/nginx/conf.d/*.conf;
server {
  listen 80;
  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
}
}

然后尝试构建映像并运行。示例 Dockerfile 内容:

FROM nginx
# FROM node:8 as react-build

WORKDIR /usr/share/react

RUN curl -fsSL https://deb.nodesource.com/setup_17.x | bash -
RUN apt-get install -y nodejs

COPY package*.json ./
RUN npm install

COPY . .

RUN npm run build

RUN rm -r /usr/share/nginx/html/*

RUN cp -a build/. /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf

# Build Image
# docker build . -t <your-app-tag>:latest
# Run Container
# docker run -it --rm -d -p 8080:80/tcp --name <your-app-name> <your-app-tag>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 2013-08-15
    • 2015-10-13
    • 2020-02-04
    相关资源
    最近更新 更多