Nginx 1.10 整合 Spring CLoud GateWay 2.1.3 搭建域名访问环境

  1. 前提
    0.1 Spring Boot + Spring Cloud 项目位于本地(win10)
    0.2 Nginx运行在本地虚拟机Virtual Box 的 Docker 上
    0.3 SwitchHosts 将本地域名gulimall.com与本机ip地址映射
    在这里插入图片描述

0.4 本机ip地址由cmd命令行ipconfig得到, 必须确保该本地ip与虚拟机ip互相ping得通

注意:配置文件中 ##开头的注释是自定义的注释, 其下一行是自定义编辑的配置,

  1. Nginx 配置

1.1 编辑 Nginx的总配置文件 nginx.conf

找到 nginx.conf
/mydata/nginx/conf/nginx.conf
在其http块中添加上游服务器upstream配置,其他不变

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

	## gulimall是这个upstream的名称
    upstream gulimall{
      ## 192.168.1.21是本机ip, 88是Spring CLoud Gateway的端口号
      server 192.168.1.21:88;
    }

    include /etc/nginx/conf.d/*.conf;
}

1.2 编辑Nginx的 server配置文件gulimall.conf

server {
    listen       80;
    ## 本server的名称是gulimall.com
    server_name  gulimall.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
       ## 让Nginx的header携带host信息
       proxy_set_header Host $host;
       ## 这里的gulimall是Nginx主配置文件中upstream的名称, 表示反向代理该upstream
       proxy_pass http://gulimall;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  1. Spring Cloud Gateway 配置
    application.yml 配置路由 gulimall_host_route
spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: https://www.baidu.com
          predicates:
            - Query=url,baidu

        - id: qq_route
          uri: https://www.qq.com
          predicates:
            - Query=url,qq

        - id: product_route
          uri: lb://gulimall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: third_party_route
          uri: lb://gulimall-third-party
          predicates:
            - Path=/api/thirdparty/**
          filters:
            - RewritePath=/api/thirdparty/(?<segment>.*),/$\{segment}

        - id: member_route
          uri: lb://gulimall-member
          predicates:
            - Path=/api/member/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: ware_route
          uri: lb://gulimall-ware
          predicates:
            - Path=/api/ware/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}
# 精确的路由应该被优先处理,所以放在更高处
        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:  # 这段过滤器和验证码有关,api内容缓存了/renren-fast,还得注意/renren-fast也注册到nacos中
            - RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}
		## uri, 
		##		lb是负载均衡缩写, 
		##		gulimall-product是一个微服务模块的名称, 注意这里如果含有下划线如写成gulimall_product会报错
		## Host,
		##		**.gulimall.com 表示任意以gulimall.com为后缀的三级域名 将会路由到uri指示的微服务模块(此处为gulimall-product)
		##		测试以下网址均可正常访问
		##			gulimall.com
		##			gulimall.com/api/product/attrattrgrouprelation/list
        - id: gulimall_host_route
          uri: lb://gulimall-product
          predicates:
            - Host=**.gulimall.com,gulimall.com
  1. 流程总结
    以访问gulimall.com为例
    浏览器输入gulimall.com回车
    -> SwitchHosts 将 gulimall.com 映射成 192.168.56.10 # 虚拟机ip, 默认端口80, 访问到Nginx #访问gulimall.com就是访问192.168.56.10同时携带"gulimall.com"这个字符串, 找Nginx分析这个串
    -> 1. Nginx根据主配置文件 nginx.conf 知道 名为gulimall的 上游服务器upstream 配置了某个 ip:port (我们所设定的Spring Cloud Gateway 的地址) 2. Nginx根据子配置文件gulimall.conf 知道 将名为 gulimall.com的 server 反向代理 proxy_pass指向的 gulimall(即上游服务器)
    -> Nginx会携带着"gulimall.com"这个host名称信息, 代理访问 这个上游服务器配置的地址, 即 Spring Cloud Gateway的地址
    -> Spring Cloud Gateway 根据配置文件application.yml 中的路由配置找到 名为 gulimall.com的Host, 将请求转到 uri: lb://gulimall-product
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值