1, environmental

CentOS6.5

JDK1.7

Nginx1.7.4

2. Environment preparation

Install the necessary tools first

[html] view plain copy

  1. yum -y install gcc gcc-c++ autoconf automake make  
  2. yum -y install zlib zlib-devel openssl openssl–devel pcre pcre-devel  

3. Download nginx

Nginx.org/en/download…

The Linux version is displayed on the left, and the Windows version is displayed on the right


Alternatively, run the following command in the command box to download it

[html] view plain copy

  1. Wget # http://nginx.org/download/nginx-1.7.4.tar.gz

[html] view plain copy

  1. #tar -zxvf nginx-1.7.4.tar.gz

4, installation,

After decompressing, switch to nginx-1.7.4 directory

perform

[html] view plain copy

  1. ./configure  

/configure: error: The HTTP rewrite module requires the PCRE library.

You can either disable the module by using –without-http_rewrite_module

option, or install the PCRE library into the system, or build the PCRE library

statically from the source with nginx by using –with-pcre=<path> option.


You need to install the PCRE, which allows ngnix to support the rewrite function using the command to install:

[html] view plain copy

  1. yum install pcre-devel.i686  

Type y, press Enter


Complete is then prompted to indicate success.

/configure displays the same error; Run the file-name PCRE command to query the installation location of the PCRE./auto/lib/ PCRE is displayed


Run the./configure –with-pcre=./auto/lib/pcre command./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using –without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using –with-zlib=<path> option. Also missing zlib Library information! Yum -y install make zlib zlib-devel GCC -c++ libtool yum -y install make zlib zlib-devel GCC -c++ libtool /configure –with-pcre=./auto/lib/pcre


If the configuration is successful, run the following command to install: make && make install


Yum -y install zlib zlib-devel openssl openssl–devel pcre pcre-devel yum -y install zlib zlib-devel openssl–devel pcre pcre-devel /configure: make && make install complete!


Remember some of the directory locations in the image above! Run the following command to change the configuration file: CD /usr/local/nginx/conf Run the following command to change the configuration file: vi Nginx. conf Modify the server configuration section. Press I to enter the editing mode


Press ESC to exit the editing mode, press the colon key: to enter the command mode, enter wq, and press Enter to save the configuration and exit. Run the following command to switch to the sbin directory: CD /usr/local/nginx/sbin Start nginx :./nginx open firewall port: /sbin/iptables -I INPUT -p TCP –dport 88 -j ACCEPT /etc/init.d/iptables save http://your server IP:88/ See the following information?


/configure auto/ Unix: No such file or directory./configure auto/ Unix: No such file or directory./configure auto/ Unix: No such file or directory Tangled for a long time, and then found that when decompression there are a lot of files did not decompress out, it should be judged that the installation package compression file is broken, so decompression when there are a lot of files or directories, download a decompression installation problem is solved. Configure Nginx startup, vi/etc/rc. D/rc. Local added at the end of the file “/ usr/local/Nginx/sbin/Nginx” Nginx common commands: reload the configuration file: / usr/local/nginx/sbin/nginx -s reload stop nginx service: / usr/local/nginx/sbin/nginx -s stop file permissions problem: Run the following command to change the permission: chmod -r 777. /uploadfile 5. Configure Nginx+Tomcat and use Nginx to implement reverse proxy

At this point we are still accessing the Tomcat server directly, now I want to access Tomcat via Nginx, i.e. typing localhost will display our demo page. This requires us to modify the nginx core configuration file, the nginx.conf file in the conf folder of the nginx directory. First we need to understand the function of some nodes in this file. Worker_processes: Number of worker_connections: Maximum number of connections to a single process Server: Each server corresponds to a proxy server lister: listening port. The default value is 80. Server_name: specifies the domain name of the current service. Index: When no home page is specified, the specified file will be selected by default. Multiple files can be selected, separated by Spaces. Proxy_pass: Upstream name{}: server cluster name once we know what the node does, we know the server part of the file we need to modify.


It accesses its welcome page, index.html

Let’s make a few minor changes to this code. Is to redirect the request to the server we define


Then enter the command nginx -s reload in CMD to restart nginx.

After the restart, we will type localhost again, you can see that the page is our demo.

At this point, the reverse proxy is complete, so that all requests go through the proxy server to the official server, which protects the site to some extent.

6. Load balancing is implemented


Load balancing means that the proxy server evenly distributes the received requests to each server. The advantage of load balancing may not be obvious when the number of visits is small or the number of concurrent visits is small. Not to mention the number of visits and high concurrency such as Taobao Double 11 and Ministry of Railways snatping tickets, even the buying up activities of general websites will also cause great pressure on the server, which may cause the server to crash. And load balancing can obviously reduce or even eliminate the occurrence of this situation, let’s talk about the implementation method. Copy the project from Tomcat1 to Tomcat2 and modify the text on the next page slightly so that we can later distinguish which Tomcat our request was distributed to. Tomcat1 port is 8080,tomcat2 port is 8081.

With the server ready, we need to define a cluster of servers outside of the server using the upstream tag mentioned above. The server cluster name is netitcast.com.


At the same time we need to modify the server, redirect the path to ask you server cluster.


Restart Nginx, type localhost:88 in your browser, refresh a few times, and you can see the two pages switching back and forth.