2014-03-14 165 views
1

我试图让https与某些网址一起工作。但似乎https到处都是。详细地说,我在Nginx上创建了2个虚拟主机。第一台虚拟主机,端口80,另一台虚拟主机443包含SS​​L。现在我的网站.i.e domain.com适用于http和https,这不是我想要的。我希望https可以在Nginx虚拟主机中使用规则指定的某个url上工作。使用https重定向仅针对nginx上的特定网址

主要问题是,当我尝试让我的主站点首先使用http,然后当我转到包含https“secure_area”的url时,它工作正常。但是,无论何时我在其他地方追踪我的网站,https都会继续使用所有其他网址。

这里是我的443虚拟主机配置:

ssl_session_cache共享:SSL:5米; add_header Strict-Transport-Security“max-age = 31536000; includeSubDomains”;

server { 
     listen 443 ssl spdy; 
     listen [::]:443 ssl spdy; 
     #server_name www.mydomain.com; 

     ssl_session_timeout 5m; 
     root /vars/www/public_html/; 
     index index.php index.html index.htm; 
     ssl_certificate /path_to_ssl/certificate.pem; 
     ssl_certificate_key /path_to_key/server.key; 
     ssl_ciphers 'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS'; 
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
     ssl_prefer_server_ciphers on; 

     location/{ 
     try_files $uri $uri/ /index.php; 
     } 

     location ~ \.php$ { 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $remote_addr; 
     proxy_set_header Host $host; 
     proxy_pass http://127.0.0.1:8080; 
     } 

     location ~ /\.ht { 
       deny all; 
     } 

     # Serve static files directly 
     location ~* \.(png|jpe?g|gif|ico)$ { 
       expires 1y; access_log off; try_files $uri $uri/ @rewrite; gzip off; 
     } 
     location ~* \.(css)$ { 
       expires 1d; access_log off; 
     } 
     location ~* \.(js)$ { 
       expires 1h; access_log off; 
     } 
     location /secure_area/ { 
     auth_basic   "Restricted"; 
     auth_basic_user_file htpasswd; 
     rewrite^https://$http_host$request_uri? permanent; 
     } 


    } 

,这里是我的80虚拟主机配置:

server { 
     listen 80 default_server; 
     server_name mydomain.com; 
     return 301 http://www.mydomain.com; 
} 

server { 
     listen 80; 
     server_name www.mydomain.com; 
     root /vars/www/public_html/; 
     index index.php index.html index.htm; 

     location/{ 
     try_files $uri $uri/ /index.php; 
     } 
     location ~ \.php$ { 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $remote_addr; 
     proxy_set_header Host $host; 
     proxy_pass http://127.0.0.1:8080; 
     } 
     location ~ /\.ht { 
       deny all; 
     } 

      location /secure_area/ { 
     rewrite^https://$http_host$request_uri? permanent; 
     } 

    } 

的情况下,没有人注意到,Nginx的在前端工作作为反向proexy阿帕奇

现在没有任何人有任何想法如何强制HTTPS只在一些网址和在我的情况下secure_area和强制HTTP所有其他网址?

感谢

回答

3

你可以告诉SSL服务器来回重定向到http是否有任何其他的URL访问

server { 
    listen 80; 
    server_name example.com; 
    # normal http settings 
    location /secure_area/ { 
    return 301 https://$http_host$request_uri$is_args$query_string; 
    } 
} 
server { 
    listen 443 ssl spdy; 
    server_name example.com; 
    # ssl settings; 
    location /secure_area/ { 
    #serve secure area content 
    } 
    location/{ 
    return 301 http://$http_host$request_uri$is_args$query_string; 
    } 
} 
+0

感谢@Mohammad的代码。我会试试看看它是否有效。 –

+0

不幸的是,在尝试了最后2个小时后,代码无法工作。可能这只适用于通配符SSL,不是吗? –

+0

不是真的,你到底得到了什么? –

相关问题