How do I properly write an "or" statement in an Nginx location block?
8,879
In nginx, if is evil. But if you really want to do it that way, here's how you would do it:
if ($request_uri ~* "($\/image\/.*)|(.*\.(ico|gif|jpe?g|png)$)") {
Essentially, just combine the regexes.
Author by
Dave
Updated on September 18, 2022Comments
-
Dave almost 2 years
I'm using Nginx on CentOS 7. I want to add a cache control header for files that end in a particular extension or taht contain a "/image/" string in the URL. I tried this
location / { proxy_pass http://scale; # match the name of upstream directive which is defined above proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; if ($request_uri ~* ".(ico|gif|jpe?g|png)$") | ($request_uri ~* "/image/") { expires 60d; access_log off; add_header Pragma public; add_header Cache-Control "public"; break; } }
but upon restarting the server, I'm getting a
invalid condition "$request_uri" in /etc/nginx/conf.d/scale.conf:16
error. If I remove " | ($request_uri ~* "/image/")" from the problematic line, all restarts fine, but then I'm unable to match stuff that I want. How do I write a proper or statement in my configuration file?