Nginx do javaweb load balancing

Source: Internet
Author: User
Tags openssl library

With the increase of user volume, a single server has not satisfied the needs of users.

Getting ready: Installing GCC, pcre-devel, zlib, and OpenSSL are offline please poke here

GCC installation
Installation nginx need to download the source code to compile, compile depends on the GCC environment, if there is no GCC environment, you need to install:

Yum Install gcc-c++

PCRE Pcre-devel Installation
PCRE (perl Compatible Regular Expressions) is a Perl library that includes a Perl-compatible regular expression library. The Nginx HTTP module uses PCRE to parse the regular expression, so the Pcre library needs to be installed on Linux, and Pcre-devel is a two-time development library using Pcre. Nginx also needs this library. Command:

Yum install-y pcre Pcre-devel

  

Zlib
The Zlib library provides a number of ways to compress and decompress, and Nginx uses zlib to gzip the contents of the HTTP package, so you need to install the Zlib library on Centos.

Yum install-y zlib Zlib-devel

  

Openssl
OpenSSL is a strong Secure Sockets Layer cipher library that includes key cryptographic algorithms, common key and certificate encapsulation management functions, and SSL protocols, and provides a rich set of applications for testing or other purposes.
Nginx not only supports the HTTP protocol, but also supports HTTPS (that is, transmitting HTTP on the SSL protocol), so you need to install the OpenSSL library in Centos.

Yum Install-y OpenSSL Openssl-devel

  

Nginx

1, decompression nginx-1.8.1.tar.gz

TAR-ZXVF NGINX-1.8.1.TAR.GZCD nginx-1.8.1

2, compile, install Nginx

./configuremakemake Install

3. Start Nginx

[[email protected] nginx-1.8.1]# whereis Nginx:/usr/local/nginx[[email protected] nginx-1.8.1]# cd/usr/local/ nginx/   [[email protected] nginx]#./sbin/nginx     #启动

4, can go to access the IP nginx port by default is 80 see the interface on behalf of the success of half PS: If you do not access, you can try to turn off the firewall. The recommendation is to turn on port 80

5, prepare two tomcat of course you can be on the same server, and then change to a painless port. My side is on two servers. One ip:10.13.5.11 another ip:10.13.5.12. For convenience The difference is two different tomcat I modified the index.jsp to the following:

First set of ip:10.13.5.11

Second set of ip:10.13.5.12

6. Configuring Nginx Configure

[Email protected] nginx]# cd/usr/local/nginx/conf/

A. Configuring back-end server groups

   

    Upstream testsite.com{server 10.13.5.11:8080 weight=1;  Server 10.13.5.12:8080 weight=2; }

  

B. Use SERVER GROUP NOTE!!!!!   If location/{Proxy_pass http://testsite.com;}    Proxy_pass http://testsite.com End can not add/, but location/other_string/{proxy_pass http://testsite.com/;} Proxy_pass http://testsite.com must be added/

  

       location/mysite/{        Proxy_pass http://testsite.com/;  Proxy_redirect default; }    

C, the last of all the code

#user nobody;worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;events {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;    Sendfile on;    #tcp_nopush on;    #keepalive_timeout 0;    Keepalive_timeout 65;    #gzip on;    Upstream Testsite.com{server 10.13.5.11:8080 weight=1;server 10.13.5.12:8080 weight=2;        } server {Listen 80;        server_name localhost;        #charset Koi8-r;        #access_log Logs/host.access.log Main;            Location/{root HTML;        Index index.html index.htm;       } location/mysite/{ Proxy_pass http://testsite.com/;p roxy_redirect default;}        #error_page 404/404.html;        # REDIRECT Server error pages to the static page/50x.html # Error_page 502 503 504/50x.html;        Location =/50x.html {root html; } # Proxy The PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ {# ProX        Y_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 # #l        ocation ~/\.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:1m;    # ssl_session_timeout 5m; # ssl_ciphers high:!anull:!    MD5;    # ssl_prefer_server_ciphers on;    # location/{# root HTML;    # index index.html index.htm; #    }    #}}

7. Restart Nginx

[Email protected] conf]#/sbin/nginx-s Reload

8. Success

First server to access ip:10.13.5.12

  

The second one is the server that accesses ip:10.13.5.11

Nginx do javaweb load balancing

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.