Cloud Run 是否需要 NGINX?

lim*_*jix 9 google-cloud-platform google-cloud-run

我正在为我的博客和工作网站使用 cloud run,我真的很喜欢它。我已经根据 google 教程通过容器化部署了 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" ]
Run Code Online (Sandbox Code Playgroud)
# 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
Run Code Online (Sandbox Code Playgroud)

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

pet*_*ina 2

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

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

关于Python,除非你能以某种方式捆绑它,否则看起来不需要NGINX就可以使用。

  • 简而言之,Cloud Run 需要运行 Web 服务器的容器映像。您可以编写自己的服务器(用 Go、Java、Python、Node.js ...)来为您的博客提供服务,或者在您的应用程序前面运行 nginx、Apache、HAProxy、Envoy 或其他任何内容 - 这部分取决于您。也就是说,并非所有语言都提供开箱即用的 HTTP 服务器(例如 PHP)。 (2认同)