仅针对nginx上的特定网址重定向https

时间:2014-03-14 06:36:49

标签: apache ssl nginx https http-redirect

我正在尝试让https与一些网址一起使用。但似乎https无处不在。详细说明,我在Nginx上创建了2个vhost。第一个虚拟主机端口为80,另一个虚拟主机为443,包含SSL。现在我的网站.i.e domain.com适用于http和https,这不是我想要的。我希望https在我用Nginx vhost中的规则指定的一些网址上工作。

主要问题是,当我尝试首先使用http获取主站点时,当我转到包含https“secure_area”的URL时,它运行正常。但是,每当我在我网站的其他地方追踪它时,https会继续使用所有其他网址。

这是我的443 vhost配置:

ssl_session_cache共享:SSL:5m; 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 vhost配置:

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在前端Apache上作为反向proexy工作

现在有没有人知道如何仅在某些网址上强制使用https,在我的情况下强制使用secure_area并在所有其他网址上强制使用http?

由于

1 个答案:

答案 0 :(得分:3)

如果访问过任何其他网址,您可以告诉SSL服务器重定向回http

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;
  }
}
相关问题