Nginx+Windows搭建域名访问环境

配置hosts文件

在下面路径下找到hosts文件

C:\Windows\System32\drivers\etc

在文件最下方配置 “目标IP地址” “本机输入的想跳转到目标IP地址的域名”
192.168.205.122 www.niubi.com
配置完后,当你本地输入www.niubi.com,就会跳转到指定IP地址192.168.205.122

配置目标IP地址的nginx

  • 在nginx.conf文件配置上游服务器(以网关为例)
http{ 
	**********************
    upstream 服务器名称{
        server 服务器所在IP地址(即本地的IP地址):端口;
        }
}

  • 在nginx.conf文件配置server
server {
    listen       80;
    server_name  第一步配置的域名(www.niubi.com);

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

    location / {
        proxy_set_header Host $host;//此处设置是为了防止nginx代理给上游服务器时,会丢失host请求信息
        proxy_pass http://上游服务器名称;
}

配置上游服务器(网关)

spring:
  cloud:
    gateway:
      routes:
          - id: 唯一id
          uri: lb://负载均衡到哪个位置,写注册中心的服务名称
          predicates:
               - Host=**.第一步的域名

配置完以上,当你输入www.niubi.com时,你就可以访问到网关所负载均衡到的服务。

流程

  • 本地访问指定域名,跳转到指定IP地址
  • 由于nginx的80端口在监听,当监听到域名(server_name )为指定域名时,(location )则进行跳转
  • (location )跳转到上游服务器
  • 上游服务器又代理给了本地的网关
  • 网关根据指定的断言规则负载均衡到指定服务

猜你喜欢

转载自blog.csdn.net/fighting32/article/details/107730721
今日推荐