Today talk about nginx, in fact, there is a strange old iron, nginx is not the operation to run? Indeed, in most cases, if the company is large, if there is an operation, the operation will do the pot. But there is a trend now called DevOps and as I said before, integration of development operations, development needs to know a little bit about operations. In some Internet start-up companies just started the technical work, and is not so clear points, may be able to. Environment building, operation and maintenance, framework building, development must be taken down.

Source: github.com/limingios/n…

Nginx service Setup and Basic Demonstration (1)

Nginx

  • website

nginx.org/

  • introduce

Nginx is a lightweight, high-performance, stable, and concurrent HTTP and reverse proxy server. Also because of its characteristics, it is very widely used.

  • history

Developed by Igor Sysoev in Russia, it was originally used by Rambler (р а блер), a large Russian portal and search engine. Nginx is characterized by low memory consumption and high concurrency. In fact, NGINx’s concurrency does perform better in the same type of web server. Nginx is currently used by sina, NetEase, Tencent, and Plurk, a popular micro-blog, in Mainland China.

  • Understand the concept of agency

1. Forward proxy: In some cases, users need to manually set the IP address and port number of the proxy server to access the proxy server. 2. Reverse proxy: is used to proxy the server, proxy we want to access the target server. The proxy server accepts the request, forwards it to the server on the internal network (clustering), and returns the results from the server to the client, at which point the proxy server behaves as a server.

  • A machine configured according to the source code

System type The IP address The node role CPU Memory Hostname
Centos7 192.168.66.110 nginx 1 2G nginx
Centos7 192.168.66.111 tomcat 1 2G tomcat1
Centos7 192.168.66.112 tomcat 1 2G tomcat2

  • Three machines

The preparatory work

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
Copy the code

  • Nginx host pcRE installation package (192.168.66.110)

Sourceforge.net/projects/pc…

Nginx rewrite relies on the PCRE library, so you need to compile and install the PCRE library on a Linux system

Wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.41/pcre-8.41.tar.gz tar ZXVF pcre 8.41. Tar. Gzcd image.png
./configure
make && make install
pcre-config --prefix
Copy the code

  • Install nginx
cd. Wget http://nginx.org/download/nginx-1.13.10.tar.gz tar ZXVF nginx - 1.13.10. Tar. Gz mkdir nginxcdNginx-1.13.10./configure --prefix=/root/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/root/pcre-8.41 make && make installcd ~
cd nginx/sbin/
./nginx -v
./nginx -t
Copy the code

  • Start the nginx
cd ~/nginx/sbin/
./nginx
Copy the code

I don’t really have access

Modifying a Configuration File

vi ~/nginx/conf/nginx.conf
# change to user root
# wq save
Reload the new configuration of nginx
./sbin/nginx -s reload
Copy the code

  • Install Tomcat on machines 111 and 112
Java - wget version at http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.37/bin/apache-tomcat-8.5.37.tar.gzCopy the code

The tar ZXVF apache tomcat - 8.5.37. Tar. GzcdApache tomcat -- 8.5.37cdBin. / startup. Sh curl 127.0.0.1:8080Copy the code

  • 111 and 112 add an index.jsp file method to view
cd/root/apache-tomcat-8.5.37/webapps/ root >index.jsp vi index.jsp cat index.jspCopy the code

Upstream and location (110:110)

The future of software is going to be modular. The assembly is complete.

upstream
  • Introduction to the official website

nginx.org/en/docs/

  • Upstream instance

Nginx.org/en/docs/htt…

  • Upstream parameters
The parameter name describe
service Reverse service address plus port
weight The weight
max_fails If the host is considered to have died, the host is kicked out. The default value is 1. Enterprises generally configure 2 or 3 times, but e-commerce enterprises attach more importance to user experience, so it is 1 time. The premise is that this machine is in large supply.
fail_timeout Re-detect the time after kick out
backup Backup service
max_conns Maximum number of connections allowed
slow_start When a node recovers, it does not join the node immediately

Modify the nginx configuration file in 66.110

vi /root/nginx/conf/nginx.conf
cat /root/nginx/conf/nginx.conf
Copy the code

Since the weights are the same, two Tomcats-111 and 112 are cast in rotation

* Load balancing algorithm 1.ll+weight

The default load algorithm allocates service requests based on weight.

2.ip_hash

Application scenario of Hash based computing: The session is unidirectional and the server accesses the same server for the first time. The hash % 3 = index (IP). Disadvantages of the community or school centralized place, they are the same IP, a node load will be very very large. Become a hotspot and have ip_hash which invalidates the weight weight.

3.url_hash

(third-party) Application scenario: Static resource caching saves storage and speeds up the storage

4.least_conn

The minimum link

5.least_time

Minimum response time, compute the average node response time, and then take the one with the fastest response and assign the higher weight.

6.keeplive

Number of connections occupied. High memory consumption, but fast response time, should keep the socket connection.

  • location

1.root

The following configuration is equivalent to re-pointing the input path in the browser

2.index

Specifies the site start page based on the front and back end separation

3.proxy_set_header

Used to redefine the request header sent to the backend server

4.proxy_pass

If/is added to the url after proxy_pass, it indicates the absolute root path. If there is no /, it indicates the relative path, and the matched part of the path is also passed to the agent

Static and static separation scheme

Generally, movement and motion are separated in two ways

  1. Static files into nginx
  2. Static files are placed on the specified server, which server to redirect to by requesting the address.
  • Static files into nginx
E :wwwroot, e:wwwroot, e:wwwroot, e:wwwroot

server {  
  listen       80;  
  server_name  localhost;  

  location / {  
      root   e:wwwroot;  
      index  index.html;  
  }  

  All static requests are handled by nginx and stored in HTML
  location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$ {  
      root    e:wwwroot;  
  }  

  All dynamic requests are forwarded to Tomcat for processing
  location ~ .(jsp|do)$ {  
      proxy_pass  http://test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root e:wwwroot; }}Copy the code
  • Static server mode

Upstreams, location, location

PS: Finally come to A popular saying, you will be king, I am your eunuch, upstream is temple, temple there can be multiple, each temple had allocated up according to the different fat inside, temple are all above 120, A harem B all below 100, temple there are many love princess, at night the king have the demand, tell the eunuch, The eunuch according to the situation of the harem, according to a certain algorithm, see which love concubine can be bedchamber, I the eunuch to tell the King, the King directly to the way I tell his love concubine through the algorithm.