2015-01-09 4 views
25

私は、http-iframeをいくつかのページに持っているので、HTTPS経由でしかアクセスできないWebサイトを持っています。警告)nginx:httpからhttpsに至るまで、すべてのURLをリダイレクトします.

E.g. this pages should be redirected to https: 
http://example.com 
http://example.com/a/this-is-an-article 
http://example.com/v/this-is-a-video 

This pages should not be redirected to https (or should be redirected form https to http) 
http://example.com/l/page-with-unsafe-iframe 
http://example.com/l/other-page-with-unsafe-iframe 
+0

http iframeページは常に同じディレクトリ/ l /ですか? –

+0

はいそれは常に/ l /ディレクトリの下にあります – tiefenb

答えて

41

iframeページが常に同じディレクトリにある場合は、単純なプレフィックスの場所を使用できます。

server { 
    listen 443; 

    location /l/ { # redirect https iframe requests to http server 
     return 301 http://$server_name$request_uri; 
    } 
    # ... 
} 

server { 
    listen 80; 

    location/{ # the default location redirects to https 
     return 301 https://$server_name$request_uri; 
    } 

    location /l/ {} # do not redirect requests for iframe location 
    # ... 
} 
+0

ありがとうございます!それは私をたくさん助けた – tiefenb

+0

私はそれが助けてうれしいです。 –

+0

誰かにとっては便利かもしれませんが、1つ(またはそれ以上)の場所を除いてすべてをHTTPSにリダイレクトしようとすると、PHPハンドリングコードをHTTPのserver {}ブロックに含めてください&HTTPSバージョン –

10

あなたは、たとえば、マップとシンプルなリダイレクトルールを使用することがあります。

map $uri $redirect_https { 
    /l/page-with-unsafe-iframe   0; 
    /l/other-page-with-unsafe-iframe 0; # you can use regex here 
    default       1; 
} 

server { 
    listen 443; 

    if ($redirect_https = 0) { 
     return 301 http://$server_name$request_uri; 
    } 

    # other code 
} 
server { 
    listen 80; 

    if ($redirect_https = 1) { 
     return 301 https://$server_name$request_uri; 
    } 

    # other code 
} 

私は301リダイレクトは恒久的な書き換えとは異なり、良い習慣であることを言及する必要があります。

+1

301永久的なリダイレクトです。 –

+0

私は永続的な書き換えを意味しました。 –

関連する問題