1

My nginx configuration works great until I add a redirect that forces HTTPS. The working config before the redirect is is:

server {
  listen 80;
  listen 443 default_server ssl;
  server_name my-domain.com www.my-domain.com;
  client_max_body_size 5M;

  location / {
    proxy_pass http://localhost:3000;
  }
}

I'm running an app on port 3000 of the server, and I want to serve it up at my domain on port 80 and port 443 (http and https). However, I also want to redirect users to HTTPS when they try to access it over HTTP. I used this answer to set that up:

server {
  listen 80;
  server_name my-domain.com www.my-domain.com;
  client_max_body_size 5M;
  return 301 https://$server_name$request_uri;
}

server {
  listen 443 default_server ssl;
  server_name my-domain.com www.my-domain.com;
  client_max_body_size 5M;

  location / {
    proxy_pass http://localhost:3000;
  }
}

but this causes a redirect loop. I admit that I'm relatievly new to nginx, so if anyone could put me to resources that could explain this, then it would be much appreciated. My guess is that it has to do with the use of location and/or proxy_pass, but digging through the docs didn't lead to any deeper insights.

Another thing to note is that this app is being sent through CloudFlare.

0

This is resolved. I stuck to my original nginx config and used a CloudFlare page rule to force HTTPS.

The rule used was:

http://*gistbook.io/* => always use HTTPS

  • please accept your own answer. – Tim Oct 7 '17 at 6:38
  • If this actually is resolved, please remember to accept the answer. – Michael Hampton Jul 17 '18 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.