day85-mall business-nginx-building domain name access environment 2 (load balancing to gateway)

Previously implemented load balancing from nginx to services, but there are so many services, so it is very troublesome to make it, and there is no unified processing done by the gateway.

So this time, nginx load balance is implemented to the gateway, and then the gateway is routed to each service

1. Refer to relevant official documents

 

It can be seen that there are two steps, 1. Configure the upstream server 2. Configure the proxy

2. In the overall configuration file of nginx, configure the upstream server

Here we configure the gateway

[root@10 ~]# cd /
[root@10 /]# ls
bin  boot  dev  -e  etc  home  lib  lib64  media  mnt  mydata  opt  proc  root  run  sbin  srv  swapfile  sys  tmp  usr  -v  vagrant  var
[root@10 /]# cd mydata/nginx/conf/
[root@10 conf]# ls
conf.d  fastcgi_params  koi-utf  koi-win  mime.types  modules  nginx.conf  scgi_params  uwsgi_params  win-utf
[root@10 conf]# vi nginx.conf

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 gulimall{
        server  192.168.56.1:88;
        }
    include /etc/nginx/conf.d/*.conf;
"nginx.conf" 35L, 694C written

I added the paragraph above

It is the gateway of the machine. The real project may have multiple gateways. At that time, if you configure multiple gateways, it will be finished.

 2. Proxy configuration

Add the previously configured agent to the commodity service

Change to proxy to the upstream server configured above

[root@10 conf.d]# ls
default.conf  gulimall.conf
[root@10 conf.d]# vi gulimall.conf

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;
    }

    #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;
"gulimall.conf" 44L, 1061C written
[root@10 conf.d]# docker restart nginx
nginx
[root@10 conf.d]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED       STATUS         PORTS                                            NAMES
85e5c45a30da   nginx:1.10            "nginx -g 'daemon of…"   4 weeks ago   Up 5 seconds   0.0.0.0:80->80/tcp, 443/tcp                      nginx
6919debe7c73   elasticsearch:7.4.2   "/usr/local/bin/dock…"   4 weeks ago   Up 2 hours     0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   elasticsearch
fda06f05a0a0   kibana:7.4.2          "/usr/local/bin/dumb…"   4 weeks ago   Up 2 hours     0.0.0.0:5601->5601/tcp                           kibana
69e789223ef0   redis                 "docker-entrypoint.s…"   4 weeks ago   Up 2 hours     0.0.0.0:6379->6379/tcp                           redis
7c3556ac5cf1   mysql:5.7             "docker-entrypoint.s…"   4 weeks ago   Up 2 hours     0.0.0.0:3306->3306/tcp, 33060/tcp                mysql

3. Configure host routing

After the above configuration, we visit, still report 404, the reason is that the gateway has not configured routing rules

 Different from the previous address routing, here we need to configure the host routing https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.3.RELEASE/single/spring-cloud-gateway.html

 can be seen

So we add the following route to the configuration of the gate module. Remember to put this configuration at the bottom. The specific reason will not be mentioned. Restart

        - id: gulimall_host_route
          uri: lb://gulimall-product
          predicates:
            - Host=**.gulimall.com

 4. Solve the problem of host name loss

After restarting, it is still 404 after access

From the entire request in a certain controller, through the following access instructions, our nginx does load to the gateway, and then the gateway implements routing

 So why we visit gulimall.com or 404? The reason is that when nginx proxies to the gateway, the requested host information will be lost, which is the following stuff

Then we need to add the configuration to the configuration added in gulimall.conf before, don't forget the quotation mark at the end

proxy_set_header Host $host;

Restart nginx after configuration

Visit again, the product homepage appears

 5. Summary

Firstly, the mapping is configured on windows, and gulimall.com is mapped to the virtual machine. At this time, the default port 80 of the virtual machine is accessed, and it is monitored. At this time, nginx will proxy it to the gateway according to the configuration, but it will be lost when proxying to the gateway. A host name header, so we need to add a configuration. The gateway looks like the host is gulimall.com. Since we have a configuration in the routing, we directly route to the home page of the product page.

So far achieved the following

 

Guess you like

Origin blog.csdn.net/JavaCoder_juejue/article/details/112858021