I just want to ask an opinion about using an if statement on NGINX server.

What is the deal:

I'm configuring GCP HTTP(S)load balancer for a laravel app, but the thing is GCP LB it doesn't support forwarding traffic from HTTP to HTTPS (here is a thread discussing that topic)

now the most convenient approach I found on the internet is to configure the NGINX on my nodes with an if statement so it will automatically forward the traffic from HTTP to HTTPS.

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

Now, with this option, it works as it should but I'm more concern about the reliability because according to NGINX documentation here they strongly recommend we shouldn't use if statements if we don't really really have to.

I tried using the rewrite and return option but it doesn't work with GCP load balancer in front of them. It gives too many redirects error.

Can anyone give their opinion about this?


Your Answer

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

Browse other questions tagged or ask your own question.