Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect Http to Https in Elastic BeansTalk, Tomcat, Nginx

I have a spring boot application (1.5.3) which is running on a Tomcat container with Nginx as the load balancer. I have created below file:

.ebextentions/nginx/conf.d/myapp.conf

And I added the below content based on this article:

server {
  if ($http_x_forwarded_proto != 'https') {
    rewrite ^(.*) https://$host$1 redirect;
  }
}

I have installed SSL certificate in certificate manager and set it in my environment. Now the application serves both HTTP and https request. I want to redirect all HTTP requests to https without falling into a redirect loop. but doesn't work.

like image 500
Milad Avatar asked May 16 '26 13:05

Milad


1 Answers

I don't think you should add the server part to the .conf file. The entire .conf file should be something like this:

if ($http_x_forwarded_proto = 'http') {
    return 301 https://www.example.com$request_uri;
}

See this answer on Server Fault.

like image 61
Mark B Avatar answered May 19 '26 02:05

Mark B