Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is NGINX's $request_uri empty?

I’m using NGINX (v4.5.2) on a Kubernetes cluster, and I’m trying to configure it to redirect non-www URLs to the www equivalent. For example…

https://example.com -> https://www.example.com
https://example.com/about -> http://www.example.com/about

So this is what I’ve added to the annotations (I’ll carry on using example.com as the URL host)…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

annotations:
  ...
  nginx.ingress.kubernetes.io/from-to-www-redirect: 'true'
  nginx.ingress.kubernetes.io/configuration-snippet: |
    if ($host = 'example.com' ) {
      rewrite ^ https://www.$request_uri permanent;
    }

However, if I now navigate to https://example.com I get redirected to https://www..

The docs say $request_uri contains …

full original request URI (with arguments)

… so why is $request_uri apparently empty in my case?

>Solution :

$request_uri will contain the path with arguments. To get your desired redirect you probably want to use $host$request_uri.
For some examples of usage of $request_uri you can search it on https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ to see examples or https://www.webhosting24.com/understanding-nginx-request_uri/ also explains it quite well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading