您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)Nginx 中怎么實(shí)現(xiàn)負(fù)載均衡與反向代理,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
一、輪詢配置
#定義后端服務(wù)器組 upstream nginx-test{ server 192.168.0.128; server 192.168.0.127; } server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; root "G:/phpstudy/nginx/html"; location / { index index.html index.htm index.php l.php; autoindex on; proxy_pass http://nginx-test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9001; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } }
二、輪詢加權(quán)配置
#定義后端服務(wù)器組 upstream nginx-test{ server 192.168.0.128 weight=2; server 192.168.0.127; server 192.168.0.126 backup; # 備份服務(wù)器,其他服務(wù)器宕機(jī)后啟動(dòng) } server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; root "G:/phpstudy/nginx/html"; location / { index index.html index.htm index.php l.php; autoindex on; proxy_pass http://nginx-test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
三、IP Hash配置
#定義后端服務(wù)器組 upstream nginx-test{ ip_hash; server 192.168.0.128; server 192.168.0.127; server 192.168.0.126; } server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; root "G:/phpstudy/nginx/html"; location / { index index.html index.htm index.php l.php; autoindex on; proxy_pass http://nginx-test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
四、負(fù)載均衡與反向代理區(qū)別
1、負(fù)載均衡需要通過反向代理來實(shí)現(xiàn)
2、反向代理就是指nginx作為前端服務(wù)器,將請(qǐng)求轉(zhuǎn)發(fā)到后端,再將后端服務(wù)器的結(jié)果,返回給客戶端
它在中間做了一個(gè)代理服務(wù)器的角色
3、負(fù)載均衡對(duì)反向代理增加了一些策略,因?yàn)楹蠖耸嵌嗯_(tái)服務(wù)器,nginx會(huì)根據(jù)設(shè)定的策略將請(qǐng)求轉(zhuǎn)發(fā)給一個(gè)相對(duì)空閑的服務(wù)器,對(duì)負(fù)載進(jìn)行分流,減輕服務(wù)器壓力
五、反向代理配置
#定義后端服務(wù)器組 upstream nginx-test{ server 192.168.0.127; } server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; root "G:/phpstudy/nginx/html"; #代理配置參數(shù) proxy_connect_timeout 180; proxy_send_timeout 180; proxy_read_timeout 180; proxy_set_header Host $host; proxy_set_header X-Forwarder-For $remote_addr; location / { proxy_pass http://nginx-test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
看完上述內(nèi)容,你們對(duì)Nginx 中怎么實(shí)現(xiàn)負(fù)載均衡與反向代理有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。