【问题标题】:Routing doesn't work after Dockerizing Angular appDockerizing Angular 应用程序后路由不起作用
【发布时间】:2018-12-18 17:54:36
【问题描述】:

我是 Angular 和 Docker 的新手。我开发了一个 Angular 7 应用程序并尝试对其进行 docker 化。我确实看过很多教程,并且能够构建 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", "daemon off;"]

我使用命令运行 docker 容器

docker run -d --name containerName -p 8082:80 imagename

我的容器确实启动了,在浏览器 http://IP-address:8082/ 中查看后,我可以看到我的应用程序的 index html。除此之外,我的应用程序的其他 URL(如登录页面 (http://IP-address:8082/login) 或仪表板 (http://IP-address:8082/dashboard) 都不起作用。我看到 404 页面未找到问题。还缺少什么来确保路由在我的 dockerized 容器中正常工作?

下面是我的 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。 任何帮助表示赞赏

【问题讨论】:

    标签: angular docker dockerfile angular-routing angular7


    【解决方案1】:

    发生这种情况是因为基础nginx 图像将尝试为来自docroot 的请求提供服务,因此当您向/login 发送请求时它会尝试查找login.html

    为避免这种情况,您需要 nginx 为 index.html 提供服务 无论请求如何,从而让 angular 处理路由。

    为此,您需要更改图像中当前的nginx.conf,以包含以下内容:

    try_files $uri $uri/ /index.html;

    而不是默认为:

    try_files $uri $uri/ =404;

    您可以通过多种方式这样做,但我想最好的方法是在您的Dockerfile 中添加一个额外的命令,像这样复制 nginx 配置:

    COPY nginx/default.conf /etc/nginx/conf.d/default.conf

    并用上面指定的命令替换目录中的 nginx/default.conf 文件(包含默认的 nginx 配置)。

    编辑

    已经有图片可以做到这一点,所以你可以只使用官方图片以外的图片,它是专门为此制作的,它应该可以正常工作:example

    【讨论】:

    • 我在 Dockerfile "FROM trion/nginx-angular:latest" 中添加了这个,虽然容器正在运行,但我现在也看不到我的 index.html。它说页面不工作。 http 502
    • 我刚刚查看了他的源代码,他将nginx调整为监听8080,因此它已更改,您可以使用-p 80:8080将其映射到本地主机上的80,或者只是像之前提到的那样自定义编辑
    • @abdullahkady 我已经用 default.conf 文件编辑了这个问题。我根本没有看到你提到的 try_files 部分。我到底应该在我的 default.conf 文件中更改哪里?
    • 只需将它添加到您的location 下作为第三行
    猜你喜欢
    • 2016-06-25
    • 2021-08-13
    • 2021-05-08
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2017-09-05
    • 2021-11-02
    相关资源
    最近更新 更多