【大型电商项目开发】商城业务-nginx搭建域名访问-37

导读:本篇文章讲解 【大型电商项目开发】商城业务-nginx搭建域名访问-37,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一:Nginx+Windows搭建域名访问环境

在这里插入图片描述

1.正向代理和反向代理

正向代理:如科学上网,隐藏客户端信息
反向代理:屏蔽内网服务器信息,负载均衡访问。先去访问nginx,再通过nginx将信息传递给后台服务器。保护后台服务器
在这里插入图片描述
1)host文件存放目录:C:\Windows\System32\drivers\etc\hosts
2)可以在本机的host文件当中设置域名进行访问。当本机没有配置的时候,才会通过dns解析,然后访问公网ip

2.开启服务器的nginx,设置nginx开机自启

docker update nginx --restart=always

3.让nginx进行反向代理

1)让nginx帮我们进行反向代理,所有来自gulimail.com的请求,都转到商品服务
在这里插入图片描述
2)进入conf.d配置文件

cd /mydata/nginx/conf/conf.d

注:conf.d的配置会同步到nginx.conf的配置中
3)复制gulimail.conf的配置文件作为商城系统的配置

cp default.conf gulimail.conf

4)编写gulimail.conf配置

vi gulimail.conf
server {
    listen       81;
    server_name  gulimail.com;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    location / {
       proxy_pass http://192.168.0.1: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;
    
}
  • server_name:前台页面输入的网址,会和管理员请求接口的host进行匹配
  • proxy_pass:代理到服务器的端口

二:nginx负载均衡到网关

1.配置nginx.conf文件/http块

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;
    upstream gulimail{
     server 192.168.0.1:88;
    }
    include /etc/nginx/conf.d/*.conf;
}
  • 通过upstream将其跳转到网关

2.配置gulimail.conf/server块

  location / {
       proxy_pass http://gulimail;
    }
  • 通过proxy_pass将其负载均衡到上游服务器的upstream中

3.配置网关

在gulimail-gateway的application.yml配置中,添加网关路由

   - id: gulimail_host_route
          uri: lb://gulimail_product
          predicates:
            - Host=**.gulimail.com,gulimail.com

注:nginx代理给网关的时候,会丢失请求的host信息

4.设置nginx的请求头信息

    location / {
       proxy_set_header Host $host;
       proxy_pass http://gulimail;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/84111.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!