使用https和虚拟主机为nginx设置自定义错误页面

我们有一个作为负载均衡器的nginx(OpenResty 1.4.2.7)实例。 它有两个服务器指令,一个服务于一个特定的站点(让我们称之为www.our-special-host.com)和一个通配符。 我们正在尝试对其进行configuration,以便显示不同的502错误页面,具体取决于两个服务器伪指令的后端是closures的。

我们的configuration适用于HTTP,但不适用于HTTPS。 如果我们closures后端并打到www.our-special-host.com,我们会得到适当的HTTP和HTTPS错误。 但是,如果我们点击了其他托pipe站点,则会得到HTTP的正确错误页面,但对于HTTPS,我们会获得www.our-special-host.com的错误页面。

这里是我们的configuration(轻度编辑):

server { server_name www.our-special-host.com listen 80; listen 443 ssl; error_page 502 /nginx_errors/loadbalancer_502_on_special_host.html; location /nginx_errors/ { alias /path/to/nginx_errors/; } location / { proxy_pass xxxx; ... } ssl_certificate certificate.crt; ssl_certificate_key pk.key; } server { listen 80; listen 443 ssl; error_page 502 /nginx_errors/loadbalancer_502_on_other_hosts.html; location /nginx_errors/ { alias /path/to/nginx_errors/; } location / { proxy_pass yyyy; ... } ssl_certificate certificate.crt; ssl_certificate_key pk.key; } 

(所有相关主机都是XXX.ourdomain.com,证书是* .ourdomain.com。)

[更新]在迈克尔·汉普顿的评论下面,我添加了一个显式的通用正则expression式的第二个服务器块,即。

  server_name ~^.*$; 

行为仍然是错误的,但不同的是:

  • “特殊”网站用http :我们得到错误的页面,loadbalancer_502_on_other_hosts.html
  • 使用https的 “特殊”网站:我们得到正确的错误页面loadbalancer_502_on_special_host.html
  • “非特殊”网站与http :我们得到正确的错误页面,loadbalancer_502_on_other_hosts.html
  • 使用https的 “非特殊”网站:我们得到正确的错误页面,loadbalancer_502_on_other_hosts.html

顺便说一句,catch_all服务器名称通过server_name _; ,不需要正则expression式

从你的错误描述看来,它似乎是错误的服务器{} – 使用,但只在http,而不是https。

你有没有一个单独的IP为每个SSL主机? 如果没有,是你的nginx SNI意识? 你会用`nginx -V'来检查,它应该给出这样一行:

 $ ~/nginx -V nginx version: nginx/1.4.1 built by gcc 4.4.5 (Debian 4.4.5-8) TLS SNI support enabled .... 

如果你的nginx是SNI感知的,那么你的浏览器/操作系统可能是问题; Windows XP不能使用SNI感知。 SNI是一个openssl特性,你至less需要0.9.8f

如果SNI对您是个问题:为每个SSL主机使用单独的IP。

更多的debugging提示:

  • 为每个服务器使用单独的access_log,例如access_log /var/log/nginx/special_host.access.log; 为您的专用主机
  • 提出请求并检查,确实在哪个服务器上运行; 确保http和https你在正确的服务器上
  • 如果这是在server_part内正确debugging