3

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?

José CM
  • 41
  • 5
probitaille
  • 1,899
  • 1
  • 19
  • 37
  • 1
    You will need to use a proxy server. Common ones are Nginx or Apache. What problem did you have with Nginx? Another option is to use a Google load balancer. – John Hanley Feb 19 '19 at 21:13
  • I know that Google Cloud override some settings like firewall rules, making some Nginx settings useless. When I tried to edit my Nginx config for port forwarding, I don't see any changes at all. So, I don't even know if my settings are incorrect or if Google override some port rules. – probitaille Feb 19 '19 at 21:20
  • I cannot help you with Nginx unless you provide configuration files, error messages, etc. The solution is in the details. – John Hanley Feb 19 '19 at 22:37
  • 1
    According to your description "api.domain.com:8080" is working well , which means your vm firewall rule work fine. So I think the only thing you have to do is configuring your nginx proxy conf file. – howie Feb 20 '19 at 03:08

2 Answers2

6

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

Alexandre
  • 1,940
  • 1
  • 14
  • 21
  • This solution is easier! – probitaille Nov 12 '19 at 13:19
  • @Alexandre What type of load balancer do you create? Do you create managed instance group or can I use a single vm instance for this scenario? Also do you need a custom firewall rule or the default ones from GCP are enough? Thank you! – Kostadin Terziev Jul 29 '22 at 07:09
  • What I learned is that you should create http load balancer and assign named ports to your instance group. In the backend service of the load balancer you should assign the named port of the target instance group. Also you should create health checks for the instance group and for the load balancer. – Kostadin Terziev Jul 29 '22 at 12:08
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.

probitaille
  • 1,899
  • 1
  • 19
  • 37