七十一.搭建域名访问环境

一般来说,客户访问网站都是域名来访问的,所以接下来通过修改本地的hosts文件模拟实现域名访问效果。

先修改hosts文件,配置上域名对应的ip,注意这里的ip不是本机ip,而是安装nginx等软件的服务器地址,如下:
在这里插入图片描述

进入nginx的conf文件夹,然后进入conf.d文件夹中,然后把里面的default.conf配置文件copy一份,文件名为webshop.conf

在这里插入图片描述
修改webshop.conf文件中的配置,具体信息如下:

server {
    listen       80;
    #在这里配置上访问用的域名
    server_name  webshop.com;

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

    location / {
        #nginx代理给网关时,会丢失一些信息,这里先补充一些请求头,方便之后的网关路由规则匹配及转发请求 
        proxy_set_header Host $host;
        #这里会动态找名为webshop的上游服务器,然后负载均衡请求
        proxy_pass http://webshop;
    }

    #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.conf文件,增加上游服务器配置,如下:
在这里插入图片描述

第一个红框里配置的是网关地址,由于本机只有一台,所以先写一个,如果之后网关也集群了,那么加上相关地址,网关之间也能实现负载均衡。

第二个红框是为了解释下为什么新建webshop.conf配置文件可以生效,红框中的配置项的作用是把/etc/nginx/conf.d文件夹下所有以.conf结尾的文件的配置内容都包含进nginx.conf文件中,而新建的webshop.conf配置文件正好符合该规则。

接着修改下webshop-gateway项目的application.yml文件,添加下新的路由规则,该路由优先级放到最低,如下:

        - id: webshop_host_router
          uri: lb://webshop-product
          predicates:
            - Host=**.webshop.com,webshop.com

在这里插入图片描述
之后重启nginx服务以及webshop-gateway服务,直接访问http://webshop.com,就可以跳转到首页了。

猜你喜欢

转载自blog.csdn.net/weixin_38106322/article/details/108436670