Angular universal with docker giving bad gateway

huangapple go评论74阅读模式
英文:

Angular universal with docker giving bad gateway

问题

我正在尝试使用nginx将Angular Universal应用程序docker化,但遇到了很大的问题。
它不停地报502坏网关错误,我无法理解。

这是我的Dockerfile

FROM node:14-alpine AS builder
WORKDIR /app
COPY . .
RUN npm i && npm run build:ssr
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
RUN rm -rf ./*
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/dist/front-end-kevi .
ENTRYPOINT ["nginx", "-g", "daemon off;"]

在我的server.ts文件中,我在端口4000上启动应用程序。
所以我使用以下命令启动docker映像

docker run -d -p 4000:80 kevin

供参考,这是我的nginx配置文件。

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    location ~* \.(eot|ttf|woff|woff2)$ {
        add_header Access-Control-Allow-Origin *;
    }

    location / {
        proxy_pass http://localhost:4000;
        try_files $uri $uri/ =404;
    }

    location /OrderMationApi/api/v3/ {
        proxy_pass http://102.133.225.222;
    }
}

我在错误日志中看到以下错误

7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4000/", host: "localhost:4000"

有任何我做错的地方吗?
非常感谢任何帮助。提前致谢。

英文:

I am trying to dockerizing a angular universal app using nginx, and it's giving me really hard time.
It keeps saying 502 bad gateway and I am unable to wrap my head around it.

This is my Dockerfile

FROM node:14-alpine AS builder
WORKDIR /app
COPY . .
RUN npm i && npm run build:ssr
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
RUN rm -rf ./*
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/dist/front-end-kevi .
ENTRYPOINT ["nginx", "-g", "daemon off;"]

In my server.ts file, I am starting the app on port 4000.
So I am starting the docker image using this command

> docker run -d -p 4000:80 kevin

for the reference, here is my nginx conf file.

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    location ~* \.(eot|ttf|woff|woff2)$ {
        add_header Access-Control-Allow-Origin *;
    }

    location / {
        proxy_pass http://localhost:4000;
        try_files $uri $uri/ =404;
    }

    location /OrderMationApi/api/v3/ {
        proxy_pass http://102.133.225.222;
    }
}

and I am getting this error in error logs

> 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4000/", host: "localhost:4000"

Any idea where I am doing wrong?
Any help is highly appreciated.
Thanks in advance.

答案1

得分: 2

你说你要在端口:4000上启动你的服务器,但我在你的Dockerfile中没有看到这一点。根据你在问题中发布的配置,我觉得你认为你的容器将同时运行Nginx和你的应用程序。但实际情况是只有Nginx会运行,你的应用程序不会启动。这就是为什么你会得到一个错误消息 - Nginx 找不到 http://localhost:4000 上的任何内容的原因。

要解决这个问题,你有几个选项。一个选项是你可以构建另一个容器来运行你的应用程序,然后将Nginx指向该容器。最好的方法是使用 docker compose 同时运行它们,并将Nginx指向 http://<service-name>:4000,其中 <service_name> 是你另一个容器的名称。

另一种可能性是替换你的入口点(entrypoint)为一个启动它们两者的脚本。这不是容器的最佳实践,但是是可能的。在这种情况下,你可以编写一个脚本(例如 /docker-entrypoint.sh),内容如下:

#!/bin/sh

nginx &
npx ts-node server.ts

这会将Nginx启动到后台,并在前台运行你的服务器。再次强调,这不是最佳实践,因为现在Nginx不会将日志记录到stdout/stderr。因此,我建议使用类似 docker compose 的多容器设置。

英文:

You say that you're starting your server on :4000, but I'm not seeing that in your Dockerfile. From the configuration you have posted in your question, it seems to me that you believe your container will run both Nginx and your application. But what will happen is only Nginx will run, and your app will not be started. That's why you are getting an error - Nginx is not able to find anything on http://localhost:4000 because nothing is listening there.

To address this, you have a couple of options. One is you could build another container, to run your application, and then point Nginx to that container. The best way to do that is to run both of them with docker compose, and point Nginx to http://&lt;service-name&gt;:4000, where &lt;service_name&gt; is the name of your other container.

Another possibility is to replace your entrypoint with a script that launches both of them. This is not a container best practice, but it's possible. In that case, you would write a script (e.g. /docker-entrypoint.sh) with contents such as:

#!/bin/sh

nginx &amp;
npx ts-node server.ts

This would launch nginx into the background, and run your server in the foreground. Again, this is not a best practice, because now Nginx is not logging to stdout/stderr. So I suggest instead doing a multi-container setup with something like docker compose.

答案2

得分: -1

问题基本上出在nginx配置上。这个问题已经在这里提出过了。https://serverfault.com/questions/317393/connect-failed-111-connection-refused-while-connecting-to-upstream

英文:

The problem is basically with the nginx configuration. This question is already asked here. https://serverfault.com/questions/317393/connect-failed-111-connection-refused-while-connecting-to-upstream

huangapple
  • 本文由 发表于 2023年5月29日 22:22:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定