本文介绍了如何使用存储桶存储在 google flex/app 引擎环境中提供静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 nodejs 后端和一个 reactjs 前端.我正在使用 gcloud flex 环境(应用引擎)并希望使用 CDN 提供所有前端文件.我不希望请求触及我的 nodejs 服务器.我无法将我的项目 app.yaml 配置为执行相同操作.

I have a nodejs backend and a reactjs frontend. I am using the gcloud flex environment (app engine) and want to serve all the frontend files using a CDN. I would not want the requests to touch my nodejs server. I am unable to configure my projects app.yaml to do the same.

我怀疑我的请求没有从 CDN 提供服务,因为如果我在 nodejs 代码中注释以下行,我将无法再访问 index.html .

I suspect that my requests are not being served from a CDN because if I comment the below line in my nodejs code, I can no longer access index.html .

app.use('/', express.static(path.resolve('./frontend/dist')));

以下是 YAML 文件.

Below is the YAML file.

handlers:
- url: /(.*.html)
  mime_type: text/html
  static_files: frontend/dist/1
  upload: frontend/dist/(.*.html)

- url: /styles/(.*.css)
  mime_type: text/css
  static_files: frontend/dist/styles/1
  upload: frontend/dist/styles/(.*.css)

- url: /scripts/(.*.js)
  mime_type: text/javascript
  static_files: frontend/dist/scripts/1
  upload: frontend/dist/scripts/(.*.js)

- url: /images/(.*.(bmp|gif|ico|jpeg|jpg|png))
  static_files: frontend/dist/images/1
  upload: frontend/dist/images/(.*.(bmp|gif|ico|jpeg|jpg|png))

- url: /
  static_files: frontend/dist/index.html
  upload: frontend/dist/index.html

-  url: /.*
  script: IGNORED
  secure: always

有没有办法配置应用引擎,使静态文件请求不会对我的 nodejs 后端服务器做出反应?

Is there a way to configure app engine such that the static file requests don't react my nodejs backend servers?

谢谢

推荐答案

你搞混了 标准 GAE env app.yaml 元素(静态内容配置)到您的 flex env app app.yaml.

You're mixing up standard GAE env app.yaml elements (the static content config) into your flex env app app.yaml.

在 flex 环境中提供静态内容是不同的.

Serving the static content is different in the flex environment.

基于 express.static 的静态文件提供方法实际上对应于 从您的应用程序提供服务:

Your express.static-based method for serving static files actually corresponds to Serving from your application:

从您的应用程序提供服务

大多数网络框架都支持提供静态文件.在这示例,应用程序使用 express.static 中间件来将 ./public 目录中的文件提供给 /static URL.

Most web frameworks include support for serving static files. In this sample, the application uses the express.static middleware to serve files from the ./public directory to the /static URL.

要在没有请求影响您的应用程序的情况下提供静态内容,您需要遵循 从云存储服务:

To serve static content without the requests hitting your app you need to follow the Serving from Cloud Storage:

从 Cloud Storage 存储分区提供静态文件的示例

这个简单的示例创建了一个 Cloud Storage 存储分区并上传静态使用 Cloud SDK 的资产:

This simple example creates a Cloud Storage bucket and uploads static assets using the Cloud SDK:

  1. 创建一个存储桶.以您的项目 ID 命名您的存储桶是很常见的,但不是必需的.存储桶名称必须全局唯一.

  1. Create a bucket. It's common, but not required, to name your bucket after your project ID. The bucket name must be globally unique.

gsutil mb gs://<your-bucket-name>

  • 设置 ACL 以授予对存储桶中项目的读取访问权限.

  • Set the ACL to grant read access to items in the bucket.

    gsutil defacl set public-read gs://<your-bucket-name>
    

  • 将项目上传到存储桶.rsync 命令通常是上传和更新资产的最快和最简单的方法.你也可以使用cp.

  • Upload items to the bucket. The rsync command is typically the fastest and easiest way to upload and update assets. You could also use cp.

    gsutil -m rsync -r ./static gs://<your-bucket-name>/static
    

  • 您现在可以通过以下方式访问您的静态资产https://storage.googleapis.com//static/....

    You can now access your static assets via https://storage.googleapis.com/<your-bucket-name>/static/....

    有关如何使用 Cloud Storage 提供静态资产的更多详细信息,包括如何从自定义域名提供服务,请参阅 How to托管静态网站.

    For more details on how to use Cloud Storage to serve static assets, including how to serve from a custom domain name, refer to How to Host a Static Website.

    有关如何使用 Cloud Storage API 的更多信息从您的内部动态上传、下载和操作文件应用程序,请参阅使用云存储.

    For more information on how to use the Cloud Storage API to dynamically upload, download, and manipulate files from within your application, see Using Cloud Storage.

    这篇关于如何使用存储桶存储在 google flex/app 引擎环境中提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-01 22:00