无法将 https 站点重定向到另一个 url - nginx 服务器块

Not able to redirect https site to another url - nginx server block

我正在尝试将促销子域附加到我已经在 https 上的网站,然后使用重定向 url 重定向到网站中的另一个页面。例如,基本上如果我的网站是 https://example.com 并且有一个页面 https://example.com/xyz/xyz/promo 那么当我输入 https://promo.example.com 到这个页面时我想要一个浏览器重定向。我已经设置了所有相关的 AWS route 53 设置。

我的 nginx 服务器块有这个

   server {                                                                                                                                                                             
            listen 80 default_server;
            listen [::]:80 default_server;
        return 301 https://example.com$request_uri;
    }

    server {
            server_name www.example.com;
            return 301 https://example.com$request_uri;
    }

    server {
        server_name example.com;
        return 301 https://example.com$request_uri;
    }

    server {
        server_name promo.example.com;
        return 301 https://example.com/xyz/xyz/promo;
    }
ssl_certificate /..path/..;
ssl_certificate_key //..path/..;
ssl_dhparam /..path/...;
ssl_trusted_certificate /..path/..;

add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
ssl_prefer_server_ciphers on;
ssl_ciphers .......; //hidden
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_session_cache   shared:SSL:10m;
ssl_session_timeout 10m;
ssl_buffer_size 1400;
spdy_headers_comp 0;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=86400;
resolver_timeout 10;

server {
    listen 443 ssl spdy;
    server_name example.com;
    include /etc/nginx/helper.conf;
    root /var/www/example/  ;
    index index.php index.html;
    charset utf-8;

    location / {
            add_header "Access-Control-Allow-Origin" "*";
            try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location ~ /\.ht {
            deny all;
    }
} 

当前行为:

当我直接输入 promo.example.com 时它重定向正确 而没有 https。但是如果我输入 https://promo.example.com 它只会显示 example.com,url 是 https://promo.example.com

预期行为:

如果我输入 https://promo.example.com,它应该重定向到 https://example.com/xyz/xyz/promo

我无法放置 https://promo.example.com 然后使用服务器块进行重定向,因为 nginx 会抛出错误。

如何重定向 https://promo.example.com 以转到 https://example.com/xyz/xyz/promo

试试这个:

server {
server_name promo.example.com;
rewrite ^ https://example.com/xyz/xyz/promo permanent;

每个服务器可以有一个 https 实例,在本例中是 example.com,因此当 https 是任何 url 的前缀时,无论 urls , 用户被重定向到 example.com.

要使用 https 将用户重定向到预期的 url,您需要为该站点提供单独的端口或单独的 IP。

根据 http://nginx.org/r/listen,listen 指令的默认值是 listen *:80 | *:8000;

If the directive is not present then either *:80 is used if nginx runs with the superuser privileges, or *:8000 otherwise.

因此,您提供的片段对 https 连接根本没有影响,因为它们不适用于 ssl 端口 443。

您没有提供您的 ssl 配置片段,也没有提供证书详细信息,以便我们为您提供完整的答案,但一旦理解了上述内容,答案可能就会发挥作用。

关于配置单个服务器来处理 HTTP 和 HTTPS 请求,您可以参考 http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server

P.S。一般来说,HTTPS 本身并没有很好地规划子域,除非您支付了额外费用,否则您的证书可能不涵盖任何子域,这意味着如果您或您的用户尝试这样做,它将导致浏览器警告通过 https:// 地址方案访问此类站点。

由于使用 Strict-Transport-Security header 浏览器自动提供 301 重定向,所以这个服务器块从未被使用过:

server {
    server_name promo.example.com;
    return 301 https://example.com/xyz/xyz/promo;
}

端口重定向 80->443 发生在浏览器连接到服务器之前,因此 Nginx 始终根据端口 443 提供最新的服务器块。这应该可以帮助您:

server {
    listen 443 ssl;
    listen 80;
    server_name promo.example.com;
    return 301 https://example.com/xyz/xyz/promo;
}

有两种方法可以解决此问题:

  • 拥有*.example.com的通配符证书,因此所有子域可以共享同一个证书。

  • 运行 每个 SSL 站点在不同的 IP 地址。这样,网络服务器就知道它可以向浏览器发送哪个 SSL 证书。 - 通过检查接收传入连接的 IP 地址。