大网数据广告活动
服务器运维问答

Nginx serves the wrong certificate. Two domains with redirect

· 52CE 我爱测

I have two domains example.com and example.org . Both domains point to the same server with a static IP. The first configuration file (for example.com ): # /etc/nginx/sites-enabled/example.com.conf # Catchall for all non-existent…

问题原文

原帖链接 · zarathustra · CC BY-SA 3.0

I have two domains example.com and example.org. Both domains point to the same server with a static IP.

The first configuration file (for example.com):

# /etc/nginx/sites-enabled/example.com.conf

# Catchall for all non-existent domains
server {
   return 404;
}

# Should redirect http://example.com to https://example.com
server {
   listen 80;
   server_name example.com;
   return 301 https://$server_name$request_uri;
}

# Should redirect http://www.example.com to https://www.example.com
server {
  listen 80;
  server_name www.example.com;
  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl;
  server_name example.com;

  # Using LetsEncrypt certs
  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

  root /var/www/example.com/;
  index index.html;

}

And here's the other configuration file

# /etc/nginx/sites-enabled/example.org.conf

# Should redirect http://example.org to https://example.org
server {
   listen 80;
   server_name example.org;
   return 301 https://$server_name$request_uri;
}

# Should redirect http://www.example.org to https://www.example.org
server {
  listen 80;
  server_name www.example.org;
  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl;
  server_name example.org;

  ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;

  root /var/www/example.org/;
  index index.html;

}

So here's the problem

http://example.com -> https://example.com ✔
https://example.com ✔
http://www.example.com -> https://www.example.com ✔
https://www.example.com ✔

http://example.org -> https://example.org ✔
https://example.org ✔
http://www.example.org -> https://www.example.org ✗
https://www.example.org ✗

In the last two cases (www.example.org) the certificate of example.com is served.

SNI support is enabled: $ nginx -V => TLS SNI support enabled

What's wrong with my config?

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

You do not have server_names for the secure versions of your domains when using the www. prefix, so the default server is used in each case.

In the absence of an explicit default server, the first server block matching the appropriate port is used.

You need to add the www. variants to the server_name directive in your secure server blocks, or use the wild card syntax.

See this document for details.

For example:

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

server {
    listen 443 ssl;
    server_name example.com www.example.com;
    ...
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-01-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。