背景
只有一台云服务器,部署了自己写的后端管理系统,又需要部署下自己的个人博客平台,但是只有一个域名,想要合理的利用下二级域名。
域名配置
首先需要配置域名解析,讲需要添加的二级域名添加到现有的解析记录里:
- 各个平台操作类似,我的域名在西部数据买的,直接配置就好,截图显示如下:
- 服务器上配置安全策略打开对应端口:
服务器配置
- 阿里云安全组策略配置:
2.服务器安装nginx,已安装略过
Nginx配置
- nginx现有配置:
在conf下新增 hosts文件夹。【可自定义】
#user nobody; worker_processes 1; error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; server { listen 80; server_name mount.pub; rewrite ^(.*)$ https://${server_name}$1 permanent; error_page 500 502 503 504 /50x.html; root /var/blog/dist/; } server { listen 443 ssl; server_name mount.pub; ssl on; # https证书 ssl_certificate /usr/local/nginx/conf/cert/1_www.xxx.pub_bundle.crt; ssl_certificate_key /usr/local/nginx/conf/cert/2_www.xxx.pub.key; ssl_session_cache shared:SSL:1m; 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; location / { root /var/blog/dist; index index.html index.htm; # 避免中文乱码 charset utf-8,gbk; } location /app/ { proxy_pass https://127.0.0.1:8081; client_max_body_size 16m; client_body_buffer_size 128k; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Port $server_port; #跨域访问设置 add_header Access-Control-Allow-Origin *; } } #二级域名 配置文件夹 include hosts/*.conf; }
- 在hosts下新增二级域名的配置文件
例如我需要加的:admin.mount.pub.conf
配置如下:
server {
default_type 'text/html';
charset utf-8;
listen 80;
autoindex off;
server_name admin.mount.pub;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
location / {
index index.html index.htm;
add_header Access-Control-Allow-Origin *;
#二级域名对应的文件路径
root /var/www/dist/;
}
}`
- nginx配置生效
reload配置
# /usr/local/nginx/ 是我的nginx安装路径
/usr/local/nginx/sbin/nginx -s reload
页面访问生效
效果如下