1. 李楠的主页首页
  2. Hexo 迁移

nginx+nginx实现多站点反向代理

配置反向代理服务器A

第一步

新建前端服务器A反向代理配置文件:reverseproxy.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
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
upstream backend {
server localhost:80;
}

server {
listen 80;
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/conf/ssl/www.a.com.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/www.a.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
server_name www.a.com a.com;
location / {
proxy_pass https://backend;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

server {
listen 80;
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/conf/ssl/www.b.com.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/www.b.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
server_name www.b.com b.com;
location / {
proxy_pass https://backend;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

server {
listen 80;
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/conf/ssl/www.c.com.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/www.c.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
server_name www.c.com c.com;
location / {
proxy_pass https://backend;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

server {
listen 80;
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/conf/ssl/www.e.com.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/www.e.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
server_name www.e.com e.com;
location / {
proxy_pass https://backend;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 80;
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/conf/ssl/www.f.com.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/www.f.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
server_name www.f.com f.com;
location / {
proxy_pass https://backend;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

将上方 localhost:80 替换成你服务器 ip:端口号 , a.com b.com c.com d.com e.com f.com 分别替换成你的域名

第二步

reverseproxy.conf 上传至服务器 /usr/local/nginx/conf/vhost/ 目录下

第三步

编辑nginx配置文件 /usr/local/nginx/conf/nginx.conf ,在 http{} 内添加下边一行:

1
vhost/*.conf;

反向代理服务器A配置完毕

多站点服务器B配置

nginx正常部署多个站点

站点域名解析

解析域名至反向代理服务器A

测试

ping域名 www.a.com www.b.com www.c.com www.d.com www.e.com www.f.com 查看ip是否是代理服务器A的IP,如果是,那就证明配置成功。如果不是,请对照文章检查配置文件。

本文作者:Drixn

本文链接:https://www.drixn.com/78.html

版权申明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

发表评论

电子邮件地址不会被公开。 必填项已用*标注