What I try to do is very simple, I am using AWS application load balancer, and I want to redirect all my http requests to https: I have the following setup: An application load balancer (ALB1) which has two listeners:
HTTP:80
HTTP:443
Both rules are forwarding to a target group (TG1) in which 1 target instance is registered on both port 80 and 443, and both healthy.
and my nginx conf has the following setup:
server {
listen 80;
server_name {{server_name}};
access_log /var/log/nginx/http_redirect.log;
return 301 https://$server_name$request_uri;
}
server {
server_name {{server_name}};
listen 443 ssl default_server;
...
...
}
However, when I go to http://server_name/ping, I expect it to be redirected to https://server_name/ping, but I just got a 502 Bad Gateway
, and I checked the log /var/log/nginx/http_redirect.log
, it shows something like:
172.xx.xx.xx - - [13/Oct/2017:22:31:13 -0700] "\x16\x03\x01\x00\xA5\x01\x00\x00\xA1\x03\x03|\xFE\xC1\x88\x9E\xC88\xF8\xCDLn\xBAV,\xCE\xAF\xAA\xF2\x9Axv\x16\xD0\xC2\xE6\xFBE\x95oi%\x01\x00\x00(\xC0+\xC0/\xC0#\xC0'\xC0\x09\xC0\x13\xC0,\xC00\xC0$\xC0(\xC0\x14\xC0" 400 181 "-" "-"
another strange thing is if I refresh the page again, it gives the result without being redirected, and then refresh again, I will get 502 Bbad Gateway
again, basically, I will get 502 Bad Gateway
and result without redirect alternately. FYI, there is nothing in my nginx error log.
More observation: when I get correct response without redirecting, $remote_addr shows the IP of my vpn, and when I get 502, $remote_addr show another different IP.
Could anyone help? Thanks