nginx根据域名转发服务

背景:申请了一个域名,计划是用这个域名部署三个不同的服务在同一台服务器上,我通过一级、二级域名来区分不同的服务。

如: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;
    }
}
 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值