Operation and maintenance - Nginx reverse proxy tomcat
仅有的幸福
仅有的幸福 2017-05-16 17:13:23
0
4
518

There are two jsp applications, the local access addresses are as follows

http://127.0.0.1:8080/app1
http://127.0.0.1:8080/app2

Access to http://domain.com/app1 and http://domain.com/app2 can be achieved through the following configuration

server {
    listen    80;
    server_name    domain.com;
    charset    utf-8;
    location /{
        proxy_pass http://127.0.0.1:8080/;
        proxy_redirect  off;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Now I want to use two domain names to access these two applications
Visit http://127.0.0.1:8080/app1 through http://app1.domain.com
Visit http://app2 .domain.com to access http://127.0.0.1:8080/app2
Configure as follows

server {
    listen    80;
    server_name  app1.domain.com;
    charset utf-8;
    location /{
        proxy_pass http://127.0.0.1:8080/app1;
        proxy_redirect  off;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

server {
    listen    80;
    server_name  app2.domain.com;
    charset utf-8;
    location /{
        proxy_pass http://127.0.0.1:8080/app2;
        proxy_redirect  off;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

The result can only display the homepage, and static resources cannot be loaded

#我感觉应该这么写可是有语法错误
if ($uri ^/$){proxy_pass http://127.0.0.1:8080/app1;}
if ($uri ^/app1){proxy_pass http://127.0.0.1:8080/$request_uri;}

So how should it be configured?

仅有的幸福
仅有的幸福

reply all(4)
迷茫

What does Unable to load mean? Is it 502? Or is the static path in jsp wrong?

迷茫

In fact, you can just base it on the above. . Then pseudo-static forwarding based on the second-level domain name will be enough. . I won’t write down the details. The principle is like that. It takes several tries to determine the writing method.

The general idea is to forward all .domain.com/ to http://127.0.0.1:8080/$1/$2

阿神

Thanks for the invitation!
I think it is necessary to separate dynamic and static, and let js, css and images be processed by ngixn instead of tomcat. The jsp request is forwarded to tomcat for processing.


location ~ .*\.(jpg|js|css)$ 
{ 
root /home/www/image/;
}
世界只因有你

Dear. . . If you haven't written root yet, how can nginx give you the proxy file? That's fine upstairs.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template