139-140、商城业务-nginx-搭建域名访问环境一(反向代理配置)、搭建域名访问环境二(负载均衡到网关)

139、商城业务-nginx-搭建域名访问环境一(反向代理配置)

1)修改本地的C:\Windows\System32\drivers\etc\hosts文件,添加域名映射规则:

ip 域名

192.168.56.10 gulimall.com

2) Nginx+Windows搭建域名访问环境

在这里插入图片描述

3) 正向代理与反向代理

在这里插入图片描述

4)Nginx配置文件

在这里插入图片描述
/mydata/nginx/conf/nginx.conf


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;
    include /etc/nginx/conf.d/*.conf;
}

注意这里的“include /etc/nginx/conf.d/.conf;”,它是将“/etc/nginx/conf.d/.conf”目录下的所有配置文件包含到nginx.conf文件中。下面是该文件的内容:
/etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/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;
    #}
}

在Nginx上配置代理,使得所有到gulimall.com的请求,都转到gulimall-product服务。
现在需要明确部署情况,我们的nginx部署在192.168.56.10上,而且是以docker容器的方式部署的,部署时将本机的/mydata/nginx/conf/挂载到了nginx容器的“/etc/nginx ”目录,gulimall-product服务部署在192.168.3.3上。
在192.168.56.10上创建“gulimall.conf”文件:

touch /mydata/nginx/conf/conf.d/gulimall.conf

注意:gulimall.conf的位置是映射后的路径,对应于Nginx容器的/etc/nginx/conf.d/gulimall.conf
内容如下:

server {
    listen       80;
    server_name  gulimall.com;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
       proxy_pass http://192.168.43.43:10000}
 }

修改配置文件完毕后,重启nginx。
如果启动失败可以查看日志:docker logs nginx

5)启动流程

整个的数据流是这样的:

  1. 浏览器请求gulimall.com,在本机被解析为192.168.56.10
  2. 192.168.56.10的80端口接收到请求后,解析请求头求得host,对应“gulimall.conf”配置文件中的server_name ,在根据“gulimall.conf”中配置的规则,将请求转到“http://192.168.3.3:10000”,然后该服务响应请求,返回响应结果。

问题:
但是这样做还是有些不够完美,“gulimall-product”可能部署在多台服务器上,通常请求都会被负载到不同的服务器上,这里我们直接指定一台设备的方式,显然不合适
可以配置网关的地址,通过网关可以路由的到对应服务

140、商城业务-nginx-搭建域名访问环境二(负载均衡到网关)

1)关于Nginx的负载均衡

nginx官网
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2)配置负载均衡:

  1. 修改“/mydata/nginx/conf/nginx.conf”,添加如下内容
   upstream gulimall {
       server 192.168.43.43:88;
   }
  1. 修改“/mydata/nginx/conf/conf.d/gulimall.conf”文件
    location / {
      proxy_pass   http://gulimall;
    }
  1. 在“gulimall-gateway”添加Host路由规则:
- id: gulimall_host_route
  uri: lb://gulimall-product
  predicates:
    - Host=**.gulimall.com

注意:这个路由规则,一定要放置到最后,否则会优先进行Host匹配,导致其他路由规则失效。
4. 配置完成后,重启Nginx,再次访问
再次访问的时候,返回404状态码

原因分析:Nginx代理给网关的时候,会丢失请求头的很多信息,如HOST信息,Cookie等。

解决方法:需要修改Nginx的路径映射规则,加上“ proxy_set_header Host       $host;
  1. 修改“gulimall.conf”文件
location / {
      proxy_set_header Host  $host;
      proxy_pass   http://gulimall;
}
  1. 重启Nginx容器,再次访问成功

3)问题

4)spring-cloud官网

找到Gateway
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值