【问题标题】:Cloud Run needs NGINX or not?Cloud Run 是否需要 NGINX?
【发布时间】:2020-08-30 19:02:40
【问题描述】:

我正在为我的博客和工作网站使用云运行,我真的很喜欢它。 我根据谷歌教程通过容器化部署了 python API 和 Vue/Nuxt 应用程序。 我不明白的一件事是为什么不需要在前面放置 NGINX。

# Use the official lightweight Node.js 12 image.
# https://hub.docker.com/_/node
FROM node:12-slim

# Create and change to the app directory.
WORKDIR /usr/src/app

# Copy application dependency manifests to the container image.
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
# Copying this separately prevents re-running npm install on every code change.
COPY package*.json ./

# Install production dependencies.
RUN npm install --only=production

# Copy local code to the container image.
COPY . ./

# Run the web service on container startup.
RUN npm run build
CMD [ "npm", "start" ]
# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.7-slim

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

# Install production dependencies.
RUN apt-get update && apt-get install -y \
libpq-dev \
gcc
RUN pip install -r requirements.txt

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn -b :$PORT --workers=4 main:server

这一切都无需我调用 Nginx。 但是我读了很多文章,人们将 NGINX 捆绑在他们的容器中。所以我想澄清一下。我正在做的事情有什么缺点吗?

【问题讨论】:

    标签: google-cloud-platform google-cloud-run


    【解决方案1】:

    使用 NGINX 或静态文件服务器的一个显着优势是容器映像的大小。在提供 SPA(无 SSR)时,您只需将捆绑的文件发送给客户端。无需捆绑编译应用程序所需的构建依赖项或运行时。

    您的第一个映像是将具有依赖关系的整个源代码复制到映像中,而您需要的只是编译文件(如果不运行 SSR)。 NGINX 可以为您提供“静态站点服务器”,它只会为您的构建服务,并且是一个轻量级的解决方案。

    关于python,除非你能以某种方式捆绑它,否则在没有NGINX的情况下使用看起来没问题。

    【讨论】:

    • 啊,非常感谢!我一直在努力寻找一个好的答案。所以本质上对于 nodejs 案例,如果它是一个 SPA,我可以只包含 nginx 来提供静态文件。但是,如果它是 SSR,我不需要它吗?此外,firebase 托管比 NGINX Container for SPA 应用程序更有优势,对吧?
    • 关于 Firebase,我建议你使用它,它对于 SPA 来说比在 Cloud Run 上运行要简单得多。现在 SSR,您可能需要带有已编译包的运行时,但这是特定于框架的。查看有关如何为 SSR 提供​​最小捆绑包的 Vue 文档 :)
    • 简而言之,Cloud Run 需要运行 Web 服务器的容器映像。您可以编写自己的服务器(使用 Go、Java、Python、Node.js ......)来为您的博客提供服务,或者在您的应用程序前面运行 nginx、Apache、HAProxy、Envoy 或其他任何东西——这部分取决于您。也就是说,并非所有语言都提供开箱即用的 HTTP 服务器(例如 PHP)。
    • @AhmetB-Google 我明白这一点,但我很好奇,Cloud Run 前面有代理吗,这就是为什么如果我只运行 gunicorn 就足够了,因为 cloud run 上有类似 Nginx 的东西前面?
    • 不用担心。即使在 GCE VM 中,gunicorn 也可以直接充当 HTTP 服务器,只要绑定到 80 端口即可,不需要 nginx。
    猜你喜欢
    • 2020-03-12
    • 2021-12-22
    • 2020-05-31
    • 2018-12-09
    • 2015-04-06
    • 2017-02-28
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    相关资源
    最近更新 更多