How to redirect traffic from port 80 to 8080 - Load balancer - Cloud DNS

Viewed 73

I have a requirement similar to this post,

Google cloud load balancer port 80, to VM instances serving port 9000

I like one of the answers (not the accepted), but how to do it ? or is there an alternate way ?

" If your app is HTTP-based (looks like it), then please have a look at the new HTTP load balancing announced in June. It can take incoming traffic at port 80 and forward to a user-specified port (eg. port 9000) on the backend. The doc link for the command is here:

https://developers.google.com/compute/docs/load-balancing/http/backend-service#creating_a_backend_service"

I dont want to create statis IP after static IP and loose track

Scenario: A Compute Engine with an application running on port 8080 or 8043 (firewall open for 8080 and 8443 , has static IP )

Now I want to hook it a domain.

Problem:

I have to specify port number - like http://mywebsite:8080

Goal: Allow use like http://mywebsite

Please can I ask how Cloud DNS and Load Balancer work. Are both needed for my scenario? help me connect the dots. Thanks

Note: Application works on 8080 only (wont run on 80)

2 Answers

DNS knows nothing about port numbers (except for special record types). DNS is a hostname to IP address translation service.

You can either use a proxy/load-balancer to proxy port 80 to 8080 or configure your app to run on port 80.

Port 80 requires permission to use. For Linux this means configuring your application to run with system privileges or starting the application with sudo.

Most applications that run on non-standard ports have a web server in front of them such as Apache or Nginx. The web server proxies port 80 to 8080 and provides a more resilient Internet facing service.

I don't want to create static IP after static IP and lose track

Unfortunately, you will need to manage your services and their resources. If you deploy a load balancer, then you can usually use private IP addresses for the compute instances. Only the load balancer requires a public IP address. The load balancer will proxy port 80 to 8080.

However, assuming that your requirements are small, you can assign a public IP address to the instance, install Apache or Nginx, and run your application on port 8080.

Today, it is rare that Internet-facing web services do not support HTTPS (port 443). Using a load balancer simplifies configuring TLS and certificate management. You can also configure TLS in Apache/Nginx and Let's Encrypt. That removes the requirement that your app supports TLS directly on something like port 8443.

I found this article and it works - https://eladnava.com/binding-nodejs-port-80-using-nginx/

Steps: (sudo apt-get update)

sudo apt-get install nginx

remove default

sudo rm /etc/nginx/sites-enabled/default

create new - node folder

sudo nano /etc/nginx/sites-available/node

<<<<<< add this to the file update domain name , port of your app ...8080 or other>>>>>

server {
    listen 80;
    server_name somedomain.co.uk;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://127.0.0.1:8080";
    }
}

create a symbolic link:

sudo ln -s /etc/nginx/sites-available/node /etc/nginx/sites-enabled/node

Restart nginx

sudo service nginx restart

Credits to original author - Elad Nava

Related