如何在 Nginx 位置區塊中正確編寫「或」語句?

如何在 Nginx 位置區塊中正確編寫「或」語句?

我在 CentOS 7 上使用 Nginx。我試過這個

  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;
    }
  }

但重新啟動伺服器後,我得到了

invalid condition "$request_uri" in /etc/nginx/conf.d/scale.conf:16

錯誤。如果我從有問題的行中刪除“ | ($request_uri ~* “/image/”)”,一切都會重新啟動,但隨後我無法匹配我想要的東西。如何在設定檔中寫入正確的 or 語句?

答案1

在 Nginx 中,如果是邪惡的。但如果你真的想這樣做,你可以這樣做:

if ($request_uri ~* "($\/image\/.*)|(.*\.(ico|gif|jpe?g|png)$)") {

本質上,只需組合正規表示式即可。

相關內容