How to reverse proxy Nginx to Tomcat server

WBOY
Release: 2023-05-17 21:01:04
forward
1510 people have browsed it

In actual production, tomcat servers are generally not used alone in projects. nginx performs better in responding to static resources. In addition, since nginx is a server specifically used for reverse proxy, it is easy to implement java requests. Forwarded to the backend and handed over to the tomcat container for processing, and itself used to process static resources

In nginx, a server {} block is often used to configure a relatively large project, usually all configurations for a domain name , there are generally multiple locations in a server block to define multiple request rules, such as domain name and root directory configuration, static resource support, php fastcgi request, url rewriting, error page configuration and other configurations, so nginx proxy tomcat and front The forwarding of php-fpm is the same, that is, the request is forwarded to the back-end dynamic module to process the request

The following is a simple location local rule configured in the server to forward the specified nginx request to the tomcat container

Assuming that tomcat is running normally and the port number is 8080, first use vim to edit the nginx.conf configuration file, for example: vim /usr/local/nginx/nginx.conf

Then at the end of the server {} block, Add the following content:

location ^~ /tomcat/ {
      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;
    }
Copy after login

How to reverse proxy Nginx to Tomcat server

Because php-fpm support is configured above, the /tomcat/ request is used here to determine whether to forward to the tomcat container, the previous ^~ It matches any domain name or IP address part, so when accessing a request like http://ip/tomcat, the tomcat container will work, and the effect is the same as accessing http://ip:8080, including under the container The projects are all exactly the same

After saving, reload the nginx configuration file: /usr/local/nginx/nginx -s reload

 How to reverse proxy Nginx to Tomcat server

Of course You can configure multiple locations here to proxy to tomcat on multiple servers. You can also freely configure multiple server {} blocks to proxy to multiple servers. All of these can be configured flexibly according to your own needs. In addition, By configuring static resource rules and other configurations, you can achieve powerful functions, such as configuring load balancing, realizing high concurrent access to the website and resource sharing of image servers, etc.

The above is the detailed content of How to reverse proxy Nginx to Tomcat server. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template