10. nginx reverse proxy load balancing
(1) Description of LNMP architecture migration database
Migrating databases: Using the database backup command (mysql mysqladmin mysqldump)
1) Back up database information
mysqldump -uroot -poldboy123 --all-databases >/tmp/bak.sql ll /tmp/bak.sql -h scp /tmp/bak.sql 172.16.1.51:/tmp/
2) Restore database communications
##db01 mysql -uroot -poldboy123 </tmp/bak.sql ###db01 Add New User grant all on wordpress.* to wordpress@'172.16.1.0/255.255.255.0' identified by 'oldboy123'; flush privileges; mysql -uwordpress -poldboy123 -h 172.16.1.51
3) After the database migration, modify the configuration file of the website connection database
Mysql-uwordpress-poldboy123-h 172.16.1.51 <--Test site web server connectivity to migrated database before modifying configuration file VIM wp-config.php <--Modify database connection parameter information on wordpress /** MySQL host*/ Define ('DB_HOST','172.16.1.51') <--Modify the host information for the connection to 172.16.1.51 Note: The web server database can now be closed
4) Stop MySQL service on nginx server
/etc/init.d/mysql stop
(2) Instructions for migrating LNMP schema data to NFS storage
01: Move the data out of the original directory first
cd /application/nginx/html/blog/wp-content/uploads mkdir /tmp/wordpress_backup -p mv ./* /tmp/wordpress_backup/ Where to store the data locally and how to get it Get the address information of the resource by right clicking on the page of the website (2) find command using -mmin 5 3. Use inotify service to monitor directory data changes
02:Configure Create Shared Directory on NFS Server
vim /etc/exports /data 172.16.1.0/24(rw,sync,all_squash) showmount -e 172.16.1.31 mount -t nfs 172.16.1.31:/data /mnt/ showmount -e 172.16.1.31 mount -t nfs 172.16.1.31:/data/ ./uploads/ mv /tmp/wordpress_backup/* ./
(3) nginx reverse proxy load balancing function
Client====Proxy Server===web Server Client sees server==proxy server Proxy server===web server Reverse Proxy Functional Architecture 3 web servers to form a web server cluster web01 10.0.0.7 172.16.1.7 web02 10.0.0.8 172.16.1.8 web03 10.0.0.9 172.16.1.9 1 Load Balancing Server lb01 10.0.0.5 172.16.1.5
1. Deploy web server
First mileage: install and deploy nginx software
mkdir /server/tools -p cd /server/tools wget http://nginx.org/download/nginx-1.12.2.tar.gz tar xf nginx-1.12.2.tar.gz yum install -y pcre-devel openssl-devel useradd -M -s /sbin/nologin www cd nginx-1.12.2 ./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module make && make install ln -s /application/nginx-1.12.2 /application/nginx /application/nginx/sbin/nginx netstat -lntup|grep nginx
Second mileage: Edit nginx configuration file
server { listen 80; server_name www.etiantian.org; root html/www; index index.html index.htm; } server { listen 80; server_name bbs.etiantian.org; root html/bbs; index index.html index.htm; } scp -rp /application/nginx/conf/nginx.conf 172.16.1.8:/application/nginx/conf/ scp -rp /application/nginx/conf/nginx.conf 172.16.1.9:/application/nginx/conf/
Mileage 3: Creating a simulated test environment
mkdir /application/nginx/html/{www,bbs} -p for name in www bbs;do echo "$(hostname) $name.etiantian.org" >/application/nginx/html/$name/oldboy.html;done for name in www bbs;do cat /application/nginx/html/$name/oldboy.html;done
Fourth mileage: Test access on a load balancing server
curl -H host:www.etiantian.org 10.0.0.7/oldboy.html web01 www.etiantian.org curl -H host:bbs.etiantian.org 10.0.0.7/oldboy.html web01 bbs.etiantian.org curl -H host:www.etiantian.org 10.0.0.8/oldboy.html web02 www.etiantian.org curl -H host:bbs.etiantian.org 10.0.0.8/oldboy.html web02 bbs.etiantian.org curl -H host:www.etiantian.org 10.0.0.9/oldboy.html web03 www.etiantian.org curl -H host:bbs.etiantian.org 10.0.0.9/oldboy.html web03 bbs.etiantian.org
Deploying a load balancing server
First mileage: install and deploy nginx software
mkdir /server/tools -p cd /server/tools wget http://nginx.org/download/nginx-1.12.2.tar.gz tar xf nginx-1.12.2.tar.gz yum install -y pcre-devel openssl-devel useradd -M -s /sbin/nologin www cd nginx-1.12.2 ./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module make && make install ln -s /application/nginx-1.12.2 /application/nginx /application/nginx/sbin/nginx netstat -lntup|grep nginx
Second milestone: Write nginx reverse proxy profile
grep -Ev "#|^$" nginx.conf.default >nginx.conf //Official link: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream Syntax: upstream name { ... } Default: — Context: http eg: upstream oldboy { server 10.0.0.7:80; server 10.0.0.8:80; server 10.0.0.9:80; } //Description: The upstream module is similar to specifying an address pool or a web server group //Official link: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except eg: location / { proxy_pass http://oldboy; } //Description: proxy_pass is mainly used to throw user access requests to the corresponding node servers in the upstream module worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream oldboy { server 10.0.0.7:80; server 10.0.0.8:80; server 10.0.0.9:80; } server { listen 80; server_name localhost; root html; index index.html index.htm; location / { proxy_pass http://oldboy; } } } /application/nginx/sbin/nginx -t /application/nginx/sbin/nginx -s reload
Third mileage: conducting access load balancing server tests
1)Testing with a browser //Resolve hosts http://Www.etiantian.org/oldboy.html <--Use ctrl+F5 to refresh tests to check for load scheduling 2)utilize curl Command to test [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/oldboy.html web01 www.etiantian.org [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/oldboy.html web02 www.etiantian.org [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/oldboy.html web03 www.etiantian.org
(4) Detailed function description of Nginx reverse proxy load balancing module
ngx_http_upstream_module http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
(1) Description of module common functions:
1) Define backend cluster web node information, define an address pool
upstream oldboy { server 10.0.0.7:80; server 10.0.0.8:80; server 10.0.0.9:80; }
2) Implement weight value load access function
upstream oldboy { server 10.0.0.7:80 weight=3; server 10.0.0.8:80 weight=1; server 10.0.0.9:80 weight=1; }
3) Define the number of backend access failures-max_fails
upstream oldboy { server 10.0.0.7:80 weight=3 max_fails=3; server 10.0.0.8:80 weight=1 max_fails=3; server 10.0.0.9:80 weight=1 max_fails=3; }
4) Define the interval between back-end failed retries-fail_timeout
upstream oldboy { server 10.0.0.7:80 weight=3 max_fails=3 fail_timeout=10s; server 10.0.0.8:80 weight=1 max_fails=3; server 10.0.0.9:80 weight=1 max_fails=3; } Description: After several failed attempts, the corresponding node will be given another chance after the time-out period has elapsed
5) Define a hot standby node for back-end services-backup
upstream oldboy { server 10.0.0.7:80; server 10.0.0.8:80; server 10.0.0.9:80 backup; }
(2) Module common scheduling algorithms:
1)Define Polling Scheduling Algorithm-rr-Default Scheduling Algorithm //Take an equal distribution 2)Define Weight Scheduling Algorithm-wrr //An able man is always busy 3)Define a static scheduling algorithm-ip_hash upstream oldboy { ip_hash; server 10.0.0.7:80; server 10.0.0.8:80; server 10.0.0.9:80 backup; } //Note: When configuring ip_hash, backup and weight parameters must not appear together 4)Define the minimum number of connections-least_conn upstream oldboy { least_conn; server 10.0.0.7:80; server 10.0.0.8:80; server 10.0.0.9:80 backup; }
(3) Module Common Functions (ngx_http_proxy_module)
proxy_set_header --- Set up a reverse proxy server to web Server's HTTP Request header information in a message server { listen 80; server_name www.etiantian.org; root html; index index.html index.htm; location / { proxy_pass http://oldboy; proxy_set_header host $host; } } server { listen 80; server_name bbs.etiantian.org; proxy_set_header host $host; root html; index index.html index.htm; location / { proxy_pass http://oldboy; proxy_set_header host $host; } } //Note: With the above configuration, access load balancing can be achieved to see different virtual host page information (oldboy, equivalent to using ip address to access the virtual host, the first server is accessed by default) server { listen 80; server_name www.etiantian.org; root html; index index.html index.htm; location / { proxy_pass http://oldboy; proxy_set_header host $host; proxy_set_header X-Forwarded-For $remote_addr; } } server { listen 80; server_name bbs.etiantian.org; root html; index index.html index.htm; location / { proxy_pass http://oldboy; proxy_set_header X-Forwarded-For $remote_addr; } } //Description: Implement user access to reverse proxy service to record real user IP address information in web server logs
(5) Cases:
Conduct business case needs combing first: Implement upload upload server pool to process requests when users request a www.etiantian.org/upload/xx address. Implements a static server pool to process requests when a user requests a www.etiantian.org/static/xx address. In addition, for other access requests, all requests are processed by the default dynamic server pool. User Request (URI) Processing Request Server Site Directory Function /upload. 10.0.0.8:80. html/www/upload. upload server /static. 10.0.0.7:80. html/www/static. static static static server / 10.0.0.9:80 html/www default
Solutions:
1) Complete nginx site server configuration
First mileage: Creating a test environment
# Create upload directory on 10.0.0.8 host and generate site test page file cd /application/nginx/html/www/ mkdir upload cp oldboy.html upload/ # Create a static directory on the 10.0.0.7 host and generate a Web site test page file cd /application/nginx/html/www/ mkdir static cp oldboy.html static/ # Create default test page file on 10.0.0.9 host
Second mileage: access testing using lb01
# Test for 10.0.0.8 access curl -H host:www.etiantian.org 10.0.0.8/upload/oldboy.html web02 www.etiantian.org # Test for 10.0.0.7 access curl -H host:www.etiantian.org 10.0.0.7/static/oldboy.html web01 www.etiantian.org # Test for 10.0.0.9 access curl -H host:www.etiantian.org 10.0.0.9/oldboy.html web03 www.etiantian.org
2) Complete nginx reverse proxy server configuration
First mileage: configure upstream module information
upstream upload { server 10.0.0.8:80; } upstream static { server 10.0.0.7:80; } upstream default { server 10.0.0.9:80; }
Second mileage: Configure proxy_pass module information
server { listen 80; server_name www.etiantian.org; root html; index index.html index.htm; location / { proxy_pass http://default; proxy_set_header host $host; proxy_set_header X-Forwarded-For $remote_addr; } location /upload { proxy_pass http://upload; proxy_set_header host $host; proxy_set_header X-Forwarded-For $remote_addr; } location /static { proxy_pass http://static; proxy_set_header host $host; proxy_set_header X-Forwarded-For $remote_addr; } }
Example 2:
Display different page information based on user request for client software
Solution:
1) nginx site server configuration
First mileage: Creating a test environment
# Create upload directory on 10.0.0.8 host and generate site test page file cd /application/nginx/html/www/ cat oldboy.html # Create a static directory on the 10.0.0.7 host and generate a Web site test page file cd /application/nginx/html/www/ cat oldboy.html # Create default test page file on 10.0.0.9 host cd /application/nginx/html/www/ cat oldboy.html //Test access: [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.7/oldboy.html web01 www.etiantian.org [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.8/oldboy.html web02 www.etiantian.org [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.9/oldboy.html web03 www.etiantian.org
2) nginx reverse proxy server configuration
First mileage: configure upstream module information
upstream iphone { server 10.0.0.8:80; } upstream android { server 10.0.0.7:80; } upstream pc { server 10.0.0.9:80; }
Second mileage: Configure proxy_pass module information
server { listen 80; server_name www.etiantian.org; root html; index index.html index.htm; location / { if ($http_user_agent ~* "iphone") { proxy_pass http://iphone; } if ($http_user_agent ~* "android") { proxy_pass http://android; } proxy_pass http://pc; proxy_set_header host $host; proxy_set_header X-Forwarded-For $remote_addr; } }