Home >Operation and Maintenance >Nginx >How to realize reverse proxy and dynamic separation of nginx+tomcat

How to realize reverse proxy and dynamic separation of nginx+tomcat

王林
王林forward
2023-06-04 09:25:031439browse

1. Install nginx and access static resources

After successful installation, start nginx, enter http://localhost/ in the browser and the welcome page will appear.

In Create the directory static\test1\js\common under nginx and put jquery.xx.js in it.

ps: static represents the main directory of static files, test1 represents the project name under tomcat, this machine uses jquery-1.12.1.min.js

to modify the configuration file nginx.conf, add the following Configuration:

#Regularly matches requests starting with static, such as /test1/static/js...

location ^~/test1/static/ {
alias static/test1/ ; #alias will discard the path configured after location; when using alias, be sure to add /
after the directory name }

Restart nginx and enter http://localhost/test1/static/ in the browser js/common/jquery-1.12.1.min.js can be accessed successfully.

2. Build java web project

Install jdk, tomcat, myeclipse or idea, etc., and create web project test1.

Set the test1 welcome page to index.jsp and deploy it under tomcat. The port number is 8080.

The browser can enter http://localhost:8080/test1/ to access it successfully.

3. Introduce static resources under nginx

Introduce nginx’s jquery file under index.jsp:

src="http:// localhost/test1/static/js/common/jquery-1.12.1.min.js"

Also write the jquery test script as follows:

  <script type="text/javascript">
    //jquery语法,能够成功alert说明jquery引入成功
    $(function() {
    alert("测试动静分离成功!");
    })
   </script>

Configure the reverse proxy of the test1 project under nginx , modify nginx.conf and add the following configuration:

#test1 project request --> Reverse proxy to 8080, except for ^~/test1/static/request
location /test1/ {
proxy_pass http://localhost:8080;
}

4. Test the reverse proxy and dynamic and static separation

Restart nginx, restart tomcat!

Enter http://localhost/test1/ in the browser, and the request will be forwarded by nginx to test1 under tomcat on port 8080.

If everything is ok, the browser will return the output of index.jsp, load the jquery file under nginx, and pop up a prompt box.

Local screenshot:

                                                                              

The above is the detailed content of How to realize reverse proxy and dynamic separation of nginx+tomcat. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete