Home  >  Q&A  >  body text

Operation and maintenance - Nginx reverse proxy tomcat

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?

仅有的幸福仅有的幸福2714 days ago458

reply all(4)I'll reply

  • 迷茫

    迷茫2017-05-16 17:15:23

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

    reply
    0
  • 迷茫

    迷茫2017-05-16 17:15:23

    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

    reply
    0
  • 阿神

    阿神2017-05-16 17:15:23

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

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-16 17:15:23

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

    reply
    0
  • Cancelreply