Nginx reverse proxy tomcat for load balancing

Source: Internet
Author: User

In the computer network, the reverse proxy is a proxy server. It obtains resources from the backend server, based on client requests, and then returns those resources to the client. Unlike the forward proxy, the forward proxy acts as a medium to return the resources obtained on the Internet to the associated client, while the reverse proxy is used as a proxy on the server side rather than the client.

Nginx (pronounced with engine x) is a Web server that can reverse proxy http, HTTPS, SMTP, POP3, IMAP protocol links, as well as a load balancer and an HTTP cache.
It was originally used by Russian large portals and search engine Rambler (Russian: Рамблер). This software is released under the Bsd-like protocol and can be run on Unix, Gnu/linux, BSD, Mac os X, Solaris, and Microsoft Windows, among other operating systems.

Download Address: http://nginx.org/en/download.html
Unzip to any directory when download is complete, double-click Nginx.exe to start
Open localhost started successfully.

Open nginx Main configuration file conf/nginx.conf

Server{Listen the; server_name localhost;#charset koi8-r;#access_log Logs/host.access.log main;Location/{root HTML;IndexIndex. htmlIndex. htm; }#error_page 404/404.html;# REDIRECT Server error pages to the static page/50x.html#Error_page -502503504/ -x.html; Location =/ -x.html {root HTML; }# Proxy The PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# Proxy_pass http://127.0.0.1;#}# Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root HTML;# Fastcgi_pass 127.0.0.1:9000;# Fastcgi_index index.php;# Fastcgi_param Script_filename/scripts$fastcgi_script_name;# include fastcgi_params;#}# Deny access to. htaccess files, if Apache ' s document Root# concurs with Nginx ' s one##location ~/\.ht {# deny all;#}}

Listen: Indicates the port on which the current proxy server listens, and the default is to listen on port 80. Note that if we have more than one server configured, this listen will be configured differently, or we will not be sure where to go.

SERVER_NAME: to indicate where to go when the supervisor hears, we go directly to the local, then directly to the Nginx folder.

Location: Represents the matching path, at which point all requests are configured/indicated to be matched here

Root: The root is configured in this means that when matching the path of this request, will be in this folder to find the corresponding file, here is useful for our static file servo.

Index: When the home page is not specified, the specified file is selected by default, it can have multiple, and is loaded sequentially, if the first one does not exist, then the second one is found, and so on.

The following error_page is a page that represents the error, which we do not use here for the moment, regardless of it.

Forwarding to Tomcat requires modification of two places

    server_name localhost:8080;      location / {          proxy_pass http://localhost:8080;    }  

We have modified the above two places, my tomcat on the 8080 port, can be modified according to their needs. Here is a new element, Proxy_pass, which represents the proxy path, which is equivalent to forwarding, rather than having to specify a folder as previously said Root.

At this time we modified the file, is not meant to have to shut down the nginx and then restarted, in fact, do not have, Nginx can reload the file.
Nginx-s Reload
Nginx runs at the command line in addition to starting other commands

Open localhost has been proxied to Tomcat

Load Balancing Configuration

 upstream Local_tomcat { server  localhost:  8080 ;  server  localhost:  9080 ;  server  {listen  ; server_name localhost;  #直接匹配网站根, through the domain name to visit the site home more frequently, using this will speed up processing, the official website said.   #这里是直接转发给后端应用服务器了, or it can be a static home page   # First required rule  location =/{Proxy_pass http: //lo Cal_tomcat/index }  # The second required rule is to handle a static file request, which is Nginx's strength as an HTTP server   # There are two configuration modes, a directory match or a suffix match, either one or a combination of  location ^~/ static /{root/webroot/ static /; Expires   H; } location ~* \. (Gif|jpg|jpeg|png|css|js|ico) $ {root/webroot/res/; Expires   D; The  #第三个规则就是通用规则, used to forward the dynamic request to the backend application server   #非静态文件请求就默认是动态请求, based on the actual grasp of   #毕竟目前的一些框架的流行, with. PHP,. The JSP suffix is rarely the case  location/{proxy_pass http: //local_tomcat }} 

Close Nginx:nginx-s Stop

Reference: http://nginx.org/en/docs/windows.html
http://cxshun.iteye.com/blog/1535188

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the Nginx reverse proxy tomcat to achieve load balancing, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.