自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

FreeBSD 8.1下用nginx配置簡單web負(fù)載均衡

原創(chuàng)
系統(tǒng) Linux
本文將介紹如何利用nginx配置簡單的web負(fù)載均衡。文中所用的平臺(tái)是64bit的FreeBSD 8.1。另外,由于Nginx負(fù)載均衡器采用ip_hash來代替默認(rèn)的rr方式,雖然解決了session問題,卻無法保證后端服務(wù)器的負(fù)載均衡。本文對(duì)session問題的解決方案也進(jìn)行了介紹。

【51CTO獨(dú)家特稿】Nginx (“engine x”) 相信大家已經(jīng)很熟悉了,是俄羅斯人Igor Sysoev(塞索耶夫)編寫的一款高性能的 HTTP 和反向代理服務(wù)器。Nginx 已經(jīng)在俄羅斯最大的門戶網(wǎng)站── Rambler Media(www.rambler.ru)上運(yùn)行了3年時(shí)間,同時(shí)俄羅斯超過20%的虛擬主機(jī)平臺(tái)采用Nginx作為反向代理服務(wù)器。

51CTO推薦專題:企業(yè)內(nèi)網(wǎng)開發(fā)環(huán)境部署與管理全攻略(FreeBSD+PHP)

在國內(nèi),已經(jīng)有 新浪博客、新浪播客、網(wǎng)易新聞、六間房、56.com、Discuz!、水木社區(qū)、豆瓣、YUPOO、海內(nèi)、迅雷在線 等多家網(wǎng)站使用 Nginx 作為Web服務(wù)器或反向代理服務(wù)器。

我觀察了國內(nèi)清一色的CDN網(wǎng)站,基本都是用了nginx作為它們的web服務(wù)器。nginx的優(yōu)點(diǎn)如下:

1、高并發(fā)連接:官方測試能夠支撐5萬并發(fā)連接,在實(shí)際生產(chǎn)環(huán)境中跑到2~3萬并發(fā)連接數(shù)。

2、內(nèi)存消耗少:在3萬并發(fā)連接下,開啟的10個(gè)Nginx進(jìn)程才消耗150M內(nèi)存(15M*10=150M)。

3、配置文件非常簡單:風(fēng)格跟程序一樣通俗易懂。

4、成本低廉:Nginx為開源軟件,可以免費(fèi)使用。而購買F5 BIG-IP、NetScaler等硬件負(fù)載均衡交換機(jī)則需要十多萬至幾十萬人民幣。

5、支持Rewrite重寫規(guī)則:能夠根據(jù)域名、URL的不同,將HTTP請(qǐng)求分到不同的后端服務(wù)器群組。

6、內(nèi)置的健康檢查功能:如果 Nginx Proxy后端的某臺(tái)Web服務(wù)器宕機(jī)了,不會(huì)影響前端訪問。

7、節(jié)省帶寬:支持GZIP壓縮,可以添加瀏覽器本地緩存的Header頭。

8、穩(wěn)定性高:用于反向代理,宕機(jī)的概率微乎其微。

9、對(duì)網(wǎng)絡(luò)的依賴性非常之低,理論上ping得通就能正常使用。

本文將介紹如何利用nginx配置簡單的web負(fù)載均衡。文中所用的平臺(tái)是64bit的FreeBSD 8.1。

部分內(nèi)容參考了張宴的相關(guān)文章。

作者簡介:余洪春(博客),網(wǎng)名撫琴煮酒,英文名Andrew.Yu,武漢某外企高級(jí)Linux/Unix系統(tǒng)管理員、項(xiàng)目實(shí)施工程師,紅帽RHCE講師,擅長負(fù)載均衡高可用和中小型證券類和商務(wù)網(wǎng)站架構(gòu),目前關(guān)注網(wǎng)站架構(gòu)研究及網(wǎng)絡(luò)安全。

安裝

cd /usr/ports/www/nginx
make install clean

配置nginx.conf文件

#user  nobody;
worker_processes  4;

#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;
    upstream webserver {
      server 192.168.21.45 weight=1;
      server 192.168.4.45  weight=1;
                        }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

	location / {
	proxy_pass      http://webserver;
	proxy_redirect          off;
	proxy_set_header        Host $host;
	proxy_set_header        X-Real-IP $remote_addr;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	client_max_body_size    10m;
	client_body_buffer_size 128k;
	proxy_connect_timeout   90;
	proxy_send_timeout      90;
	proxy_read_timeout      90;
	proxy_buffer_size       4 32k;
	proxy_temp_file_write_size 64k;
          }


        #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   /usr/local/www/nginx-dist;
        }

        # 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;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

重點(diǎn)語法講解

worker_processes  4; #這個(gè)與服務(wù)器的核數(shù)等同即可
upstream #這個(gè)里面的weight值越大,服務(wù)器所要承擔(dān)的壓力也越大;如果采用ip_hash模塊時(shí),就不能分配weight了,不然Nginx啟動(dòng)時(shí)會(huì)報(bào)錯(cuò)
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
#以上配置是讓后端的web服務(wù)器可以通過X-Forwarded-For獲取客戶端的真實(shí)IP
#允許客戶端請(qǐng)求的最大的單個(gè)文件字節(jié)數(shù)
client_max_body_size     10m;
#緩沖區(qū)代理緩沖用戶端請(qǐng)求的最大字節(jié)數(shù) 可以理解為先保存到本地再傳給用戶
client_body_buffer_size  128k;
#跟后端服務(wù)器連接的超時(shí)時(shí)間_發(fā)起握手等候響應(yīng)超時(shí)時(shí)間
proxy_connect_timeout    90;
#連接成功后_等候后端服務(wù)器響應(yīng)時(shí)間_其實(shí)已經(jīng)進(jìn)入后端的排隊(duì)之中等候處理
proxy_read_timeout       90;
#后端服務(wù)器數(shù)據(jù)回傳時(shí)間_就是在規(guī)定時(shí)間之內(nèi)后端服務(wù)器必須傳完所有的數(shù)據(jù)
proxy_send_timeout       90;
#代理請(qǐng)求緩存區(qū)_這個(gè)緩存區(qū)間會(huì)保存用戶的頭信息以供Nginx進(jìn)行規(guī)則處理,一般只要能保存下頭信息即可
proxy_buffer_size        8k;
#同上 告訴Nginx保存單個(gè)用的幾個(gè)Buffer 最大用多大空間
proxy_buffers            4 32k;
#如果系統(tǒng)很忙的時(shí)候可以申請(qǐng)更大的proxy_buffers 官方推薦*2	
proxy_busy_buffers_size 64k;
#proxy緩存臨時(shí)文件的大小
proxy_temp_file_write_size 64k;

Nginx如何解決session的問題

Nginx負(fù)載均衡器采用ip_hash來代替默認(rèn)的rr方式,即可以將某客戶端IP的請(qǐng)求通過哈希算法定位到同一臺(tái)后端web服務(wù)器上,這樣避免了session丟失,解決了session問題。

但ip_hash指令無法保證后端服務(wù)器的負(fù)載均衡,可能有些后端服務(wù)器接收的請(qǐng)求多,有些后端服務(wù)器接收的請(qǐng)求少;這樣失去了負(fù)載均衡的意義。

我們的解決方案是將用戶的登錄session信息寫進(jìn)后端的Mysql數(shù)據(jù)庫,這個(gè)在后面的PHPCMS系統(tǒng)中也實(shí)現(xiàn)了,效果也不錯(cuò);后來我提出了折衷方案,如果Nginx并發(fā)連接數(shù)(即Nginx負(fù)載均衡器的NginxStatus的active connections)>2000,即采用將session寫進(jìn)MySQL數(shù)據(jù)庫的方法;如果并發(fā)數(shù)小的話,ip_hash效果也是相當(dāng)好的。

【51CTO.com獨(dú)家特稿,轉(zhuǎn)載請(qǐng)注明原文作者和出處?!?/p>

【編輯推薦】

  1. FreeBSD 8.0+Nginx+PHP配置高性能Web平臺(tái)
  2. Nginx 502錯(cuò)誤觸發(fā)條件與解決辦法匯總
  3. 快速部署Python應(yīng)用:Nginx+uWSGI配置詳解

 

 

責(zé)任編輯:yangsai 來源: 51CTO.com
相關(guān)推薦

2011-01-13 15:57:20

FreeBSD 8.1vsftpd

2014-07-28 11:37:49

NginxTomcat

2010-03-25 18:52:15

Nginx負(fù)載均衡

2013-04-22 11:29:14

Nginx

2012-07-31 09:25:42

nginx負(fù)載均衡反向代理

2011-01-07 09:44:13

2010-03-30 13:59:56

Nginx負(fù)載均衡配置

2012-12-07 10:14:48

Nginx負(fù)載均衡

2010-06-29 11:21:58

Web服務(wù)器

2011-01-26 10:46:30

FreeBSD 8.0NginxPHP

2011-04-11 11:05:07

FreeBSD 8.1

2011-11-22 21:26:59

pfSense配置Web服務(wù)器負(fù)載均衡

2019-07-09 15:10:02

Nginx反向代理負(fù)載均衡

2010-05-04 13:32:37

nginx負(fù)載均衡器

2018-06-25 09:54:14

LinuxDNS負(fù)載均衡

2011-04-01 13:41:32

FreeBSD8.1

2010-05-04 13:38:25

nginx負(fù)載均衡器

2017-12-13 15:33:02

LinuxNginxTomcat

2018-03-14 11:13:35

Web服務(wù)器Nginx

2010-05-07 12:23:23

nginx負(fù)載均衡
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)