从 nginx if 指令中排除位置

从 nginx if 指令中排除位置

目前我使用它将所有人重定向到 SSL 页面:

if ($scheme != "https") {
    rewrite ^ https://$server_name$request_uri? permanent;
}

有人知道如何排除特定位置吗?

例如,如果我想排除 news.jpg 或 news.php

感谢您的帮助!

答案1

这应该可以解决问题。我们将 https 重定向包含在位置块中,然后为不想重定向的位置定义额外的空位置块。nginx 将首先匹配更具体的位置块,因此它不会匹配重定向块。

return 301对于像这里这样的简单重定向,是一种更快的方法。

location / {
    if ($scheme != "https") {
        return 301 https://$server_name$request_uri;
    }
}

location /news.jpg {
}

相关内容