時間:2023-04-18 21:08:01 | 來源:網站運營
時間:2023-04-18 21:08:01 來源:網站運營
十年經驗程序員,手把手教你使用Nginx搭建web集群【建議新手收藏】:#!/bin/bashnginx_pkg='nginx-1.5.1.tar.gz'nginx_prefix=/usr/local/nginxhtml=/var/nginxlog=/var/log/nginxcheck13 () { [ $UID -ne 0 ] && echo "need to be root to that" && exit 1 [ ! -f $nginx_pkg ] && echo "not found source packager" && exit 1 [ ! -d $html ] && mkdir -p $html [ ! -d $log ] && mkdir -p $log}nginx_install () { source_pkg=`echo $nginx_pkg|awk -F ".tar" '{print $1}'` [ -d /usr/src/$source_pkg ]&&rm -rf /usr/src/$source_pkg tar xf $nginx_pkg -C /usr/src cp nginxd /usr/src/$source_pkg if [ $? -eq 0 ];then cd /usr/src/$source_pkg if [ $? -eq 0 ];then yum -y install gcc-* pcre pcre-devel zlib zlib-devel openssl-* &> /dev/null [ $? -ne 0 ]&&"YUM set error" && exit 1 ./configure --prefix=$nginx_prefix if [ $? -eq 0 ];then make if [ $? -eq 0 ];then make install if [ $? -eq 0 ];then ln -s -f $nginx_prefix/conf/nginx.conf /etc/ ln -s -f $nginx_prefix/logs/ $log/logs ln -s -f $nginx_prefix/html $html/html ln -s -f $nginx_prefix/sbin/ /usr/sbin/ cp nginxd /etc/init.d/nginx;chmod 755 /etc/init.d/nginx else exit 1 fi else exit 1 fi else exit 1 fi else exit 1 fi else exit 1fi [ $? -eq 0 ]&&clear||exit echo -e "/n/033[32m Nginx Install Success: /033[0m" echo -e "/n" echo -e "/tNginx_conf: /etc/nginx.conf" echo -e "/tNginx_html: $html/html" echo -e "/tNginx_access_log: $log/logs/access.log" echo -e "/tNginx_error_log: $log/logs/error.log/n/n/n/n" read -n1 -p "press any key and exit...." echo }check13nginx_install
(2)配置web服務器操作[root@web02 ~]# sh nginx_install # 腳本安裝nginx[root@web02 ~]# echo web02 > /usr/local/nginx/html/index.html # 寫入頁面[root@web02 ~]# yum -y install elinks &>/dev/null # 安裝文本瀏覽器[root@web02 ~]# /usr/local/nginx/sbin/nginx # 啟動nginx[root@web02 ~]# elinks http://localhost -dump web02
3、配置分發(fā)器(輪詢方式分發(fā))# 清除空行和注釋項$ sed -i '/#/d' nginx.conf$ sed -i '/^$/d' nginx.conf# 配置nginx.confworker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream web{ # 名為web的反向代理群組 server 192.168.31.42; server 192.168.31.43; } server { listen 80; server_name localhost; location / { proxy_pass http://web; # 去找反向代理 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
4、集群分發(fā)測試(默認輪詢)[root@web02 ~]# elinks http://192.168.31.40 -dump web01[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web01[root@web02 ~]# elinks http://192.168.31.40 -dump web02
為了答謝大家關注和支持,這次給大家準備了限時領取福利:阿里面試題、百度面試題、滴滴面試題、華為面試題、京東面試題、美團面試題、騰訊面試題、頭條面試題、中興面試題。upstream web { server 192.168.31.42; server 192.168.31.43;}server { listen 80; server_name localhost; location / { proxy_pass http://web; } }
前面已經測試驗證了輪詢算法分發(fā)。配置backup參數如下所示:upstream web { server 192.168.31.42 weight=1; server 192.168.31.43 weight=1 backup;}server { listen 80; server_name localhost; location / { proxy_pass http://web; } }
關停第一個節(jié)點情況,訪問嘗試:[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web02
(2)基于權重的分發(fā)upstream web { # 設置權重比例1:2 server 192.168.31.42 weight=1; server 192.168.31.43 weight=2;}server { listen 80; server_name localhost; location / { proxy_pass http://web; } }
訪問測試驗證:[root@web02 ~]# elinks http://192.168.31.40 -dump web01[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web01[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web02
通過權重比例的分配,可以讓性能更強的服務器承擔處理更多的請求。upstream web { ip_hash; # 指定ip_hash即可,默認weight權重比例1: 1 server 192.168.31.42; server 192.168.31.43; }server { listen 80; server_name localhost; location / { proxy_pass http://web; } }
訪問測試驗證:# 源ip固定[root@web01 ~]# elinks http://192.168.31.40 -dump web02[root@web01 ~]# elinks http://192.168.31.40 -dump web02[root@web01 ~]# elinks http://192.168.31.40 -dump web02MacBook-Pro:~ hqs$ elinks http://192.168.31.40 -dump web01MacBook-Pro:~ hqs$ elinks http://192.168.31.40 -dump web01MacBook-Pro:~ hqs$ elinks http://192.168.31.40 -dump web01
Request URL: http://192.168.31.43/ # 請求的URLRequest Method: GET # 請求的方法Status Code: 200 OKRemote Address: 192.168.31.43:80Referrer Policy: no-referrer-when-downgrade # 請求的策略Response headers # 響應頭Accept-Ranges: bytesConnection: keep-aliveContent-Length: 6Content-Type: text/htmlDate: Fri, 26 Oct 2018 12:43:02 GMTETag: "5bd3014d-6"Last-Modified: Fri, 26 Oct 2018 11:58:05 GMTServer: nginx/1.15.5Request Headers # 請求頭GET /index.php HTTP/1.1 # 請求方法是GET;域名后面的部分就是路徑,默認是‘/’;使用的HTTP協議是1.1 Host: 192.168.31.43 # 訪問的域名(域名或IP均可)Connection: keep-alive # 長連接Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 # 用戶瀏覽器的類型Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 # 可以接受的數據類型Accept-Encoding: gzip, deflate # 壓縮Accept-Language: zh-CN,zh;q=0.9 # 語言
1、基于host分發(fā)http { upstream web1 { # 名為web1的反向代理群組 server 192.168.31.42; server 192.168.31.52; } upstream web2 { # 名為web2的反向代理群組 server 192.168.31.43; server 192.168.31.53; } server { # web1虛擬主機 listen 80; server_name www.web1.com; # 基于域名分發(fā)必須有域名 location / { proxy_pass http://web1; } } server { # web2虛擬主機 listen 80; server_name www.web2.com; # 基于域名分發(fā)必須有域名 location / { proxy_pass http://web2; } }}
基于域名的分發(fā)測試:[root@web02 ~]# elinks http://www.web1.com -dump web01[root@web02 ~]# elinks http://www.web1.com -dump web01[root@web02 ~]# elinks http://www.web1.com -dump web01[root@web02 ~]# elinks http://www.web2.com -dump web02[root@web02 ~]# elinks http://www.web2.com -dump web02[root@web02 ~]# elinks http://www.web2.com -dump web02
2、基于開發(fā)語言分發(fā)# 192.168.31.40分發(fā)器上nginx配置http { upstream php { server 192.168.31.42; } upstream html { server 192.168.31.43; } server { location ~* /.php$ { # 以php結尾的 proxy_pass http://php; } location ~* /.html$ { # 以html結尾的 proxy_pass http://html; } }}
測試驗證:# 安裝php環(huán)境$ yum install httpd php # 安裝apache和php# 啟動apache,自帶php$ systemctl start httpd# 編寫php文件$ echo "<?php phpinfo(); ?>" > /var/www/html/index.php# 訪問192.168.31.40/index.php 可以看到php-info信息頁面# 訪問192.168.31.40/index.html 可以看到web02
3、基于瀏覽器的分發(fā)$ vim /usr/local/nginx/conf/nginx.confhttp { server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } server { listen 81; server_name localhost; location / { root web3; index index.html index.htm } }}$ mkdir /usr/local/nginx/web3$ echo web03 > /usr/local/nginx/web3/index.html$ /user/local/nginx/sbin/nginx
(2)基于瀏覽器分發(fā)的分發(fā)器配置upstream elinks { server 192.168.31.42; }upstream chrome { server 192.168.31.43; }upstream any { server 192.168.31.42:81; }server { listen 80; server_name www.web1.com; location / { proxy_pass http://any; if ( $http_user_agent ~* Elinks ) { proxy_pass http://elinks; } if ( $http_user_agent ~* chrome ) { proxy_pass http://chrome; } }}
(3)訪問測試upstream bj.server { server 192.168.31.42; # web01}upstream sh.server { server 192.168.31.43; # web02}upstream default.server { server 192.168.31.42:81; # web03}geo $geo { # IP庫 default default; 192.168.31.241/32 bj; # 杭州 192.168.31.242/32 sh; # 杭州}server { listen 80; server_name www.web1.com; location / { proxy_pass http://$geo.server$request_uri; }}
關鍵詞:收藏,新手,建議,使用,程序,經驗,把手