1

我有一台服务器,它同时运行站点的开发和暂存实例,每个版本都必须在端口 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;
}

知道我在哪里搞砸了我的配置吗?

4

1 回答 1

2

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

除此之外,配置对我来说还不错(好吧,您可以将index声明移至您的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;
}
于 2012-12-06T21:55:34.647 回答