无法在docker容器中运行的nginx的子目录下部署react应用程序



我无法在docker容器中运行的nginx上的子目录下部署我的react应用程序。

以下是的详细信息

  1. React应用程序basename设置BrowserRouter basename='/myreactapp'
  2. Package.json有"主页":"/myreactapp">
  3. nginx.conf

    server {
    server_name localhost;
    index index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /usr/share/nginx/html;
    location /myreactapp {
    try_files $uri $uri/ /myreactapp/index.html;
    }
    }
    
  4. Docker文件

    FROM nginx:1.14.1
    COPY /build /usr/share/nginx/html
    COPY nginx.conf /etc/nginx/conf.d/default.conf
    EXPOSE 80
    CMD ["nginx", "-g", "daemon off;"]
    
  5. 这样,它仍然从"/"根位置为应用程序提供服务器,但不是/myreactapp

在Dockerfile中,替换此行:

COPY /build /usr/share/nginx/html

这个:

COPY /build /usr/share/nginx/html/myreactapp

对我来说,这两篇文章是

https://medium.com/@svinkle/如何部署-实际应用程序-分区-f694d46427c1

https://medium.com/@darshanpawar/enginx-configuration-changes-fter-you-deploy-react-application-in-a-sub-directory-f5b36453fed1

我的nginx.conf

server {
listen       80;
server_name  localhost;
location / {
root   /usr/share/nginx/html;
index  index.html index.htm;
}
location /myreactapp {
try_files $uri $uri/ /myreactapp/index.html;
}
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   /usr/share/nginx/html;
}
}

我的Dockerfile

# replace nginx.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
# copy files from build stage
COPY --from=build-stage /app/build /usr/share/nginx/html/myreactappenter

最新更新