2012-01-26 71 views
3

我有一個啓用ssl的nginx服務器。 目前我已爲所有目錄啓用了https。 如何啓用ssl僅用於www.example.com/shop/*目錄並禁用其他?ssl用於nginx服務器配置中的子目錄

這裏是我的conf文件:

# Redirect everything to the main site. 
server { 
    server_name *.example.com; 
    listen 80; 
    ssl on; 
    ssl_certificate /opt/nginx/conf/server.crt; 
    ssl_certificate_key /opt/nginx/conf/server.key; 
    keepalive_timeout 70; 

    access_log /home/example/nginx_logs/access.log ; 
    error_log /home/example/nginx_logs/error.log ; 

    root /home/example/public_html/example.com; 
    location ~ \.php$ { 
     try_files $uri $uri/ /index.php?q=$uri&$args;   
     root   /home/example/public_html/example.com/; 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     include  /opt/nginx/conf/fastcgi_params; 
     #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script; 
     fastcgi_param SCRIPT_NAME  $fastcgi_script_name; 
     fastcgi_param SCRIPT_FILENAME /home/example/public_html/example.com$fastcgi_script_name; 
     index index.php index.html index.htm; 
    } 

    if ($http_host != "example.com") { 
     rewrite^http://example.com$request_uri permanent; 
    } 

    include global/restrictions.conf; 

    # Additional rules go here. 

    #Only include one of the files below. 
    include global/wordpress.conf; 
# include global/wordpress-ms-subdir.conf; 
# include global/wordpress-ms-subdomain.conf; 
} 

tnanks, d

+0

沒辦法。 'ssl'是一個只存在於'http'或'server'的指令(如文檔所述) –

回答

11

這是很容易在Nginx的實現。它涉及兩個步驟。

  1. 僅當訪問yourdomain.com/shop時纔會使用端口443。所有其他請求將被重定向到端口80(HTTP)
  2. 端口80將檢查yourdomain.com/shop。如果找到,它將被重定向到端口443(HTTPS)。

這裏是它如何能夠做到快速瀏覽......

server { 
    listen 443; 
    server_name yourdomain.com; 

    # directives for SSL certificates 

    # root, index, error_log, access_log directives 

    location /shop { 
    # directives to handle what's inside /shop, for example 
    # try_files $uri $uri/ /index.php; 
    } 

    location ~ \.php$ { 
    # directives to handle PHP files 
    } 

    # leave everything else to port 80 
    location/{ 
    rewrite^http://$host$request_uri permanent; 
    } 
} 

server { 
    listen 80; 
    server_name yourdomain.com; 

    # root, index, error_log, access_log directives 

    # redirect yourdomain.com/shop to port 443 
    # Please put this before location/block as 
    # nginx stops after seeing the first match 
    location /shop { 
    rewrite^https://$host$request_uri permanent; 
    } 

    location/{ 
    # directives to handle what's inside /, for example 
    # try_files $uri $uri/ /index.php; 
    } 

    location ~ \.php$ { 
    # directives to handle PHP files 
    } 

} 
+1

感謝您修復了錯字和選項命名@atmosx。 –