Nginx reverse proxy stepping on the pit (container mode)

1. Brief introduction

1.1 What is a reverse proxy?

This is very important, a reverse proxy is a proxy server proxying the real server . The client thinks that the proxy server is the real server, so it will send the requested == resource (URL)== to the proxy server.

The proxy server is generally acted by nginx, and the proxy function is completed by the configuration file.

image-20220414221430951

1.2 Look at the picture to understand

The painting is hastily, probably has this meaning

image-20220414221938713

1.3 Summary of errors

Error log:

Full containerization of nginx and tomcat

Proxy two tomcat servers with nginx,

Hand over to tomcat1 when accessing resources with edu

Hand over to tomcat2 when accessing resources with vod

When doing reverse proxy testing, write the complete URL, not a partial path. Because the proxy server appears to the client as the real server! ! !

This time I have a deeper understanding of the reverse proxy, and there is nothing wrong with sticking to it.

At the same time, it is also necessary to ask others for advice, more communication and more thinking can better solve the problem~

Below is the error demo

image-20220414222411746

The correct case

2.1 Start nginx

docker run --name nginx -p 80:80 --link=tomcat:tomcat1 --link=tomcat02:tomca -v /opt/docker-nginx/nginx.conf:/etc/nginx/nginx.conf -v /opt/docker-nginx/log:/var/log/nginx -v /opt/docker-nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -d 313ec0a602bc

2.2 Start tomcat

start one

docker run -it -p 8080:8080 tomcat

docker cp copy files

docker cp container id: /usr/local/tomcat/webapps.dist/*/opt/webapps

Reboot the mounted volume

docker run -it -p 8081:8080 --name tomcat01 -v /opt/webapps:/usr/local/tomcat/webapps tomcat

docker run -it -p 8082:8080 --name tomcat02 -v /opt/webapps:/usr/local/tomcat/webapps tomcat

create files, create resources

[root@VM-16-8-centos vod]# ll
total 4
-rw-r–r-- 1 root root 11 Apr 14 21:42 a.html
[root@VM-16-8-centos vod]# pwd
/opt/tomcat/webapps/vod

[root@VM-16-8-centos edu]# ll
total 4
-rw-r–r-- 1 root root 8 Apr 14 21:26 a.html
[root@VM-16-8-centos edu]# pwd
/opt/tomcat/webapps/edu

image-20220414222221521

2.3 Configure nginx

[root@VM-16-8-centos docker-nginx]# vim nginx.conf 

worker_processes  1;
events {
    
    
    worker_connections  1024;
}
http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
    
    
      listen 80;
      server_name xxx.xxx.xxx.xxx;

      location ~ /edu/ {
    
    
         proxy_pass http://xxx.xxx.xxx.xxx:8081;
      }

      location ~ /vod/ {
    
    
         proxy_pass http://xxx.xxx.xxx.xxx:8082;
      }
    }
}

2.4 Restart all services

docker restart …

2.5 Testing

image-20220414222135749

image-20220414222322111

3. How does the nginx running on the cloud server proxy the local project

Can't!
Either in the cloud,Proxying can only be done if the proxy server IP and the real server IP can communicate with each other! ! !

Guess you like

Origin blog.csdn.net/qq_45714272/article/details/124183925