Nginx implements reverse proxy multiple Tomcat services through the virtual host function

need:

Frequently encountered problems: Party A’s server resources are tight, and they want to deploy a website with a formal domain name, and they don’t want to add a port number to the url;

Summary requirements:

One server, one ip, multiple projects, multiple domain names

Solutions:

  1. First log in to the domain name management background. Generally, service providers provide dns services, and map these domain names to this ip in it. 
  2. In this way, external requests will access port 80 of the server.
  3. Use nginx to monitor port 80 and reverse proxy to other ports

Specific steps:

window environment

  1. Install nginx Download address: nginx: download  After downloading, decompress and run the exe, then visit localhost to verify, and the nginx welcome page should be displayed normally.                            

  2. Configure the host file to simulate the dns service for testing, the host location is: C:\WINDOWS\system32\drivers\etc, we add these two lines

     After saving, visit these two domain names to verify whether the settings are valid, and if they are valid, the nginx welcome page will be displayed. (Note that after setting the host file, you need to restart the browser to take effect)

  3. Install tomcat download address , decompress two copies, one rename java, modify port 8080 to 8081, 8005 to 8006, one rename python, modify port 8080 to 8082, 8005 to 8007,

     The method of configuring the tomcat port is here, [ here ], and then start .

  4. Do two projects for testing: in the webapps folder of tomcatjava, create a folder, name it JAVA, create an index.html, in the webapps folder in tomcatpython, create a folder named PYTHON, and create an index. html, the content of the webpage is arbitrary, as long as the two webpages are different. Visit to verify.

  5. To configure nginx, open the nginx root directory conf/nginx.conf in Notepad. Delete the original server{...} part and insert the following code. Save and restart the nginx process.

    	server {  
            listen       80;  
            server_name  *.ilovejava.com;
      
            location / {  
                proxy_pass   http://127.0.0.1:8081/JAVA/;
                index  index.html index.htm;  
            }       
        }  
    	server {  
            listen       80;  
            server_name  *.ilovepython.com;
      
            location / {  
                proxy_pass   http://127.0.0.1:8082/PYTHON/;
                index  index.html index.htm;  
            }       
        } 

  6. Verify that both domain names can be accessed independently!

Linux environment

Take Tencent Cloud centos as an example

1: Same as above, still do a good job of dns mapping

2: The cloud server needs to open the port on the console, see Tencent Cloud API for details

 

3: This is how the cloud server looks like after logging in

 

4: Install nginx, directly type "yum install -y nginx", and then start , nginx installed on the network will automatically configure systemctl, so you can use the command "system start nginx" to start. The welcome page is shown in the figure.

 

 4: Configure nginx, use " rpm -ql nginx " to find the location of the installation file to find nginx.conf, and use vim to open "vim /etc/nginx/nginx.conf". open like this

 

 Press i to start editing, type according to the above window configuration content, and replace the ip with the public network ip of our cloud server. Then press esc to exit, enter: wq to save and exit.

 

Guess you like

Origin blog.csdn.net/pujinhong0412/article/details/120704846