[英]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 环境):
So two questions here:所以这里有两个问题:
1.Where I need to configure the port forwarding? 1.我需要在哪里配置端口转发?
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.为了将来读者的利益,这里我是如何想出如何配置端口转发的。
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)已打开。
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 配置文件。
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
Once edited, you need to restart your Nginx service with this command: sudo systemctl restart nginx
编辑完成后,您需要使用以下命令重新启动 Nginx 服务:
sudo systemctl restart nginx
Verify the state of Nginx service with this command: sudo systemctl status nginx
使用以下命令验证 Nginx 服务的状态:
sudo systemctl status nginx
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.