Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
容器化的ReactJs应用程序(来自nginx映像)并不服务于所有路由_Reactjs_Docker_Nginx_Docker Compose_Dockerfile - Fatal编程技术网

容器化的ReactJs应用程序(来自nginx映像)并不服务于所有路由

容器化的ReactJs应用程序(来自nginx映像)并不服务于所有路由,reactjs,docker,nginx,docker-compose,dockerfile,Reactjs,Docker,Nginx,Docker Compose,Dockerfile,我正在尝试在GKE上部署ReactJS服务。我使用Nginx构建我的react映像(我使用createreact应用程序和react路由器) 这是我的文件 FROM node:8.9.3-alpine as build-deps2 RUN mkdir /irooltest WORKDIR /irooltest COPY . /irooltest RUN npm install RUN npm run build FROM nginx:1.12-alpine COPY --from=build

我正在尝试在GKE上部署ReactJS服务。我使用Nginx构建我的react映像(我使用createreact应用程序和react路由器)

这是我的文件

FROM node:8.9.3-alpine as build-deps2
RUN mkdir /irooltest WORKDIR /irooltest COPY . /irooltest
RUN npm install RUN npm run build

FROM nginx:1.12-alpine 
COPY --from=build-deps2 /irooltest/build /usr/share/nginx/html
EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
这是我的docker撰写文件:

services:
  irooltest:
     image: chaimaennar/irooltest
     ports:
      - 8080:80
     networks:
            default:

networks:
   default:
      external:
        name: mynetwork
我尝试将我的应用程序导出到80以外的端口,但无法导出(我知道这是由于nginx,它是我默认配置为在端口80上提供服务的)

另一个问题是,当运行我的应用程序时,它只显示根页面,任何其他(我用react router定义的路由)显示:

404未找到nginx/1.12.2

当我看到来源(在浏览器的“检查”部分中)时,我看到:

-Top
  -localhost:8080
    -static
    -index.css
  -Webpack://
在网页下,我可以看到我所有的文件夹(代码)

以下是我的路线:

}/>
} /> 
} /> 
{
手工认证(道具);
return}}/>
(
)
} /> 
} />
我的问题是:

这是正常的行为吗


nginx是否应该为路由服务进行特殊配置?

事实证明,使用
react router
nginx
需要进行特定配置,因为
nginx
无法真正识别我在应用程序代码中使用react router指定的路由

1。将这些行添加到
Dockerfile
中,告诉docker不要使用
conf.d
default
nginx
配置。

RUN rm -rf /etc/nginx/conf.d
COPY conf /etc/nginx
2。添加文件夹
conf
conf.d
和文件
default.conf

 conf
   |
    conf.d
    default.conf
3。在
default.conf
文件中添加这些行

server {
 listen 80;
  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}
他们告诉nginx提供
index.html
文件

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