同一台Nginx服务配置多个 server_name

  • login.example.com
  • system.example.com
  • example.com
#禁止IP访问
server {
  listen 80 default;
  server_name _;
  return 403;
}


# login.example.com
server {
  #SSL configuration
  listen 443 ssl;
  listen [::]:443 ssl;

  server_name login.example.com;
  #ssl证书配置
  ssl_certificate "/xx/xx/ssl/a.pem";
  ssl_certificate_key "/xx/xx/ssl/a.key";
  ssl_session_cache shared:SSL:1m;
  ssl_session_timeout 10m;
  ssl_ciphers HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers on;

  #index.html
  location / {
    root /var/www/login.example.com/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html;
  }

  location /server {
    proxy_pass http://127.0.0.1:8080;
  }
}


# http 跳转 https
server {
  listen 80;
  server_name login.example.com;
  return 301 https://login.example.com$request_uri;
}


# system.example.com
server {
  #SSL configuration
  listen 443 ssl;
  listen [::]:443 ssl;
  server_name system.example.com;

  ssl_certificate "/xx/xx/ssl/a.pem";
  ssl_certificate_key "/xx/xx/ssl/a.key";
  ssl_session_cache shared:SSL:1m;
  ssl_session_timeout 10m;
  ssl_ciphers HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers on;

  location / {
    root /var/www/system.example.com/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html;
  }

  location /server {
    proxy_pass http://127.0.0.1:8080;
  }
}


# http 跳转 https
server {
  listen 80;
  server_name system.example.com;
  return 301 https://system.example.com$request_uri;
}


#example.com
server {
  #SSL configuration
  listen 443 ssl default_server;
  listen [::]:443 ssl default_server;
  server_name example.com;

  ssl_certificate "/xx/xx/ssl/a.pem";
  ssl_certificate_key "/xx/xx/ssl/a.key";
  ssl_session_cache shared:SSL:1m;
  ssl_session_timeout 10m;
  ssl_ciphers HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers on;

  location / {
    root /var/www/example.com/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
}


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

Q.E.D.


曙光在头上,不抬起头,便永远只能看见物质的闪光。