配置 nginx 反向代理

前提:
nginx 以 docker 的形式,安装在虚拟机里,虚拟机 ip 是 192.168.94.137
商品服务 在192.168.94.135 的 11000 端口启动
网关服务 在192.168.94.135 的 13000 端口启动

宿主机配置域名访问nginx

  1. 用 SwitchHosts 配置域名映射
    192.168.94.137 www.mall.com
    
  2. 访问 http://www.mall.com,成功访问到 nginx
    在这里插入图片描述

配置nginx反向代理

  1. nginx.conf 配置文件解析

    # 全局块的一些配置,配置nginx的一些全局指令,如用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成的worker process数等
    user  nginx;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    
    # 事件块的一些配置,配置影响 nginx 服务器或用户的网络连接。
    events {
        worker_connections  1024;
    }
    
    # http 块,可以嵌套多个 server,配置dialing,缓存,日志定义等绝大多数功能和第三方模块的配置
    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;
    
        # 一些配置,可以放在配置文件中,然后以 include 指令引入
        include /etc/nginx/conf.d/*.conf;
    }
    

    如包含的配置文件 /mydata/nginx/conf/conf.d/default.conf

    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/log/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        #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;
        #}
    }
    
  2. 在nginx配置,通过域名,访问到后端 product 服务 http://192.168.94.135:11000,该服务存在于另一台虚拟机上。

    # 将配置文件复制一份
    cp /mydata/nginx/conf/conf.d/default.conf /mydata/nginx/conf/conf.d/mall.conf
    vim mall.conf
    

    在这里插入图片描述

  3. 分布式情况下,如果让nginx反向代理到某个微服务,可能有局限性。若微服务部署多份的情况下,没办法做到负载均衡。若微服务改变端口,也需要修改配置文件。这种情况下,我们可以让 nginx 代理到网关。网关统一路由到相应的微服务。
    参考文档 https://nginx.org/en/docs/http/load_balancing.html

    http {
        upstream myapp1 {
            server srv1.example.com;
            server srv2.example.com;
            server srv3.example.com;
        }
    
        server {
            listen 80;
    
            location / {
                proxy_pass http://myapp1;
            }
        }
    }
    

    网关的配置,可以参考上面的配置来配置。
    修改 nginx.conf 配置文件 和 mall.conf 配置文件
    在这里插入图片描述

  4. 配置 nginx 访问网关,网关转发请求到商品服务

    1. 配置网关。在网关的微服务中配置如下:
      # nginx 到 网关的路由规则
      - id: mall_host_route
        uri: lb://mall-product
        predicates:
          - Host=**.mall.com  #对于nginx转发到网关请求,符合二级域名是mall.com的,都转交给mall-product商品微服务
      
    2. 配置了第一步,还需在nginx中配置如下,否则访问不到:
      在这里插入图片描述
      原因:是因为经过网关的请求,网关会将请求头等相关很多配置信息丢弃。所以请求到了网关的时候,由于丢弃请求头,网关没办法根据请求,路由到相应的微服务。
      如果不做配置,如下圈红的信息会被 nginx 丢弃,网关这拿不到这个信息,无法进行路由。
      在这里插入图片描述

网关路由规则解析

  1. 网关路由规则

    spring:
      application:
        name: mall-gateway
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
        gateway:
          routes:
            - id: product_route
              uri: lb://mall-product
              predicates:
                - Path=/api/product/**
              filters:
                - RewritePath=/api/(?<segment>.*),/$\{segment}
            # nginx 到 网关的路由规则
            - id: mall_host_route
              uri: lb://mall-product
              predicates:
                - Host=**.mall.com
    
  2. 分析:

    1. 当我们直接访问 http://www.mall.com/,由于之前在nginx做了配置,请求来到网关,网关匹配到如下配置,将我们带到了商品服务的首页 index.html页面。
      在这里插入图片描述
    2. 当我们访问 http://www.mall.com/api/product/brand/list 这样一个 api时候,也能拿到数据。原因是我们配置了下面这段路由规则:
      在这里插入图片描述
      这个请求,经过nginx后会转成 http://192.168.94.13000/api/product/brand/list,接着经过网关的时候,遇到的以上配置的路由规则,则请求被转换成 http://mall-product/product/brand/list,最终访问到了商品服务。
  3. 若上面的两条路由规则,变换顺序。则 http://www.mall.com/api/product/brand/list 请求会变成404,原因是:没有路径重写,到了商品微服务,找不到相应的api。

nginx 配置动静分离

/static 路径下的请求,路由到 /usr/share/nginx/html,这是静态资源放的目录

    location /static {
       root   /usr/share/nginx/html;
    }

其它请求 路由到网关

    location / {
       proxy_set_header Host $host;
       proxy_pass http://mall;
    }

完整配置

server {
    listen       80;
    server_name  www.mall.com;

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

    location /static {
       root   /usr/share/nginx/html;
    }

    location / {
       proxy_set_header Host $host;
       proxy_pass http://mall;
    }

    #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;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值