背景:申请了一个域名,计划是用这个域名部署三个不同的服务在同一台服务器上,我通过一级、二级域名来区分不同的服务。
如:a.com 对应8080这个服务,b.a.com 对应8081这个服务, c.a.com对应8082这个服务。
首先应该在DNS解析器中配置a.com,b.a.com, c.a.com这三个域名的解析,然后通过nginx转发。
根据域名配置了三个转发:
http://www.ha.com 前后端分离,静态页面放在web/rest目录下,后台请求根据请求路径转发到http://localhost:8080
http://images.ha.com 图片服务器
https://admin.ha.com 转发请求到https://localhost:8081
直接上配置:
worker_processes 4; #处理器个数
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#gzip on;
server {
listen 80;
server_name www.ha.com; #根据域名拦截http服务
access_log logs/portal.access.log; #设置访问日志存储位置和名称
location / {
root web/rest; #静态页面
index index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /404.html;
location = /50x.html {
root html;
}
location ^~ /api/ {
proxy_pass http://localhost:8080; #根据请求路径转发给后台服务(将包含/api/的请求转发)
}
add_header Access-Control-Allow-Origin *; #允许跨域
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}
server {
listen 80; #根据域名拦截http服务
server_name admin.ha.com;
location / {
root html;
index bad.html; #拦截所有的http请求到html/bad.html,即不允许http访问,只能通过https访问
}
}
server {
listen 443 ssl; #根据域名拦截https服务
server_name admin.ha.com;
access_log logs/admin.access.log;
ssl_certificate ../ssl/test.crt;
ssl_certificate_key ../ssl/test.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass https://localhost:8081;
}
}
server {
listen 80;
server_name images.ha.com; #根据域名拦截https服务
access_log logs/images.access.log;
location / {
root E:/data/; #图片存放位置
}
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}
}