Nginx通过域名和子域名转发至多服务+配置全站HTTPS+强制HTTPS

我们通常在云服务器上拥有一台服务器、一个域名的时候,可以通过多个子域名来进行不同服务(后端、前端等)的转发,其中多个子域名可以解析至同一个服务器。
比如,我们的主域名是:
www.domain.comdomain.com
子域名为:
sub1.domain.comsub2.domain.com等。
且在域名解析服务上完成域名的解析,此处所有的域名均指向同一服务器(公网IP),如:1.1.1.1

监听端口

监听:http的80端口和https的443端口
listen 80; listen 443 ssl;

申请https证书

腾讯云可以在域名解析中,点击SSL进行https的证书申请,个人用户可以选择免费版,大概几分钟至几十分钟可申请通过。子域名也需要进行申请。
其他厂商类似。

申请通过后,进行证书下载:

证书上传

解压压缩包后,将域名_bundle.crt文件和域名.key上传至Nginx的nginx.conf的同级目录下。

ssl配置

1
2
3
4
5
6
7
8
9
10
11
server_name  www.domain.com domain.com;
#证书文件名称
ssl_certificate domain.com_bundle.crt;
#私钥文件名称
ssl_certificate_key ldomain.com.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1.2 TLSv1.3;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
XML

http转https

1
2
3
4
# 强制跳转https
if ($scheme = http ) {
return 301 https://$host$request_uri;
}
XML

完整配置

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

client_max_body_size 50m;
client_body_buffer_size 10m;
client_header_timeout 1m;
client_body_timeout 1m;

gzip on;
gzip_buffers 4 16k;
gzip_comp_level 4;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;

server {
listen 80;
listen 443 ssl;
# 强制跳转https
if ($scheme = http ) {
return 301 https://$host$request_uri;
}
server_name www.domain.com domain.com;
#证书文件名称
ssl_certificate domain.com_bundle.crt;
#私钥文件名称
ssl_certificate_key domain.com.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1.2 TLSv1.3;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
root /home/app/public; #前端目录public
index index.html index.htm;
try_files $uri $uri/ /index.html;
}

location ^~ /api/ {
proxy_pass http://localhost:9080/; #后端服务
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}

server {
listen 80;
listen 443 ssl;
# 强制跳转https
if ($scheme = http ) {
return 301 https://$host$request_uri;
}
server_name sub1.domain.com;
#证书文件名称
ssl_certificate sub1.domain.com_bundle.crt;
#私钥文件名称
ssl_certificate_key sub1.domain.com.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1.2 TLSv1.3;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;

location / {
root /home/app/main; #前端地址main
index index.html index.htm;
try_files $uri $uri/ /index.html;
}

location ^~ /api/ {
proxy_pass http://localhost:9080/; #后端服务
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}

server {
listen 80;
listen 443 ssl;
# 强制跳转https
if ($scheme = http ) {
return 301 https://$host$request_uri;
}
server_name sub2.domain.com;
#证书文件名称
ssl_certificate sub2.domain.com_bundle.crt;
#私钥文件名称
ssl_certificate_key sub2.domain.com.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1.2 TLSv1.3;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;

location / {
root /home/app/static; #前端地址static
autoindex on; # 开启浏览器功能
}
}
}
XML

Nginx通过域名和子域名转发至多服务+配置全站HTTPS+强制HTTPS
https://leehoward.cn/2022/02/21/Nginx通过域名和子域名转发至多服务+配置全站HTTPS+强制HTTPS/
作者
lihao
发布于
2022年2月22日
许可协议