Configuring HTTPS and Subdomain on GCP and configure NGINX HTTP redirection to HTTPS
Introduction
Google Cloud Platform (GCP), offered by Google, is a suite of cloud computing services that runs on the same infrastructure that Google uses internally for its end-user products
Configure GCP
So on GCP, I have 1 VM that contain 2 applications,
- Web UI application, using nginx that expose port 80
- API application, using nginx that expose port 8085
Then I have 1 domain name, e.g : hendisuhardja.com. Now I want to configure the VM to use the domain. Let’s say Web UI application will have domain hendisuhardja.com and API application will use subdomain api.hendisuhardja.com. We could do it easily on GCP and we could use Google Certificate to set HTTPS too
Below are the steps :
Set Web UI Load Balancing
- Open GCP (https://console.cloud.google.com/) then login
- Select the project, then open Navigation Menu on Top Left of the page, navigate to Network Services -> Load Balancing
- Then on Load balancing page, click “Create Load Balancer”
- Choose HTTP(S) Load Balancing
- Choose From Internet to my VMs
- On Backend configuration section, choose Backend services -> Create a backend service
- Set Protocol as “HTTP”, Named port “http-ui” (Note : please make sure you use different named port for each load balancing, because if you use same name, it will overwrite the others), choose your instance group, choose your application exposed port, in my case it is 80
- On Host and path rules, configure as below
- On Frontend configuration section, Set Protocol as “HTTPS” and IP version to “IPv4” then on IP address choose “Create IP address” (Ephemeral IP Address is not static, so don’t use this because you need to map static IP address to domain)
- On “Select a certificate, choose “Create a new certificate”
- Give name of your certificate, on create mode choose “Create Google-managed certificate” , for domains, fill with your domain name, in my case “hendisuhardja.com”. Then click Create
- After you make load balancing, navigate to Network services -> Cloud DNS, choose Zone type “Public”, DNS Name fill with your domain name, in my case “hendisuhardja.com”, then click Create
- Make sure you have Type NS and A. On Resource Record Type “A”, fill the IP Address
Set API Load Balancing
Steps are similar when you create Web UI Load Balancing, the different are below
- On Backend configuration, set Named port to different name with Web UI Named port. e.g : http-api, then set port number to your API exposed port number, e.g : 8085
- On Create certificate, on domains field, fill with sub domain “api.hendisuhardja.com”.
- Then on Cloud DNS, create new record set, fill DNS Name with “api”. Then Resource Record Type “A”, then set your IP Address with IP Address from API Load Balancing
NGINX Configuration for HTTP Redirection to HTTPS
For NGINX file, you could use below configuration to redirect http request to https, e.g : when user navigate to http://hendisuhardja.com, it will redirect to https://hendisuhardja.com with below configuration
http {
server {
listen 80; if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}
location / {
try_files $uri $uri/ /index.html;
}
}
}
Thanks for reading