nginx将所有http重定向到https,但有异常

时间:2015-01-20 18:24:41

标签: nginx url-redirection

我想将所有http流量重定向到https,只有少数例外。我希望保留在http。

的网址/例外/中的任何内容

尝试了Redirect all http to https in nginx, except one file

建议的以下内容

但它没有用。 / 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;
}

1 个答案:

答案 0 :(得分:6)

Nginx找到最长的匹配位置并首先对其进行处理,但无论如何处理服务器块末尾的返回。这将重定向除了/ 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;
    }    
}