2

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).

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).

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):

  1. Forwarding port 80 to 8080 using NGINX
  2. Best practices when running Node.js with port 80 (Ubuntu / Linode)

So two questions here:

1.Where I need to configure the port forwarding?

  • Directly in my Ubuntu instance with Nginx or Linux config files?
  • With gcloud command?
  • In a secret place in the UI of console.cloud.google.com?

2.What settings or configuration I need to save?

4

2 回答 2

4

One possibility is to use Google Cloud Load balancer. https://cloud.google.com/load-balancing/docs/

1) Create a backend service that listen on port 8080

2) Create a frontend service that listen on port 80

3) Then forward frontend trafic on this backend service

4) Bonus : You can create a ssl certificate auto managed by GCP https://cloud.google.com/load-balancing/docs/ssl-certificates#managed-certs

于 2019-02-20T08:43:31.277 回答
1

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. Follow this process well described here: Google Cloud - Configuring Firewall Rules. You will need to be sure that port 80 (or 443 for HTTPS) and your Node.JS port (e.g 8080 in my case) are opened.

  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. In my case, I need to edit the Nginx config file located in: /etc/nginx/sites-available/default.

  3. Use this example for reference to edit your Nginx config file: nginx config for http/https proxy to localhost:3000

  4. Once edited, you need to restart your Nginx service with this command: sudo systemctl restart nginx

  5. Verify the state of Nginx service with this command: sudo systemctl status nginx

  6. Your port should be redirected correctly to your Node.js application.

Thanks to @John Hanley and @howie for the orientation about Nginx configuration.

EDIT: This solution is still working but the accepted answer is easier.

于 2019-02-20T14:03:58.350 回答