Nginx为同主机配置Https多域名

前面,我有提到nginx多网站配置,以及nginx配置https.但是在windows下面同主机配置多Https域名暂未提及,那么这次正好遇到这个场景。我就把他记录下来

前言

最近在做一个私活,因为老板考虑到节约成本,就只有一台2核4G的windows主机,可是现在是要独立出来多个微信小程序,故会涉及到多个应用。当然我也以才用nginx强大的location配置功能转发到不同的目录这也是可以实现的。但是感觉不是很优雅,所以还是使用强大的Server模块吧。我们来看看官网介绍http://nginx.org/en/docs/http/configuring_https_servers.html,官方文档也是很详细的

下载Nginx

windows使用nginx很简单,直接下载官方编译好的即可(如果自己来编译的话,操作还是有些繁琐).下载nginx的zip压缩包,如果想使用nginx+lua的话,那么就去下载OpenResty吧。

配置Nginx

首先肯定是去域名注册商去下载nginx的SSL证书,然后把我们的证书放在nginx的conf目录下。接下来就是编辑我们的nginx.conf配置文件。配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
server {
listen 443 ssl;
server_name xxxxx.baoqipai.com;
ssl_certificate cert/xxxxx.baoqipai.com.pem;
ssl_certificate_key cert/xxxxx.baoqipai.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://127.0.0.1:8002;
}
}
server {
listen 443 ssl;
server_name admin.xxxxx.nationallab.cn;
ssl_certificate cert/admin.xxxxx.nationallab.cn.pem;
ssl_certificate_key cert/admin.xxxxx.nationallab.cn.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://127.0.0.1:8001;
}
}
server {
listen 443 ssl;
server_name host.xxxxx.nationallab.cn;
ssl_certificate cert/host.xxxxx.nationallab.cn.pem;
ssl_certificate_key cert/host.xxxxx.nationallab.cn.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://127.0.0.1:8001;
}
}

启动nginx,解析域名,此时不出意外应该可以正常访问了。

遇到的问题

这里有个会有一个小问题,会报could not build server_names_hash, you should increase server_names_hash_bucket_size: 32,这是因为server配置中server_name的值过长导致,http://nginx.org/en/docs/http/server_names.html提到修改server_names_hash_bucket_size的值:

http {
server_names_hash_bucket_size 64;

那么server_names_hash_bucket_size的默认值是多少呢?
官网文档中有提到:

1
2
3
Syntax:	server_names_hash_bucket_size size;
Default: server_names_hash_bucket_size 32|64|128;
Context: http

Sets the bucket size for the server names hash tables. The default value depends on the size of the processor’s cache line. The details of setting up hash tables are provided in a separate document.

# ,
You forgot to set the qrcode for Alipay. Please set it in _config.yml.
You forgot to set the qrcode for Wechat. Please set it in _config.yml.
You forgot to set the business and currency_code for Paypal. Please set it in _config.yml.
You forgot to set the url Patreon. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×