How to build a web server
Menu Starting this time, I will try to create a WebAPI. In line with the concept of this site, I will focus on the thought process rather …
I guess you can use AWS ALB (Application Load Balancer) to force HTTPS or redirect with/without www. This is not particularly difficult to do and there is plenty of information about it out there, but the ALB is a bit cheaper and is usually the one to use.
However, there are some cases where ALB cannot be used: when the NS (Name Server) cannot be set to Route53. If you set up DNS other than Route53, you have no choice but to use CNAME for routing to ALB. But, for example, if you set up a web server with the domain aaa.com and www.aaa.comで運用したい場合 (rewrite from no www to www), you can’t set CNAME for “aaa.com”. That’s why I can’t point my domain (aaa.com) to ALB.
In such cases, NLB (Network Load Balancer) can be used to achieve redirection. Since there is not much information on this NLB, I will introduce it here. For your information, this time I will introduce the case of redirecting to “SSL communication with www”.
In the NLB, you can set a static IP address for the load balancer. Therefore, you can set up DNS with or without www with A record and point it to the NLB. Moreover, the AWS ACM allows you to set up SSL certificates for easy https communication. Here is an image.
And since the web server side receives it on port 80, Nginx should redirect HTTP communication to HTTPS communication, not load balancer. If you Google it, you can often find the following conf settings.
if ($http_x_forwarded_proto != https){
return 301 https://$host$request_uri;
}
But that’s the way it’s written for ALB, and it doesn’t work for NLB!
I did some research and found the following solution. The same method can be used for Apache.
server {
listen 8080;
server_name _;
root /usr/share/nginx/html;
if ($http_user_agent !~* ELB-HealthChecker){
return 301 https://$host$request_uri;
}
}
This will allow HTTP (80) communication to the NLB to flow to port 8080 of Nginx and force a redirect to 443. At this time, we can return 200 only during the health check of the LB.
With nginx, you can do the following as usual.
server {
listen 80;
server_name aaa.com;
return 301 $scheme://www.aaa.com$request_uri;
}
A case like this would be, for example, when a customer acquires a domain name from Name.com, and since the MX record for the email address has already been set up, the customer does not want to transfer the name server to Route53. There is also a case where an uncle who dislikes AWS is in charge, and he claims that he doesn’t want to use AWS at least for NS.
Introducing the case of using NLB when ALB+Route53 can do it easily.
Menu Starting this time, I will try to create a WebAPI. In line with the concept of this site, I will focus on the thought process rather …
Menu It is easy to visualize the implementation of visible front-end development and its UI processing, such as web front-end …