Nginx reverse agent + tomcat (nginx forwarding rules and load balancing)

Keywords: Nginx Tomcat PHP Apache

Sometimes I want to realize reverse proxy through nginx, and map to different tomcat servers according to different URLs, which has achieved the purpose of hanging multiple websites and applications in one server.


1) The simplest is to modify the nginx installation directory nginx.conf File:

Part of the original document was:

 server {
        listen       80;
        server_name  localhost;
    <span class="hljs-comment">#charset koi8-r;</span>

    <span class="hljs-comment">#access_log  logs/host.access.log  main;</span>

    location / {
        root   html;
        index  index.html index.htm;
    }

    <span class="hljs-comment">#error_page  404              /404.html;</span>

    <span class="hljs-comment"># redirect server error pages to the static page /50x.html</span>
    <span class="hljs-comment">#</span>
    error_page   <span class="hljs-number">500</span> <span class="hljs-number">502</span> <span class="hljs-number">503</span> <span class="hljs-number">504</span>  /<span class="hljs-number">50</span>x.html;
    location = /<span class="hljs-number">50</span>x.html {
        root   html;
    }

    <span class="hljs-comment"># proxy the PHP scripts to Apache listening on 127.0.0.1:80</span>
    <span class="hljs-comment">#</span>
    <span class="hljs-comment">#location ~ \.php$ {</span>
    <span class="hljs-comment">#    proxy_pass   http://127.0.0.1;</span>
    <span class="hljs-comment">#}</span>

    <span class="hljs-comment"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span>
    <span class="hljs-comment">#</span>
    <span class="hljs-comment">#location ~ \.php$ {</span>
    <span class="hljs-comment">#    root           html;</span>
    <span class="hljs-comment">#    fastcgi_pass   127.0.0.1:9000;</span>
    <span class="hljs-comment">#    fastcgi_index  index.php;</span>
    <span class="hljs-comment">#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;</span>
    <span class="hljs-comment">#    include        fastcgi_params;</span>
    <span class="hljs-comment">#}</span>

    <span class="hljs-comment"># deny access to .htaccess files, if Apache's document root</span>
    <span class="hljs-comment"># concurs with nginx's one</span>
    <span class="hljs-comment">#</span>
    <span class="hljs-comment">#location ~ /\.ht {</span>
    <span class="hljs-comment">#    deny  all;</span>
    <span class="hljs-comment">#}</span>
}</code></pre>Modified as:

 server {
        listen       80;
        server_name  localhost;
    <span class="hljs-comment">#charset koi8-r;</span>

    <span class="hljs-comment">#access_log  logs/host.access.log  main;</span>

    location ^~ /Express/ {
        proxy_pass   http:<span class="hljs-comment">//127.0.0.1:8080/;</span>
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-<span class="hljs-keyword">For</span> $proxy_add_x_forwarded_for;
    }

    <span class="hljs-comment">#error_page  404              /404.html;</span>

    <span class="hljs-comment"># redirect server error pages to the static page /50x.html</span>
    <span class="hljs-comment">#</span>
    error_page   <span class="hljs-number">500</span> <span class="hljs-number">502</span> <span class="hljs-number">503</span> <span class="hljs-number">504</span>  /<span class="hljs-number">50</span>x.html;
    location = /<span class="hljs-number">50</span>x.html {
        root   html;
    }

    <span class="hljs-comment"># proxy the PHP scripts to Apache listening on 127.0.0.1:80</span>
    <span class="hljs-comment">#</span>
    <span class="hljs-comment">#location ~ \.php$ {</span>
    <span class="hljs-comment">#    proxy_pass   http://127.0.0.1;</span>
    <span class="hljs-comment">#}</span>

    <span class="hljs-comment"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span>
    <span class="hljs-comment">#</span>
    <span class="hljs-comment">#location ~ \.php$ {</span>
    <span class="hljs-comment">#    root           html;</span>
    <span class="hljs-comment">#    fastcgi_pass   127.0.0.1:9000;</span>
    <span class="hljs-comment">#    fastcgi_index  index.php;</span>
    <span class="hljs-comment">#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;</span>
    <span class="hljs-comment">#    include        fastcgi_params;</span>
    <span class="hljs-comment">#}</span>

    <span class="hljs-comment"># deny access to .htaccess files, if Apache's document root</span>
    <span class="hljs-comment"># concurs with nginx's one</span>
    <span class="hljs-comment">#</span>
    <span class="hljs-comment">#location ~ /\.ht {</span>
    <span class="hljs-comment">#    deny  all;</span>
    <span class="hljs-comment">#}</span>
}</code></pre>It's actually a revision server In node location The content of the node.

The above implementation maps all URLs related to Express to http://127.0.0.1:8080. That is to say, tomcat has successfully implemented the function.

The server above can have multiple (multiple server domains). If there is a server_name needs to be specified. The default is localhost, which can be server_name   www.test.com; Or server_name   xxx.test.com ; that is, when the secondary domain name

2)

Suppose that there are three tomcat in place. ulr access is / tomcat/6 /, / tomcat/7 /, / tomcat/8 /. To achieve load balanced forwarding through nginx, you should write as follows:

 location ^~ /tomcat/6/ {
        proxy_pass   http://127.0.0.1:8686/;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location ^~ /tomcat/7/ {
        proxy_pass   http://127.0.0.1:8787/;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location ^~ /tomcat/8/ {
        proxy_pass   http://127.0.0.1:8888/;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
So, by visiting http://xxx.xxx.xxx.xxx/tomcat/6/ It can be directly forwarded to port 8686 of 127.0.0.1 for testing.
In addition, ^ ~ / tomcat/6 / means that the matching URL starts with / tomcat/6 /. Case calculation is required.


3)

Reverse proxy is suitable for many situations, and load balancing is the most common use.

nginx As one of the most popular web servers, reverse proxy can be easily implemented.

Official document of nginx reverse agent: NGINX REVERSE PROXY

When several different web servers are deployed on a host and need to be able to access these web servers at port 80 at the same time, you can use nginx's reverse proxy function: use nginx to monitor all requests at port 80, and forward to the corresponding web server according to the forwarding rules (more commonly, forward by URI).

For example, there are three servers: webmail, webcom and webdefault running on portmail, portcom and portdefault. To access these three web servers from port 80 at the same time, you can run nginx on port 80, Then forward the request under / mail to the webmail server, and forward the request under / com to the webcom server, Forward all other requests to the webdefault server.

Suppose the server domain name is example.com , the corresponding nginx http configuration is as follows:

http {
    server {
            server_name example.com;
        location /mail/ {
                proxy_pass http:<span class="hljs-comment">//example.com:protmail/;</span>
        }

        location /com/ {
                proxy_pass http:<span class="hljs-comment">//example.com:portcom/main/;</span>
        }

        location / {
                proxy_pass http:<span class="hljs-comment">//example.com:portdefault;</span>
        }
}

}


The above configuration forwards requests according to the following rules (GET and POST requests will be forwarded):

  • Will http://example.com/mail/ Forward the request to http://example.com:portmail/
  • Will http://example.com/com/ Forward the request to http://example.com:portcom/main/
  • Forward all other requests to http://example.com:portdefault/

It should be noted that in the above configuration, the proxy setting of webdefault does not specify a URI, while that of webmail and webcom specifies a URI (respectively, / and / main /).  
If the proxy address has a URI, the URI replaces the part of the URI that the location matches.  
If there is no URI in the proxy address, the complete request URL will be forwarded to the proxy.

Official document description:

If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter. 
If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).

Example of forwarding configured above:

  • http://example.com/mail/index.html -> http://example.com:portmail/index.html
  • http://example.com/com/index.html -> http://example.com:portcom/main/index.html
  • http://example.com/mail/static/a.jpg -> http://example.com:portmail/static/a.jpg
  • http://example.com/com/static/b.css -> http://example.com:portcom/main/static/b.css
  • http://example.com/other/index.htm -> http://example.com:portdefault/other/index.htm

Reference connection: http://blog.bbzhh.com/index.php/aboutme.html

http://blog.csdn.net/tobacco5648/article/details/51099426




Posted by grigori on Fri, 05 Jun 2020 22:25:27 -0700