谷粒商城--nginx--高级篇笔记四

谷粒商城–nginx–高级篇笔记四

1. nginx搭建域名访问 (反向代理)

1.1 动静分离

image-20211027173750215

1.2 正向代理与反向代理

  • 正向代理隐藏客户端
  • 反向代理隐藏服务端

image-20211027174337185

1.3 nginx与windows搭建域名访问环境

为什么能够通过修改host文件来实现域名访问?

众所周知域名解析是通过dns来解析的,但是在通过网络的dns解析之前需要找到本机的host文件查询是否有对应的域名映射,如果有就通过映射的域名地址访问,如果没有再访问dns

1.3.1 通过改host文件的方法就可以实现

host文件地址C:\Windows\System32\drivers\etc

image-20211027180003586

也可以使用工具SwitchHosts

注意:启动SwitchHosts必须以管理员身份启动

image-20211027175808170

1.3.2 配置host

新建本地方案,IP地址为虚拟机地址

192.168.157.128 gulimall.com

image-20211027182343822

1.3.3 域名测试

之前虚拟机配置的nginx的index.html页面显示出来了,域名访问配置成功

image-20211027182454167

1.1.4 nginx搭建域名访问(nginx转发网关)

1.1.4.1 实现

image-20211029112224117

1.1.4.1 nginx配置文件结构

image-20211029112527346

image-20211029112737733

nginx.config文件对应的http块

image-20211029113416553

conf.d下的default.conf对应真实的server块

image-20211029113607705

1.1.4.2 http块配置上游服务器

image-20211031173925935

http {
    
    
    upstream gulimall{
    
    
      server 192.168.157.128:88      #配置网关为上游服务器
    }
1.1.4.3 server块配置域名监听并转发

image-20211031173828419

server {
    
    
    listen       80;
    server_name  gulimall.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    
	#配置域名转发到上游服务器
    location / {
    
    
        proxy_pass http://gulimall;
    }

#下面配置为默认配置
1.1.4.4 网关服务配置

gulimall-gateway/src/main/resources/application.yml

- id: gulimall_host_route
  uri: lb://gulimall-product
  predicates:
    - Host=gulimall.com,item.gulimall.com
1.1.4.5 nginx反向代理host信息丢失问题

解决 server块新增proxy_set_header Host $host;配置

image-20211031174503308

重启nginx 访问gulimall.com

image-20211031174550705

猜你喜欢

转载自blog.csdn.net/qq_31745863/article/details/121331734