Nginx搭建域名访问环境
正向代理与反向代理
Nginx配置文件
Windows修改host文件
192.168.195.100为虚拟机ip
192.168.195.100 gulimall.com
然后修改Nginx配置文件
server {
listen 80;
server_name gulimall.com;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
proxy_pass http://192.168.195.100:10000;
}
#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;
}
}
Nginx帮我们反向代理,如果请求是来源于gulimall.com的,全部转发到商品服务http://192.168.195.100:10000
但是这样有个弊端,分布式情况下商品服务可能不止一个,不能每次都去改配置文件,因此最好的办法是nginx将请求直接代理给网关,然后再转发给相应的服务
在nginx文件夹的nginx.conf中配置网关地址,这里可以配置多个地址,用来负载均衡
upstream gulimall {
server 192.168.195.88:88;
}
配置转发到http://gulimall
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;
proxy_set_header Host $host;
}
.......
}
配置网关
- id: gulimall_host_route
uri: lb://gulimall-product
predicates:
- Host=**.gulimall.com,gulimall.com
访问guliamall.com通过网关转发到商品服务成功
Nginx直接代理给网关,网关判断:
- 如果是/api/*,装交给对应的服务器
- 如果是妈祖域名,转交给对应的服务
转发效果
访问流程
在windows里面配置了gulimall的虚拟机,访问gulimall.com转到了虚拟机里面192.168.195.100,默认是访问80端口,192.168.195.100:80
192.168.195.100 gulimall.com
虚拟机里面装了nginx,进入到nginx80端口,通过监听80端口nginx把请求转发到windowshttp://gulimall
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;
proxy_set_header Host $host;
}
.......
}
http://gulimall请求转发到网关192.168.195.88:88
upstream gulimall {
server 192.168.195.88:88;
}
然后网关拦截gulimall.com的请求,并转发到商品服务gulimall-product
- id: gulimall_host_route
uri: lb://gulimall-product
predicates:
- Host=**.gulimall.com,gulimall.com