共計 7077 個字符,預計需要花費 18 分鐘才能閱讀完成。
這篇文章主要介紹“nginx 負載均衡如何配置”,在日常操作中,相信很多人在 nginx 負載均衡如何配置問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”nginx 負載均衡如何配置”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
1、nginx 以及相關模塊源碼安裝:
我們先進行源碼所需要的模塊
— nginx-1.13.7.tar.gz
— openssl-1.1.0g.tar.gz
— pcre-8.41.tar.gz
— zlib-1.2.11.tar.gz
對應下載地址:
wget http://nginx.org/download/nginx-1.13.7.tar.gz wget https://www.openssl.org/source/openssl-1.1.0g.tar.gz wget http://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz wget http://www.zlib.net/zlib-1.2.11.tar.gz
(1)解壓 nginx:
(2)解壓 openssl:
(3)解壓 pcre:
(4)解壓 zlib:
(5)進行配置,先進入到 nginx 里面去,然后執行下面語句:
./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_stub_status_module --with-stream --with-pcre=/home/txp/share/nginx/pcre-8.41 --with-zlib=/home/txp/share/nginx/zlib-1.2.11 --with-openssl=/home/txp/share/nginx/openssl-1.1.0g
然后直接 make:
然后接著再 sudo make install:
最終我們可以看到在 /usr/local/nginx/ 目錄下看到安裝的 nginx:
現在我們可以試著來運行 nginx,并進行訪問(下面的訪問成功):
這里小結一下:
很多開源軟件的安裝步驟大概都差不多是下面這樣的套路(比如等下我們下面要安裝的模塊,也是這樣安裝的思路, 所以這里就不造輪子了)
— ./cofigure
— make
–sudo make install
2、自己寫 conf 文件
在平時的開發過程中,主要我們要去配置它的 conf 文件夾下的 nginx.conf 文件
root@ubuntu:/usr/local/nginx# ls client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
這個文件原本內容是:
#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; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 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$ { # proxy_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 # #location ~ /\.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; # } #} }
這里內容比較多,我們現在自己來寫一個配置文件來實現 nginx 的訪問:
txp@ubuntu:/usr/local/nginx$ sudo mkdir demo_conf txp@ubuntu:/usr/local/nginx$ cd demo_conf/ txp@ubuntu:/usr/local/nginx$ vim demo_conf/demo.conf
worker_processes 4;#進程數量 events{ worker_connections 1024;#并發訪問數量 } http{ server { listen 8888;#監聽端口 server_name localhost;#服務器名稱 client_max_body_size 100m;#訪問的數量大小 location / { root /usr/local/nginx/html/ # 訪問這個本地服務器里面的這個 html 頁面 } } }
現在我們來看一下我們自己配置的是否成功,先把之前的 nginx 關閉掉:
./sbin/nginx -s stop
然后再執行這個配置文件:
./sbin/nginx -c demo_conf/demo.conf
這里擴展一下基礎知識點:
Nginx 由配置文件中指定的指令控制的模塊組成。指令分為簡單指令和塊指令。一個簡單的指令由空格分隔的名稱和參數組成,并以分號 (;) 結尾。塊指令具有與簡單指令相同的結構,但不是以分號結尾,而是以大括號 ({和}) 包圍的一組附加指令結束。如果塊指令可以在大括號內部有其他指令,則稱為上下文(例如:events,http,server 和 location)。配置文件中放置在任何上下文之外的偽指令都被認為是主上下文。events 和 http 指令駐留在主上下文中,server 在 http 中的,而 location 在 http 塊中。
3、負載均衡、反向代理和靜態資源的訪問演示:
– 反向代理原理(ReverseProxy):它是指以代理服務器來接受 internet 上的連接請求,然后將請求轉發給內部網絡上的服務器,并將從服務器上得到的結果返回給 internet 上請求連接的客戶端, 簡單來說就是真實的服務器不能直接被外部網絡訪問, 想要訪問必須通過代理,如下圖所示:
上圖中有兩個網關,一個是 nginx 應用層網關,一個路由器硬件網關,nginx 和各服務器都是在同一個局域網里面; 路由器它做了一個端口映射 (nat) 直接訪問到 nginx, 給人的感覺 nginx 就在公網上面;
注意這里的服務器對外不提供服務的,通過 nginx 代理來向外提供服務; 外面訪問的是公網 ip, 然后通過端口映射找到 nginx
現在我們用 nginx 做代理配置(比如說我這里用 143 的這臺機器代理 141 這臺機器):
worker_processes 4; events{ worker_connections 1024; } http{ server { listen 8888; server_name localhost; client_max_body_size 100m; location / { root /usr/local/nginx/html/; proxy_pass http://192.168.29.141; } } }
注意:141 的那臺機器也安裝 nginx 了,然后當我訪問 143 這臺機器的時候,其實訪問的是 141 這臺機器的內容,這就是代理的使用了:
— 負載均衡:從負載均衡四個字來看,肯定是用來減輕服務器的訪問壓力的; 比如說當一臺服務器的單位時間內的訪問量越大時,服務器壓力就越大,大到超過自身承受能力時,服務器就會崩潰(比如每年雙十一活動,淘寶就使用了 nginx 的負載均衡的功能,不然當天那么多的用戶活躍在淘寶上,服務器肯定吃不消啊!)。因此為了避免服務器崩潰,讓用戶有更好的體驗,我們通過負載均衡的方式來分擔服務器壓力。我們可以建立很多很多服務器,組成一個服務器集群,當用戶訪問網站時,先訪問一個中間服務器(也就是我們的 nginx),在讓這個中間服務器在服務器集群中選擇一個壓力較小的服務器,然后將該訪問請求引入該服務器。如此以來,用戶的每次訪問,都會保證服務器集群中的每個服務器壓力趨于平衡,分擔了服務器壓力,避免了服務器崩潰的情況。
下面我演示負載均衡案例:
worker_processes 4; events{ worker_connections 1024; } http{ upstream backend{ server 192.168.29.142 weight=2;//weight 表示權重 server 192.168.29.141 weight=1; } server { listen 8888; server_name localhost; client_max_body_size 100m; location / { # root /usr/local/nginx/html/; # proxy_pass http://192.168.29.141; proxy_pass http://backend; } } }
注意:權重表示被訪問的更多,這里由于我三臺機器都安裝了 nginx,所以內容顯示看不出什么不同之處來,其實 142 的機器被訪問了 2 次,141 的機器被訪問了 1 次,我這里有三臺機器:141、142、143:
— 訪問靜態資源(圖片和視頻)
這里我在 143 的機器上放了幾張圖片,然后在 /usr/local/nginx 目錄下創建了一個 images 文件夾,然后把 143 機器上的圖片 copy 到 images 下面來:
root@ubuntu:/usr/local/nginx# ls client_body_temp fastcgi_temp images proxy_temp scgi_temp vip_conf conf html logs sbin uwsgi_temp root@ubuntu:/usr/local/nginx# mv /home/txp/share/nginx/*.png images/ root@ubuntu:/usr/local/nginx# ls client_body_temp fastcgi_temp images proxy_temp scgi_temp vip_conf conf html logs sbin uwsgi_temp root@ubuntu:/usr/local/nginx# cd images/ root@ubuntu:/usr/local/nginx/images# ls 1.png 2.png 3.png
然后進行 conf 文件配置:
worker_processes 4; events{ worker_connections 1024; } http{ upstream backend{ server 192.168.29.142 weight=2; server 192.168.29.141 weight=1; } server { listen 8888; server_name localhost; client_max_body_size 100m; location / { # root /usr/local/nginx/html/; # proxy_pass http://192.168.29.141; proxy_pass http://backend; } location /images/ { root /usr/local/nginx/; } } }
實現結果如下:
下面我在演示一下視頻訪問,同樣,我創建一個 media 目錄,然后把 143 機器上的 test.mp4copy 到 media 目錄來:
root@ubuntu:/usr/local/nginx# mv /home/txp/share/nginx/test.mp4 media/ root@ubuntu:/usr/local/nginx# cd media/ root@ubuntu:/usr/local/nginx/media# ls test.mp4
conf 文件配置:
worker_processes 4; events{ worker_connections 1024; } http{ upstream backend{ server 192.168.29.142 weight=2; server 192.168.29.141 weight=1; } server { listen 8888; server_name localhost; client_max_body_size 100m; location / { # root /usr/local/nginx/html/; # proxy_pass http://192.168.29.141; proxy_pass http://backend; } location /images/ { root /usr/local/nginx/; } location ~\.(mp3|mp4) # 這里利用了正則表達式 root /usr/local/nginx/media/; } } }
結果如下:
到此,關于“nginx 負載均衡如何配置”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!