Nginx tomcat cluster and Server Load balancer configuration instance

Source: Internet
Author: User
Tags sendfile

Nginx tomcat cluster and Server Load balancer configuration instance
I. Introduction to concepts used in nginx tomcat

1. Reverse Proxy. When a client request arrives, the reverse proxy receives the request and forwards the request to the backend server. If load balancing is performed, the request is distributed to the Server Load balancer servers.

The forward proxy is the client.

The reverse proxy is for the server. Request this reverse proxy server is like directly requesting the server where the resource is located.

2. The Server Load balancer mentioned here refers to distributing these requests to the backend servers (tomcat servers) as configured by nginx after receiving requests from the client, when weight is configured, the higher the weight, the higher the probability of receiving the request. ip_hash is allocated according to the ip hash value.

3. Static and Dynamic separation: static requests are processed by nginx, while dynamic requests are processed by tomcat. We know that tomcat can process static and dynamic requests, but why does it need to use nginx? Because nginx is more efficient in processing static requests than tomcat, so it gives play to its respective advantages.

That's exactly what we know when we get started ......

Ii. Configure an instance
# User nobody; # Number of processes. Generally, this is equal to the number of cpu cores worker_processes 1; # error Log configuration # error_log logs/error. log; # error_log logs/error. log notice; # error_log logs/error. log info; # pid process # pid logs/nginx. pid; events {# Number of work process connections worker_connections 1024;} http {include mime. types; default_type application/octet-stream; # log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request"' # '$ status $ body_bytes_sent "$ http_referer "'# '"$ Http_user_agent" "$ http_x_forwarded_for"'; # access_log logs/access. log main; # enable the efficient file transmission mode and call sendfile for file transmission. However, generally, to balance the speed with the I/O, # reduce the system load, set sendfile to off, sendfile on; # tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 65; # Whether to enable gzip compression output # gzip on; # configure Server Load balancer. nginx is used as a reverse proxy, access nginx is the server configured for server Load balancer. You can view the server Load balancer address through log # The error log you can view clearly: client: 127.0.0.1, server: localhost, request: "GET/index. jsp # HTTP/1.1 ", upstream:" http: // 127.0.0.1: 18081/index. jsp ", host:" localhost "upstream localhost {# ip_hash iphash implements load balancing. Here we use the method of reincarnation # nginx forwarding backend tomcat server configuration and its weight configuration, the larger the weight, the larger the load. The larger the load, the server localhost: 18081 weight = 1; server localhost: 18080 weight = 1 ;}# the server {# Is the port listen 80 of the nginx service; # This is the access domain name server_name localhost; # charset UTF-8 character encoding settings; index index.html index. jsp index.htm index. do; # nginx root directory. When a static request is sent The root html; # access_log logs/host will be searched in this directory by default. access. log main; # Dynamic and Static separation, dynamically reversed to the tomcat of Server Load balancer # jsp, do, and other pages are handed over to tomcat for location ~ \. (Jsp | do) $ {# proxy path, which is consistent with the localhost path of the Server Load balancer upstream localhost, but http cannot be forgotten! Proxy_pass http: // localhost; proxy_set_header HOST $ host; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ signature;} # The static page is handled by nginx. location ~. *\. (Htm | html | gif | jpg | jpeg | png | bmp | swf) $ {expires 30d ;}# error_page 404/404 .html; # redirect server error pages to thestatic page/50x.html # error_page 500 502 503 504/50 x.html; location =/50x.html {root html;} # proxy the PHP scripts to Apachelistening on 127.0.0.1: 80 ## location ~ \. Php $ {# proxy_pass http: // 127.0.0.1; #}# pass the PHP scripts to FastCGIserver 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. htaccess files, ifApache's document root # concurs with nginx's one # location ~ /\. Ht {# deny all ;#}# another virtual host using mix of IP-, name-, and port-based configuration # server {# listen 8000; # listen somename: 8080; # server_name somename alias another. alias; # location/{# root html; # index index.html index.htm; #}#}# HTTPS server # server {# listen 443 ssl; # server_name localhost; # ssl_certificate cert. pem; # ssl_certificate_key cert. key; # ssl_session_cache shared: SSL: 1 m; # ssl_session_timeout 5 m; ## ssl_ciphers HIGH :! ANULL :! MD5; # ssl_prefer_server_ciphers on; ## location/{# root html; # index index.html index.htm ;#}#}}
Iii. system deployment

1. Deploy your system to tomcat first, and then configure the static resource slices, js, and css used by the system to the nginx root directory, here, pay attention to some problems during system deployment. tomcat deployment is as old as ever. Static resources are deployed under nginx, and static resources are placed in the nginx root directory, that is, the root directory indicated by root under the server virtual host, and the deployed static resources also need the project name --> Resource Name

2. nginx deployment directory:

3. music Project deployed under tomcat:

 

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.