nginx 将所有 http 重定向到 https,但有例外

nginx redirect all http to https with exceptions

除了少数例外,我想将所有 http 流量重定向到 https。 url 中带有 /exception/ 的任何内容我想继续使用 http.

已尝试

建议的以下方法

但它不起作用。 /exception/ urls 将从 nginx 传递到 apache 以在 laravel 框架中进行一些 php 处理,但这无关紧要。

非常感谢任何改进建议!

server {
    listen 127.0.0.1:80;

    location / {
        proxy_pass http://127.0.0.1:7080;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-Accel-Internal /internal-nginx-static-location;
        access_log off;
    }

    location /exception/ {
        # empty block do nothing
        # I've also tried adding "break;" here
    }

    return 301 https://localhost$request_uri;
}

Nginx 找到最长的匹配位置并首先处理它,但是服务器块末尾的 return 不管怎样都被处理了。这将重定向除上游传递的 /exception/ 之外的所有内容。

server { 
    listen 127.0.0.1:80;
    access_log off;

    location / {
        return 301 https://localhost$request_uri; 
    }

    location /exception/ {
        proxy_pass http://127.0.0.1:7080;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-Accel-Internal /internal-nginx-static-location;
    }    
}