簡單實現(xiàn)Nginx的反向代理+負(fù)載均衡

一、引言

上次我們體驗了Nginx反向代理的使用,配置是非常簡單的,一句配置搞定。這章我們來講講在Nginx如何使用反向代理+負(fù)載均衡。負(fù)載均衡估計程序員都聽說過,比如開發(fā)一個電商、web端項目什么后期優(yōu)化需要做負(fù)載均衡,不然同時10w用戶同時訪問,程序就容易相對應(yīng)的崩潰。

所謂負(fù)載均衡,是由多臺服務(wù)器或服務(wù)共同完成一個功能點,從而達(dá)到負(fù)載均衡的效果。打個比方:用戶請求發(fā)起一個請求,網(wǎng)站顯示的圖片量又比較大,如果說這個時候有N個用戶同時訪問,那么全部的工作量都放在了一臺服務(wù)器上,指不定什么時候就崩潰了。如果說有多臺服務(wù)器平分這個任務(wù),那么這樣就很輕松了,效率也會有相對應(yīng)的提高。

二、實現(xiàn)

proxy_pass如何指向多臺服務(wù)器?

答:把多臺服務(wù)器用upstream綁定在一起并起一個組名,然后使用proxy_pass指向該組即可。

小編為了做演示,使用了tomcat發(fā)布了一個web頁面,頁面簡簡單單就一張圖片,代碼如下:

<html>

<head>

????<title>welcome</title>

</head>

<body>

????<img src="/images/test.jpg"/>

</body>

</html>


實現(xiàn)效果:訪問這個頁面時,由多個服務(wù)來提供圖片的顯示。

實現(xiàn)步驟:

1、先建立幾個虛擬主機(jī),有多少個服務(wù)提供就可以創(chuàng)建多少個,小編在同一臺服務(wù)器進(jìn)行演示,就只創(chuàng)建兩個了。端口分別是81、82,也有分別的日志文件保存。

? server {

? ? ? ? listen 81;

? ? ? ? server_name localhost;

? ? ? ? location / {

? ? ? ? ? root /var/www;

? ? ? ? ? index index.html;

? ? ? ? }

? ? ? ? access_log logs/access_81.log main;

? }

? server {

? ? ? ? listen 82;

? ? ? ? server_name localhost;

? ? ? ? location / {

? ? ? ? ? root /var/www;

? ? ? ? }

? ? ? ? access_log logs/access_82.log main;

? }

2、使用upstream綁定多個虛擬主機(jī),起名為imgserver。綁定了如上兩個虛擬主機(jī),并設(shè)置一系列參數(shù)。

weight = 1 //表示權(quán)重,意思就是優(yōu)先誰來處理這次請求,這里小編設(shè)置兩個都是一樣的。

max_fails = 2 //連接失敗次數(shù),如果該地址連接失敗兩次,則表示該服務(wù)器已經(jīng)掛了,就不會在分配任務(wù)給它。

fail_timeout = 3 //超時時長,多久沒連接上則表示連接失敗

upstream imgserver {

? ? ? ? server 111.231.51.81:81 weight=1 max_fails=2 fail_timeout=3;

? ? ? ? server 111.231.51.81:82 weight=1 max_fails=2 fail_timeout=3;

}

3、proxy_pass 指向該組即可。

通過ip訪問,默認(rèn)就是80端口,會轉(zhuǎn)發(fā)到tomcat發(fā)布的服務(wù)上,如果有請求地址中包含images,則會由upstream分配給不同的地址處理。 訪問地址:http://111.231.51.81/?,最后的效果可以查看不同的日志文件,則可以區(qū)分是哪一個處理的請求。

? ? server {

? ? ? ? listen 80;

? ? ? ? server_name 111.231.51.81;

? ? ? ? location /{

? ? ? ? ? ? proxy_pass http://111.231.51.81:8086/;

? ? ? ? }

? ? ? ? location ~* images {

? ? ? ? ? ? proxy_pass http://imgserver;

? ? ? ? }

? ? }


三、整個配置文件展示,如果還有不清楚的小伙伴,可以參考一下,最好自己動動小手實戰(zhàn)一遍喲。以前文章中的實戰(zhàn)演示也包含在其中了。

#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"';

? ? #tcp_nopush? ? on;

? ? #keepalive_timeout? 0;

? ? keepalive_timeout? 65;

? ? #gzip? on;

? upstream imgserver {

server 111.231.51.81:81 weight=1 max_fails=2 fail_timeout=3;

server 111.231.51.81:82 weight=1 max_fails=2 fail_timeout=3;

? }


? server {

listen 81;

server_name localhost;

location / {

? root /var/www;

}

access_log logs/access_81.log main;

? }

? server {

listen 82;

server_name localhost;

location / {

? root /var/www;

}

access_log logs/access_82.log main;

? }


? server {

listen 80;

server_name www.suyouge.com;

location /{

? proxy_pass http://www.suyouge.com/;

}

? ? }

? ? server {

listen 80;

server_name 111.231.51.81;

location /{

? ? proxy_pass http://111.231.51.81:8086/;

}

location ~* images {

? ? proxy_pass http://imgserver;

}? ?

? ? }

? ? server {

? ? ? ? listen? ? ? 8088;

? ? ? ? server_name? localhost;

? ? ? ? #charset koi8-r;

? ? ? ? #access_log? logs/host.access.log? main;


location / {

? # if ($remote_addr = 116.238.62.103) {

? # return 404;

? #? } ?

? if ($http_user_agent ~ Firefox) {

set $isfox? 1;

? }

? if ($fastcgi_script_name ~ firefox.html){

set $isfox? 0;

? }

? if ($isfox = 1){

rewrite ^.*$ /404.html;break;

? } ?

? ? root /usr/local/nginx/html;

? index index.html index.htm;

}

? ? ? ? # 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? t

? ? ? ? #? ? 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;

? ? ? ? server_name? localhost;

? ? ? ? ssl? ? ? ? ? ? ? ? ? on;

? ? ? ? ssl_certificate? ? ? 1_www.suyouge.com_bundle.crt;

? ? ? ? ssl_certificate_key? 2_www.suyouge.com.key;

? ? ? ? ssl_session_timeout? 5m;

? ? ? ? ssl_protocols? SSLv2 SSLv3 TLSv1;

? ? ? ? ssl_ciphers? HIGH:!aNULL:!MD5;

? ? ? ? ssl_prefer_server_ciphers? on;

location / {

? ? ? ? ? ? root? /var/www/html;

? ? ? ? ? ? index? index.html index.htm;

? ? ? ? }

? ? }

}




https://www.suyouge.com

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容