訂閱
糾錯(cuò)
加入自媒體

nginx負(fù)載均衡一些配置的實(shí)戰(zhàn)演示!

       #
       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.a(chǎn)lias;
   #    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;
   #    }
   #}

這里內(nèi)容比較多,我們現(xiàn)在自己來(lái)寫(xiě)一個(gè)配置文件來(lái)實(shí)現(xiàn)nginx的訪問(wèn):

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;#進(jìn)程數(shù)量
events{
       worker_connections 1024;#并發(fā)訪問(wèn)數(shù)量

http{
       server {
               listen 8888;#監(jiān)聽(tīng)端口
               server_name localhost;#服務(wù)器名稱(chēng)
               client_max_body_size 100m;#訪問(wèn)的數(shù)量大小
               location / {
               root /usr/local/nginx/html/ #訪問(wèn)這個(gè)本地服務(wù)器里面的這個(gè)html頁(yè)面
               }
       }

現(xiàn)在我們來(lái)看一下我們自己配置的是否成功,先把之前的nginx關(guān)閉掉:

./sbin/nginx -s stop

然后再執(zhí)行這個(gè)配置文件:

./sbin/nginx -c demo_conf/demo.conf

這里擴(kuò)展一下基礎(chǔ)知識(shí)點(diǎn):

Nginx 由配置文件中指定的指令控制的模塊組成。指令分為簡(jiǎn)單指令和塊指令。一個(gè)簡(jiǎn)單的指令由空格分隔的名稱(chēng)和參數(shù)組成,并以分號(hào)(;)結(jié)尾。塊指令具有與簡(jiǎn)單指令相同的結(jié)構(gòu),但不是以分號(hào)結(jié)尾,而是以大括號(hào)({和})包圍的一組附加指令結(jié)束。如果塊指令可以在大括號(hào)內(nèi)部有其他指令,則稱(chēng)為上下文(例如:events,http,server 和 location)。配置文件中放置在任何上下文之外的偽指令都被認(rèn)為是主上下文。events 和 http 指令駐留在主上下文中,server 在 http 中的,而 location 在 http 塊中。

3、負(fù)載均衡、反向代理和靜態(tài)資源的訪問(wèn)演示:

--反向代理原理(ReverseProxy):它是指以代理服務(wù)器來(lái)接受internet上的連接請(qǐng)求,然后將請(qǐng)求轉(zhuǎn)發(fā)給內(nèi)部網(wǎng)絡(luò)上的服務(wù)器,并將從服務(wù)器上得到的結(jié)果返回給internet上請(qǐng)求連接的客戶(hù)端,簡(jiǎn)單來(lái)說(shuō)就是真實(shí)的服務(wù)器不能直接被外部網(wǎng)絡(luò)訪問(wèn),想要訪問(wèn)必須通過(guò)代理,如下圖所示:

上圖中有兩個(gè)網(wǎng)關(guān),一個(gè)是nginx應(yīng)用層網(wǎng)關(guān),一個(gè)路由器硬件網(wǎng)關(guān),nginx和各服務(wù)器都是在同一個(gè)局域網(wǎng)里面;路由器它做了一個(gè)端口映射(nat)直接訪問(wèn)到nginx,給人的感覺(jué)nginx就在公網(wǎng)上面;

注意這里的服務(wù)器對(duì)外不提供服務(wù)的,通過(guò)nginx代理來(lái)向外提供服務(wù);外面訪問(wèn)的是公網(wǎng)ip,然后通過(guò)端口映射找到nginx

現(xiàn)在我們用nginx做代理配置(比如說(shuō)我這里用143的這臺(tái)機(jī)器代理141這臺(tái)機(jī)器):

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的那臺(tái)機(jī)器也安裝nginx了,然后當(dāng)我訪問(wèn)143這臺(tái)機(jī)器的時(shí)候,其實(shí)訪問(wèn)的是141這臺(tái)機(jī)器的內(nèi)容,這就是代理的使用了:

-- 負(fù)載均衡:從負(fù)載均衡四個(gè)字來(lái)看,肯定是用來(lái)減輕服務(wù)器的訪問(wèn)壓力的;比如說(shuō)當(dāng)一臺(tái)服務(wù)器的單位時(shí)間內(nèi)的訪問(wèn)量越大時(shí),服務(wù)器壓力就越大,大到超過(guò)自身承受能力時(shí),服務(wù)器就會(huì)崩潰(比如每年雙十一活動(dòng),淘寶就使用了nginx的負(fù)載均衡的功能,不然當(dāng)天那么多的用戶(hù)活躍在淘寶上,服務(wù)器肯定吃不消。。。因此為了避免服務(wù)器崩潰,讓用戶(hù)有更好的體驗(yàn),我們通過(guò)負(fù)載均衡的方式來(lái)分擔(dān)服務(wù)器壓力。我們可以建立很多很多服務(wù)器,組成一個(gè)服務(wù)器集群,當(dāng)用戶(hù)訪問(wèn)網(wǎng)站時(shí),先訪問(wèn)一個(gè)中間服務(wù)器(也就是我們的nginx),在讓這個(gè)中間服務(wù)器在服務(wù)器集群中選擇一個(gè)壓力較小的服務(wù)器,然后將該訪問(wèn)請(qǐng)求引入該服務(wù)器。如此以來(lái),用戶(hù)的每次訪問(wèn),都會(huì)保證服務(wù)器集群中的每個(gè)服務(wù)器壓力趨于平衡,分擔(dān)了服務(wù)器壓力,避免了服務(wù)器崩潰的情況。

下面我演示負(fù)載均衡案例:

worker_processes 4;
events{
       worker_connections 1024;

http{
       upstream backend{
               server 192.168.29.142 weight=2;//weight表示權(quán)重
               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;
               }
       }

注意:權(quán)重表示被訪問(wèn)的更多,這里由于我三臺(tái)機(jī)器都安裝了nginx,所以?xún)?nèi)容顯示看不出什么不同之處來(lái),其實(shí)142的機(jī)器被訪問(wèn)了2次,141的機(jī)器被訪問(wèn)了1次,我這里有三太機(jī)器:141、142、143:

-- 訪問(wèn)靜態(tài)資源(圖片和視頻)

這里我在143的機(jī)器上放了幾張圖片,然后在/usr/local/nginx目錄下創(chuàng)建了一個(gè)images文件夾,然后把143機(jī)器上的圖片copy到images下面來(lái):

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

然后進(jìn)行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/;
               }
       }

實(shí)現(xiàn)結(jié)果如下:

下面我在演示一下視頻訪問(wèn),同樣,我創(chuàng)建一個(gè)media目錄,然后把143機(jī)器上的test.mp4copy到media目錄來(lái):

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) #這里利用了正則表達(dá)式
                       root /usr/local/nginx/media/;
               }
       }

結(jié)果如下:

三、總結(jié)

今天就暫時(shí)總結(jié)這么多吧,還有cgi和fastcgi的使用和區(qū)別,就暫時(shí)不講了,如果哪天有用到,再來(lái)實(shí)戰(zhàn)演示。

<上一頁(yè)  1  2  3  
聲明: 本文由入駐維科號(hào)的作者撰寫(xiě),觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問(wèn)題,請(qǐng)聯(lián)系舉報(bào)。

發(fā)表評(píng)論

0條評(píng)論,0人參與

請(qǐng)輸入評(píng)論內(nèi)容...

請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字

您提交的評(píng)論過(guò)于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

  • 看不清,點(diǎn)擊換一張  刷新

暫無(wú)評(píng)論

暫無(wú)評(píng)論

    掃碼關(guān)注公眾號(hào)
    OFweek人工智能網(wǎng)
    獲取更多精彩內(nèi)容
    文章糾錯(cuò)
    x
    *文字標(biāo)題:
    *糾錯(cuò)內(nèi)容:
    聯(lián)系郵箱:
    *驗(yàn) 證 碼:

    粵公網(wǎng)安備 44030502002758號(hào)