布线不';停靠Angular app后无法工作
我对Angular和Docker是新手。我已经开发了一个Angular 7应用程序,并试图对其进行dockerize。我确实看过很多教程,并且能够构建Docker图像。 下面是我的Dockerfile布线不';停靠Angular app后无法工作,angular,docker,dockerfile,angular-routing,angular7,Angular,Docker,Dockerfile,Angular Routing,Angular7,我对Angular和Docker是新手。我已经开发了一个Angular 7应用程序,并试图对其进行dockerize。我确实看过很多教程,并且能够构建Docker图像。 下面是我的Dockerfile FROM nginx:1.13.3-alpine ## Remove default nginx website RUN rm -rf /usr/share/nginx/html/* COPY ./ /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g
FROM nginx:1.13.3-alpine
## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*
COPY ./ /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
我使用命令运行docker容器
docker run -d --name containerName -p 8082:80 imagename
我的容器确实启动了,在浏览器中查看后,我可以看到我的应用程序的索引html。除此之外,我的应用程序的其他url(如登录页面()或仪表板())都无法正常工作。我看到404页未找到问题。为了确保路由在停靠的容器中正常工作,还缺少什么
下面是我的default.conf文件
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
我的docker版本是docker版本17.03.2-ce。
这是因为基本的
nginx
映像将尝试为docroot
的请求提供服务,因此当您向/login
发送请求时,它将尝试查找login.html
为了避免这种情况,您需要nginx为index.html
提供服务,而不管请求如何,从而让angular
处理路由
为此,您需要更改图像中当前的nginx.conf
,以包括以下内容:
try_files$uri$uri//index.html代码>
默认值不是:
try_files$uri$uri/=404代码>
您可以通过多种方式执行此操作,但我想最好的方法是在Dockerfile
中有一个额外的命令,该命令通过nginx配置进行复制,如下所示:
复制nginx/default.conf/etc/nginx/conf.d/default.conf
并用上面指定的命令替换目录中的nginx/default.conf
文件(包含默认nginx配置)
编辑
已经有图像可以做到这一点,所以您可以使用官方图像以外的图像,这是专门为此制作的,应该可以正常工作:我在Dockerfile“FROM trion/nginx angular:latest”中添加了这一点,尽管容器正在运行,但我现在也看不到我的index.html。它说页面不工作。http 502I刚刚检查了他的源代码,他将nginx调整为在8080上侦听,因此它发生了更改,您可以使用-p 80:8080
将其映射到本地主机上的80,或者按照前面提到的编辑@abdullahkady进行自定义,我已使用default.conf文件编辑了问题。我一点也不明白你提到的try_文件部分。我应该在default.conf文件中的什么位置进行更改?只需将其添加到位置下的第三行即可