【问题标题】:redirect with https only for specific url on nginx仅针对 nginx 上的特定 url 使用 https 重定向
【发布时间】:2014-04-19 07:43:02
【问题描述】:

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

主要问题是当我尝试首先使用 http 获取我的主站点,然后当我转到包含 https“secure_area”的 url 时,它工作正常。但是,每当我在我的网站的其他地方进行此操作时,https 都会继续在所有其他 url 上。

这是我的 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 虚拟主机配置:

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 中充当反向代理

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

谢谢

【问题讨论】:

    标签: apache ssl nginx https http-redirect


    【解决方案1】:

    如果访问了任何其他 URL,您可以告诉 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;
      }
    }
    

    【讨论】:

    • 感谢@Mohammad 提供代码。我会尝试一下,看看它是否有效。
    • 不幸的是,在尝试了最后 2 小时后,代码不起作用。这可能只适用于通配符 SSL,不是吗?
    • 您是否为第一台服务器设置了location /?普通的http服务器
    • 是的,我这样做了,但是没有用。必须将服务器块分开或放在同一个块中,这有关系吗?
    • 您将配置添加到我在代码中注释的位置,例如#ssl settings#serve secure area#normal http settings
    猜你喜欢
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 2012-11-20
    相关资源
    最近更新 更多