简体   繁体   English

如何使用 Google Cloud Compute Engine 为 Node.JS 应用程序配置端口转发

[英]How to configure Port Forwarding with Google Cloud Compute Engine for a Node.JS application

I'm trying to configure a port forwarding (port 80 to port 8080) for a Node.js application hosted on Google Cloud Compute Engine (Ubuntu and Nginx).我正在尝试为 Google Cloud Compute Engine(Ubuntu 和 Nginx)上托管的 Node.js 应用程序配置端口转发(端口 80 到端口 8080)。

My ultimate goal is to have an url like "api.domain.com" showing exactly the same thing from "api.domain.com:8080" (:8080 is working actually).我的最终目标是让像“api.domain.com”这样的 url 显示与“api.domain.com:8080”完全相同的东西(:8080 实际上正在工作)。

But because it's a virtual server on Google platform, I'm not sure what kind of configuration I can do.但是因为它是谷歌平台上的虚拟服务器,我不确定我可以做什么样的配置。

I tried these solutions without success (probably because it's a Google Cloud environment):我尝试了这些解决方案但没有成功(可能是因为它是 Google Cloud 环境):

  1. Forwarding port 80 to 8080 using NGINX 使用 NGINX 将端口 80 转发到 8080
  2. Best practices when running Node.js with port 80 (Ubuntu / Linode) 使用端口 80 运行 Node.js 的最佳实践(Ubuntu / Linode)

So two questions here:所以这里有两个问题:

1.Where I need to configure the port forwarding? 1.我需要在哪里配置端口转发?

  • Directly in my Ubuntu instance with Nginx or Linux config files?直接在我的 Ubuntu 实例中使用 Nginx 或 Linux 配置文件?
  • With gcloud command?使用 gcloud 命令?
  • In a secret place in the UI of console.cloud.google.com?在 console.cloud.google.com 的 UI 中的秘密位置?

2.What settings or configuration I need to save? 2.我需要保存哪些设置或配置?

One possibility is to use Google Cloud Load balancer.一种可能性是使用 Google Cloud Load balancer。 https://cloud.google.com/load-balancing/docs/ https://cloud.google.com/load-balancing/docs/

1) Create a backend service that listen on port 8080 1)创建一个监听8080端口的后端服务

2) Create a frontend service that listen on port 80 2)创建一个监听80端口的前端服务

3) Then forward frontend trafic on this backend service 3)然后在这个后端服务上转发前端流量

4) Bonus : You can create a ssl certificate auto managed by GCP https://cloud.google.com/load-balancing/docs/ssl-certificates#managed-certs 4)奖励:您可以创建由 GCP https://cloud.google.com/load-balancing/docs/ssl-certificates#managed-certs自动管理的 ssl 证书

For the benefit of future readers, here how I figured out how to configure the port forwarding.为了将来读者的利益,这里我是如何想出如何配置端口转发的。

  1. You will need to be sure that your Firewall on Google Platform is well configured.您需要确保 Google 平台上的防火墙配置良好。 Follow this process well described here: Google Cloud - Configuring Firewall Rules .遵循此处详细描述的此过程: Google Cloud - 配置防火墙规则 You will need to be sure that port 80 (or 443 for HTTPS) and your Node.JS port (eg 8080 in my case) are opened.您需要确保端口 80(或 HTTPS 的 443)和您的 Node.JS 端口(例如,在我的情况下为 8080)已打开。

  2. You will need to configure the port forwarding directly on the server.您需要直接在服务器上配置端口转发。 As far as I know, as opposed to the firewall rules, this is not a configuration that you can do in the Google Cloud platform UI.据我所知,与防火墙规则相反,这不是您可以在 Google Cloud 平台 UI 中进行的配置。 In my case, I need to edit the Nginx config file located in: /etc/nginx/sites-available/default .就我而言,我需要编辑位于/etc/nginx/sites-available/default 中的 Nginx 配置文件。

  3. Use this example for reference to edit your Nginx config file: nginx config for http/https proxy to localhost:3000使用此示例作为参考编辑您的 Nginx 配置文件: http/https 代理到 localhost:3000 的 nginx config

  4. Once edited, you need to restart your Nginx service with this command: sudo systemctl restart nginx编辑完成后,您需要使用以下命令重新启动 Nginx 服务: sudo systemctl restart nginx

  5. Verify the state of Nginx service with this command: sudo systemctl status nginx使用以下命令验证 Nginx 服务的状态: sudo systemctl status nginx

  6. Your port should be redirected correctly to your Node.js application.您的端口应该正确重定向到您的 Node.js 应用程序。

Thanks to @John Hanley and @howie for the orientation about Nginx configuration.感谢 @John Hanley 和 @howie 关于 Nginx 配置的介绍。

EDIT : This solution is still working but the accepted answer is easier.编辑:这个解决方案仍然有效,但接受的答案更容易。

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

相关问题 如何使用 Docker 映像将 Node.js HTTP/2 应用程序部署到 Google Compute Engine 上? - How do I deploy a Node.js HTTP/2 application onto Google Compute Engine using a Docker image? Google Compute Engine上的Node.js - Node.js on Google Compute Engine 如何在谷歌计算引擎上将 node.js 连接到 mysql? - How to connect node.js to mysql on google compute engine? 如何在Cpanel中为Node.js应用程序配置端口? - How to configure port for Node.js Application in Cpanel? Winston不会在Google Cloud Compute Engine上托管的Node.js应用程序的制作版本中记录事件 - Winston not logging events in productions version of a Node.js app hosted on Google Cloud Compute Engine 如何在 Google Cloud App Engine for Node.js 中使用 Memcache - How to use Memcache in Google Cloud App Engine for Node.js 从Google Compute Engine外部访问Node.js服务器 - Reaching Node.js server externally from Google Compute Engine 无法加载默认凭据? (Node.js 谷歌计算引擎) - Could not load the default credentials? (Node.js Google Compute Engine) 在Google Compute Engine Debian服务器上运行Node.js - Run Node.js on a Google Compute Engine Debian server 在Google Compute Engine上使用gcloud安装node.js - install node.js using gcloud on Google Compute Engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM