需求
项目需要两个域名使用同一个服务器,配置多个项目文件。
所谓虚拟主机,是说通过几个不同的url地址,都能到达nginx环境,只不过针对不同的url,处理的逻辑不同。
nginx支持虚拟主机,但是浏览器等客户端不知道,所以虚拟主机的几个地址,应该是都指向nginx所在的ip地址,虚拟主机功能才能正常。
规划
域名a以及域名b的http默认80端口配置官网;
域名a以及域名b的https默认443端口配置接口;
域名a语句域名b的后台管理系统82以及83端口分别配置后台管理系统,然后dns解析在配置隐形url。
配置
官网的两个配置
server {
listen 80;
server_name a.com;
location / {
root /www/website-novel;
index index.html index.htm;
}
}
server {
listen 80;
server_name b.com;
location / {
root /www/website-onlinenovel;
index index.html index.htm;
}
}
接口https的配置
server {
listen 8089;
listen 443 ssl;
server_name a.com:8089;
charset utf8;
ssl_certificate /usr/local/nginx/conf/cert/a.com.pem; # pem文件的路径
ssl_certificate_key /usr/local/nginx/conf/cert/a.com.key; # key文件的路径
# ssl验证相关配置
ssl_session_timeout 5m; #缓存有效期
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议
ssl_prefer_server_ciphers on; #使用服务器端的首选算法
access_log /var/log/nginx/novel_access.log;
error_log /var/log/nginx/novel_error.log;
location / {
#include uwsgi_params;
include /usr/local/nginx/conf/uwsgi_params;
uwsgi_pass 127.0.0.1:8888;
uwsgi_read_timeout 2;
index index.html index.htm;
}
location /static {
alias /www/novel/static;
}
}
server {
listen 8087;
listen 443 ssl;
server_name b.com;
# 域名,多个以空格分开
ssl_certificate /usr/local/nginx/conf/cert/b.com.pem; # pem文件的路径
ssl_certificate_key /usr/local/nginx/conf/cert/b.com.key; # key文件的路径
# ssl验证相关配置
ssl_session_timeout 5m; #缓存有效期
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议
ssl_prefer_server_ciphers on; #使用服务器端的首选算法
charset utf8;
access_log /var/log/nginx/novel_access.log;
error_log /var/log/nginx/novel_error.log;
location / {
#include uwsgi_params;
include /usr/local/nginx/conf/uwsgi_params;
uwsgi_pass 127.0.0.1:8889;
uwsgi_read_timeout 2;
index index.html index.htm;
}
location /static {
alias /www/online_novel/static;
}
}
后台管理系统配置
server {
listen 82;
server_name novel.a.com;
location / {
root /www/inovel/;
index index.html index.html;
}
location /admin-api {
proxy_pass http:
}
}
server {
listen 83;
server_name novel.b.com;
location / {
root /www/online_inovel/;
index index.html index.html;
}
location /admin-api {
proxy_pass http:
}
}
最后在两个域名的dns解析分别都配置@解析到该服务器ip即可
最后效果
a官网:
http:
a接口:
https:
a后台管理系统:
http:
b官网:
http:
b接口:
https:
b后台管理系统:
http: