簡體   English   中英

如何同時通過http和https處理nginx中的一些url,並將其余所有重定向到https

[英]How to handle some urls in nginx both via http and https and redirect all the rest to https

現在,我有這種解決方案:

# HTTP server setup
server {
    server_name dev.server.com ;
    listen 80;
    set root /usr/local/www/me ;

    # So here comes the tricky part to allow handling some urls
    # both via http / https:

    set $allow_http 'no' ;

    if ($uri ~* "^\/(internal|export)\/") {
        set $allow_http 'yes' ;
    }

    if ($request_uri = '/some/?tricky=url') {
        set $allow_http 'yes' ;
    }

    if ($allow_http = 'no') {
        return 301 https://$host$request_uri ;
    }

    include /home/me/main.conf ;
}

# HTTPS server setup
server {
    server_name dev.server.com ;
    listen 443 ssl;
    set root /usr/local/www/me ;

    include /home/me/main.conf ;
}

它是這樣的。

但是,也許有更好的方法可以在不使用if的情況下完成相同的結果?

我不太確定它的性能是否更好,但是您可以將默認位置定義為:

# Default redirect to SSL server
location / {
    return 301 https://$host$request_uri;
}

而不是使用Ifs,而是將特殊情況配置為位置,例如:

location ~* /(internal|export)/ {
   echo "Doing internal http stuffs here"
}

我認為,將地點分開放置可以為您提供更大的靈活性,並且看起來更干凈。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM