SSL重定向失败

SSL重定向失败,ssl,nginx,Ssl,Nginx,我有一台同时运行站点开发和登台实例的服务器,每个版本都必须在端口80和443上应答。登台实例(只有一个)的工作方式与我预期的完全相同,但是为每个用户配置的开发实例可以直接在任一协议上加载给定的页面,但是如果我在一个端口上的页面上并尝试链接到另一个端口,则会失败 我的配置 server { listen 80; server_name ~^dev\.(?<username>[^.]+)\.client\.tld\.net$ ~^(?&

我有一台同时运行站点开发和登台实例的服务器,每个版本都必须在端口80和443上应答。登台实例(只有一个)的工作方式与我预期的完全相同,但是为每个用户配置的开发实例可以直接在任一协议上加载给定的页面,但是如果我在一个端口上的页面上并尝试链接到另一个端口,则会失败

我的配置

  server {
    listen 80;
    server_name ~^dev\.(?<username>[^.]+)\.client\.tld\.net$
                ~^(?<username>[^.]+)\.client\.dev\.tld\.net$
                ~^(?<username>[^.]+)\.dev\.client\.tld\.net$;

    location / {
      rewrite ^(.*) http://$username.client.tld.net$1 permanent;
    }
  }
  # This is the primary host that will ultimately answer requests.
  server {
    listen      80;
    server_name ~^(?<username>[^.]+)\.client\.tld\.net$;
    root        /home/$username/client/www/app/webroot;
    index       index.php;

    access_log /var/log/nginx/client.sandbox.access.log;
    error_log  /var/log/nginx/client.sandbox.error.log;

    location / {
      try_files $uri $uri/ /index.php?url=$uri;
    }

    location ~ \.php$ {
      include /etc/nginx/conf/php;
    }

    include /etc/nginx/conf/expire_content;
    include /etc/nginx/conf/ignore;
  }

  server {
  listen 443 ssl;
  server_name ~^dev\.(?<username>[^.]+)\.client\.tld\.net$
              ~^(?<username>[^.]+)\.client\.dev\.tld\.net$
              ~^(?<username>[^.]+)\.dev\.client\.tld\.net$;

  location / {
    rewrite ^(.*) https://$username.client.tld.net$1 permanent;
  }
}
# This is the primary host that will ultimately answer requests.
server {
  listen      443 ssl;
  server_name ~^(?<username>[^.]+)\.client\.tld\.net$;
  root        /home/$username/client/www/app/webroot;
  index       index.php;

  include /etc/nginx/conf/ssl;

  access_log /var/log/nginx/client.sandbox.access.log;
  error_log  /var/log/nginx/client.sandbox.error.log;

  location / {
    try_files $uri $uri/ /index.php?url=$uri;
  }

  location ~ \.php$ {
    include /etc/nginx/conf/php;
  }

  include /etc/nginx/conf/expire_content;
  include /etc/nginx/conf/ignore;
}
服务器{
听80;
服务器名称~^dev.(?[^.]+)\.client\.tld\.net$
~^(?[^.]+)\.client\.dev\.tld\.net$
~^(?[^.]+)\.dev\.client\.tld\.net$;
地点/{
重写^(.*)http://$username.client.tld.net$1永久;
}
}
#这是最终响应请求的主主机。
服务器{
听80;
服务器名称~^(?[^.]+)\.client\.tld\.net$;
root/home/$username/client/www/app/webroot;
index.php;
access\u log/var/log/nginx/client.sandbox.access.log;
error\u log/var/log/nginx/client.sandbox.error.log;
地点/{
试试文件$uri$uri//index.php?url=$uri;
}
位置~\.php${
包括/etc/nginx/conf/php;
}
包括/etc/nginx/conf/expire\u内容;
include/etc/nginx/conf/ignore;
}
服务器{
听443ssl;
服务器名称~^dev.(?[^.]+)\.client\.tld\.net$
~^(?[^.]+)\.client\.dev\.tld\.net$
~^(?[^.]+)\.dev\.client\.tld\.net$;
地点/{
重写^(.*)https://$username.client.tld.net$1永久;
}
}
#这是最终响应请求的主主机。
服务器{
听443ssl;
服务器名称~^(?[^.]+)\.client\.tld\.net$;
root/home/$username/client/www/app/webroot;
index.php;
包括/etc/nginx/conf/ssl;
access\u log/var/log/nginx/client.sandbox.access.log;
error\u log/var/log/nginx/client.sandbox.error.log;
地点/{
试试文件$uri$uri//index.php?url=$uri;
}
位置~\.php${
包括/etc/nginx/conf/php;
}
包括/etc/nginx/conf/expire\u内容;
include/etc/nginx/conf/ignore;
}

知道我在哪里配置了吗?

首先,没有必要创建四个单独的配置,因为您的两个服务器(HTTP和HTTPS)的主体完全相同。您可以使用
$scheme
变量,该变量包含
http
https
,具体取决于您刚刚使用的上下文(用于重定向)。其次,我在
dev
配置中没有看到任何
root
声明,也没有可能导致浏览器出现问题的证书

除此之外,配置在我看来还行(好吧,您可以将
索引
声明移动到
http
配置中;这样您就不必一直重复它)

请查看我为您制作的以下(注释)示例配置。也许有帮助

# Put this in http context!
index           index.php;

server {
  # One server configuration to rule them all!
  listen        80;
  listen        443 ssl;

  # Seems legit.
  server_name   ~^dev\.(?<username>[^.]+)\.client\.tld\.net$
                ~^(?<username>[^.]+)\.client\.dev\.tld\.net$
                ~^(?<username>[^.]+)\.dev\.client\.tld\.net$;

  # Where am I?
  #root          /home/$username/client/www/app/webroot;

  # No wildcard certificate? No need to specify /etc/nginx as all paths
  # in the configuration are relative to the installation path.
  #include       conf/ssl;

  location / {
    # May work as well, can't test.
    #rewrite ^(.*) $scheme://$server_name$1 permanent;
    rewrite ^(.*) $scheme://$username.client.tld.net$1 permanent;
  }
}

server {
  listen        80;
  listen        443 ssl;
  server_name   ~^(?<username>[^.]+)\.client\.tld\.net$;
  root          /home/$username/client/www/app/webroot;
  include       conf/ssl;
  access_log    /var/log/nginx/client.sandbox.access.log;
  error_log     /var/log/nginx/client.sandbox.error.log;

  location / {
    try_files $uri $uri/ /index.php?url=$uri;
  }

  location ~ \.php$ {
    include     conf/php;
  }

  include       conf/expire_content;
  include       conf/ignore;
}
#将其置于http上下文中!
index.php;
服务器{
#一个服务器配置来管理所有服务器!
听80;
听443ssl;
#似乎合法。
服务器名称~^dev.(?[^.]+)\.client\.tld\.net$
~^(?[^.]+)\.client\.dev\.tld\.net$
~^(?[^.]+)\.dev\.client\.tld\.net$;
#我在哪里?
#root/home/$username/client/www/app/webroot;
#没有通配符证书?无需将/etc/nginx指定为所有路径
#在配置中,是相对于安装路径的。
#包括conf/ssl;
地点/{
#也可能有效,但无法测试。
#重写^(.*)$scheme://$server\u name$1永久;
重写^(.*)$scheme://$username.client.tld.net$1永久;
}
}
服务器{
听80;
听443ssl;
服务器名称~^(?[^.]+)\.client\.tld\.net$;
root/home/$username/client/www/app/webroot;
包括conf/ssl;
access\u log/var/log/nginx/client.sandbox.access.log;
error\u log/var/log/nginx/client.sandbox.error.log;
地点/{
试试文件$uri$uri//index.php?url=$uri;
}
位置~\.php${
包括conf/php;
}
包括conf/u内容;
包括conf/ignore;
}