2015-01-09 11 views
25

को छोड़कर, http से https तक सब कुछ रीडायरेक्ट करें, मेरे पास एक वेबसाइट है जो केवल एक यूआरएल-पैटर्न को छोड़कर HTTPS पर पहुंच योग्य होनी चाहिए (क्योंकि मेरे पास कुछ पेज http-iframe हैं और मैं सुरक्षा से बचना चाहता हूं चेतावनी)nginx: एक यूआरएल-पैटर्न

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

यह किसी के लिए उपयोगी हो सकता है, लेकिन यदि आप एक (या अधिक) स्थान को छोड़कर सब कुछ HTTPS पर रीडायरेक्ट करने का प्रयास करते हैं, तो ध्यान रखें कि यदि आप PHP का उपयोग कर रहे हैं, तो HTTP के लिए दोनों सर्वर {} ब्लॉक में PHP हैंडलिंग कोड शामिल करना याद रखें और एचटीटीपीएस संस्करण। –

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

मेरा मतलब स्थायी पुनर्लेखन था। –