2014-12-07 18 views
0

現在我有這種類型的解決方案:如何既通過HTTP和HTTPS處理在nginx的某些URL重定向和所有其餘爲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 S'

回答

0

我真的不知道,如果它是「更好」皮爾斯,但你可以定義默認位置爲:

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

和,而不是使用如果,你的特殊情況下,配置爲地點,如:

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

擁有不同的位置爲您提供更大的靈活性,並且在我看來看起來更乾淨。

+0

是的,但是我必須爲http和https服務器重複這個部分'location〜* /(internal | export)/ {...}'。 – chestozo 2014-12-08 20:51:47

+0

如果你需要它們,你可以將它們移動到你的main.conf中,對嗎? – Marian 2015-01-28 23:41:44

相關問題