简体   繁体   English

Dockerizing Angular应用程序后路由不起作用

[英]Routing doesn't work after Dockerizing Angular app

Am new to Angular and Docker. 是Angular和Docker的新手。 I have developed an Angular 7 application and trying to dockerize it. 我开发了一个Angular 7应用程序并试图将它停靠。 I did see many tutorials and was able to build the Docker image. 我确实看到了很多教程,并且能够构建Docker镜像。 Below is my Dockerfile 下面是我的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;"]

And i run the docker container using command 然后我使用命令运行docker容器

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

My container did start and after looking in the browser http://IP-address:8082/ I could see index html of my app. 我的容器确实启动了,在浏览器中查看http:// IP-address:8082 /我可以看到我的应用程序的索引html。 Apart from that none other url of my application like the login page ( http://IP-address:8082/login ) or dashboard ( http://IP-address:8082/dashboard ) works. 除了我的应用程序的其他网址,如登录页面( http:// IP地址:8082 /登录 )或仪表板( http:// IP地址:8082 /仪表板 )工作。 I see 404 page not found issue. 我看到404页面未找到问题。 What else is missing to make sure routing works in my dockerized container? 还有什么可以确保路由在我的dockerized容器中工作?

Below is my default.conf file 下面是我的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;
    #}
}

And my docker version is Docker version 17.03.2-ce. 我的docker版本是Docker版本17.03.2-ce。 Any help appreciated 任何帮助赞赏

This is happening because the base nginx image will try to serve the requests from the docroot , so it will try to find a login.html when you send a request to /login . 发生这种情况是因为基本nginx映像将尝试提供来自docroot的请求,因此当您向/login发送请求时,它将尝试查找login.html

To avoid this, you need nginx to serve the index.html no matter the request , and thus letting angular take care of the routes. 为了避免这种情况, 无论请求什么 ,都需要nginx来提供index.html ,从而让angular处理路由。

To do so, you will need to change your nginx.conf , which is currently in the image, to include the following : 为此,您需要更改当前在图像中的nginx.conf ,以包含以下内容:

try_files $uri $uri/ /index.html;

Instead of the default being: 而不是默认值:

try_files $uri $uri/ =404;

You can do so in many ways, but I guess the best approach is to have an extra command in your Dockerfile , that copies over the nginx configuration like so : 您可以通过多种方式实现这一目标,但我想最好的方法是在Dockerfile一个额外的命令,通过nginx配置进行复制,如下所示:

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

And have that nginx/default.conf file in your directory (containing the default nginx configuration) with the command specified above replaced. 并将目录中的nginx/default.conf文件(包含默认的nginx配置)替换为上面指定的命令。

EDIT 编辑

There is already images that does exactly that, so you can just use an image other than the official one, that is made specifically for that, and it should work fine: example 已经存在完全相同的图像,因此您可以使用除官方图像之外的图像,这是专门为此而制作的图像,它应该可以正常工作: 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM