nginx簡單反向代理總結javascript
nginx 實現負載均衡。由於nginx在處理併發方面的優點,如今這個應用很是常見。
nginx 這個輕量級、高性能的 web server 主要能夠幹兩件事情:php
〉直接做爲http server(代替apache,對PHP須要FastCGI處理器支持);
〉另一個功能就是做爲反向代理服務器實現負載均衡css
如下咱們就來舉例說明如何使用 nginx 實現負載均衡。由於nginx在處理併發方面的優點,如今這個應用很是常見。固然了Apache的 mod_proxy和mod_cache結合使用也能夠實現對多臺app server的反向代理和負載均衡,可是在併發處理方面apache仍是沒有 nginx擅長。html
a.環境:centos.5.7 OS, 同時安裝nginx(8080)和apache(80)和php-fpm ,nginx用來做爲反向代理服務器,放置到一臺apache以前,做爲用戶訪問的入口;
nginx僅僅處理靜態頁面,動態的頁面(php請求)通通都交付給後臺的兩臺apache來處理。
也就是說,能夠把咱們網站的靜態頁面或者文件放置到nginx的目錄下;動態的頁面和數據庫訪問都保留到後臺的apache服務器上。前端
b. 以下介紹兩種方法實現server cluster的負載均衡。
咱們假設前端nginx(爲127.0.0.1:8080)僅僅包含一個靜態頁面index.html,裏面寫this is nginx server
後臺的一個apache服務器(分別爲localhost:80)也能夠兩臺,一臺爲index.php(裏面測試代碼爲this is apache server 192.168.10.114;,假若有另外一臺根目錄僅僅放置一個test.php(裏面測試代碼爲 this is apache server2「;)。java
c.針對不一樣請求的負載均衡:node
d. 在最簡單地構建反向代理的時候 (nginx僅僅處理靜態不處理動態內容,動態內容交給後臺的apache server來處理),咱們具體的設置爲:在nginx.conf中修改:
複製代碼 代碼以下:
location ~ \.php$ {
proxy_pass 127.0.0.1:80 ;
}mysql
〉 這樣當客戶端訪問localhost:8080/index.html的時候,前端的nginx會自動進行響應;
〉當用戶訪問localhost:8080/index.php的時候(這個時候nginx目錄下根本就沒有該文件),可是經過上面的設置 location ~ \.php$(表示正則表達式匹配以.php結尾的文件,詳情參看location是如何定義和匹配的 http://wiki.nginx.org/NginxHttpCoreModule) ,nginx服務器會自動pass給 192.168.10.114的apache服務器了。該服務器下的test.php就會被自動解析,而後將html的結果頁面返回給nginx,而後 nginx進行顯示(若是nginx使用memcached模塊或者squid還能夠支持緩存),輸出結果爲打印server2。linux
1. 先安裝好nginx軟件和php-fpm軟件和apache mysql軟件。
#!bin/sh
yum -y install yum-fastermirror.noarch //安裝fastermirrornginx
rpm -ivh rpmforge-release-0.5.2-2.el5.rf.i386.rpm //安裝rpmforge源
rpm -ivh epel-release-5-4.noarch.rpm //安裝epel源
cp -rf alt.ru.repo /etc/yum.repos.d //把alt.ru.repo拷貝到/etc/yum.repos.d目錄中
yum -y update//更新一下
yum -y install mysql mysql-server //安裝mysql數據庫
chkconfig mysqld on//設置開機啓動
service mysqld start//設置啓動mysqld進程
mysqladmin -u root password 123456//設置mysql密碼
yum -y install nginx php-fpm //安裝nginx和php-fpm
chkconfig nginx on//設置啓動nginx
chkconfig php-fpm on
cp -rf nginx.conf /etc/nginx
cp -rf php-fpm.conf /etc
service nginx start
service php-fpm start
yum -y install php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-snmp php-mcrypt php-mhash php-mbstring php-dba php-bcmath php-tidy php-ncurses php-jpgraph
yum -y install httpd php
chkconfig httpd on
service httpd start
上面的是最基本的命令安裝好nginx和httpd和php-fpm以後就要作準備測試了。
2.修改配置文件
grep -v "#" /etc/nginx/nginx.conf>/etc/nginx.conf.1//去掉不用的#的內容
備份一下cp -rf /etc/nginx/nginx.conf /root
rm -rf /etc/nginx/nginx.conf//刪除原來的配置
mv /etc/nginx/nginx.conf.1 nginx.conf//重命名
vi /etc/nginx/nginx.conf//配置文件以下:
user nginx;
worker_processes 2;
worker_rlimit_nofile 100000;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
gzip on;
gzip_static on;
gzip_comp_level 5;
gzip_min_length 1024;
keepalive_timeout 65;
limit_zone myzone $binary_remote_addr 10m;
include /etc/nginx/conf.d/*.conf;
server {
limit_conn myzone 10;
listen 8080; //這裏是修改的端口默認爲80
server_name _;
location / {
root /usr/share/nginx/html;//這裏是網站的根目錄隨便改哈哈!
index index.php index.html index.htm;//加上index.php
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
proxy_pass http://192.168.10.114:80;
} //這行井號去掉填上apache 80端口由於在同一臺機器上全部就是localhost.
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
}
每一個段落的解釋說明以下:
#運行用戶
user www-data;
#啓動進程,一般設置成和cpu的數量相等
worker_processes 1;
#全局錯誤日誌及PID文件
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
#工做模式及鏈接數上限
events {
use epoll; #epoll是多路複用IO(I/O Multiplexing)中的一種方式,可是僅用於linux2.6以上內核,能夠大大提升nginx的性能
worker_connections 1024;#單個後臺worker process進程的最大併發連接數
# multi_accept on;
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型,類型由mime.type文件定義
include /etc/nginx/mime.types;
default_type application/octet-stream;
#設定日誌格式
access_log /var/log/nginx/access.log;
#sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
#必須設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off,以平衡磁盤與網絡I/O處理速度,下降系統的uptime.
sendfile on;
#tcp_nopush on;
#鏈接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
#開啓gzip壓縮
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#設定請求緩衝
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
#設定負載均衡的服務器列表
upstream mysvr {
#weigth參數表示權值,權值越高被分配到的概率越大
#本機上的Squid開啓3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
server {
#偵聽80端口
listen 80;
#定義使用www.xx.com訪問
server_name www.xx.com;
#設定本虛擬主機的訪問日誌
access_log logs/www.xx.com.access.log main;
#默認請求
location / {
root /root; #定義服務器的默認網站根目錄位置
index index.php index.html index.htm; #定義首頁索引文件的名稱
fastcgi_pass www.xx.com;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
# 定義錯誤提示頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root;
}
#靜態文件,nginx本身處理
location ~ ^/(p_w_picpaths|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/htdocs;
#過時30天,靜態文件不怎麼更新,過時能夠設大一點,若是頻繁更新,則能夠設置得小一點。
expires 30d;
}
#PHP 腳本請求所有轉發到 FastCGI處理. 使用FastCGI默認配置.
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
include fastcgi_params;
}
#設定查看Nginx狀態的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
#禁止訪問 .htxxx 文件
location ~ /\.ht {
deny all;
}
}
}
以上是一些基本的配置,使用Nginx最大的好處就是負載均衡
若是要使用負載均衡的話,能夠修改配置http節點以下:
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型,類型由mime.type文件定義
include /etc/nginx/mime.types;
default_type application/octet-stream;
#設定日誌格式
access_log /var/log/nginx/access.log;
#省略上文有的一些配置節點
#。。。。。。。。。。
#設定負載均衡的服務器列表
upstream mysvr {
#weigth參數表示權值,權值越高被分配到的概率越大
server 192.168.8.1x:3128 weight=5;#本機上的Squid開啓3128端口
server 192.168.8.2x:80 weight=1;
server 192.168.8.3x:80 weight=6;
}
upstream mysvr2 {
#weigth參數表示權值,權值越高被分配到的概率越大
server 192.168.8.x:80 weight=1;
server 192.168.8.x:80 weight=6;
}
#第一個虛擬服務器
server {
#偵聽192.168.8.x的80端口
listen 80;
server_name 192.168.8.x;
#對aspx後綴的進行負載均衡請求
location ~ .*\.aspx$ {
root /root; #定義服務器的默認網站根目錄位置
index index.php index.html index.htm; #定義首頁索引文件的名稱
proxy_pass http://mysvr ;#請求轉向mysvr 定義的服務器列表
#如下是一些反向代理的配置可刪除.
proxy_redirect off;
#後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP
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; #nginx跟後端服務器鏈接超時時間(代理鏈接超時)
proxy_send_timeout 90; #後端服務器數據回傳時間(代理髮送超時)
proxy_read_timeout 90; #鏈接成功後,後端服務器響應時間(代理接收超時)
proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k如下的話,這樣設置
proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳
}
}
}
3.測試
a.[root@zh888 htdocs]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN 1636/snmpd
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 3197/php-cgi
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1815/mysqld
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 1740/smbd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1612/portmap
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 3136/nginx.conf//已經啓動nginx 8080端口
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1966/dnsmasq
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 1740/smbd
tcp 0 0 :::80 :::* LISTEN 3029/httpd //httpd已經啓動爲80端口
tcp 0 0 :::22 :::* LISTEN 1682/sshd
b.[root@zh888 htdocs]# ls /usr/local/apache/htdocs/
index.php
[root@zh888 htdocs]# ls /usr/share/nginx/html/
index.html
[root@zh888 htdocs]# cat /usr/local/apache/htdocs/index.php
this is apache server 192.168.10.114
[root@zh888 htdocs]# cat /usr/share/nginx/html/index.html
this is nginx server
[root@zh888 htdocs]# /usr/local/apache/bin/ab -c50 -n 50 http://192.168.10.114:8080/index.html //對index.html 50次請求和50次index.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.10.114 (be patient).....done
Server Software: nginx
Server Hostname: 192.168.10.114
Server Port: 8080
Document Path: /index.html
Document Length: 21 bytes
Concurrency Level: 50
Time taken for tests: 0.057 seconds
Complete requests: 50
Failed requests: 0
Write errors: 0
Total transferred: 11200 bytes
HTML transferred: 1050 bytes
Requests per second: 875.09 [#/sec] (mean)
Time per request: 57.137 [ms] (mean)
Time per request: 1.143 [ms] (mean, across all concurrent requests)
Transfer rate: 191.43 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 10 5.7 10 19
Processing: 1 22 12.6 22 43
Waiting: 1 22 12.6 22 43
Total: 20 32 6.9 32 43
Percentage of the requests served within a certain time (ms)
50% 32
66% 36
75% 38
80% 39
90% 42
95% 42
98% 43
99% 43
100% 43 (longest request)
apache 處理50次請求,50次index.php
[root@zh888 htdocs]# /usr/local/apache/bin/ab -c50 -n 50 http://192.168.10.114/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.10.114 (be patient).....done
Server Software: Apache/2.2.19
Server Hostname: 192.168.10.114
Server Port: 80
Document Path: /index.php
Document Length: 37 bytes
Concurrency Level: 50
Time taken for tests: 0.132 seconds
Complete requests: 50
Failed requests: 0
Write errors: 0
Total transferred: 11100 bytes
HTML transferred: 1850 bytes
Requests per second: 378.73 [#/sec] (mean)
Time per request: 132.019 [ms] (mean)
Time per request: 2.640 [ms] (mean, across all concurrent requests)
Transfer rate: 82.11 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 57 33.9 58 114
Processing: 18 61 25.9 62 105
Waiting: 0 51 30.5 53 103
Total: 105 118 7.9 119 132
Percentage of the requests served within a certain time (ms)
50% 119
66% 123
75% 125
80% 127
90% 130
95% 131
98% 132
99% 132
100% 132 (longest request)
nginx反向50次請求50次index.php
[root@zh888 htdocs]# /usr/local/apache/bin/ab -c50 -n 50 http://192.168.10.114:8080/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.10.114 (be patient).....done
Server Software: nginx
Server Hostname: 192.168.10.114
Server Port: 8080
Document Path: /index.php
Document Length: 162 bytes
Concurrency Level: 50
Time taken for tests: 0.102 seconds
Complete requests: 50
Failed requests: 10
(Connect: 0, Receive: 0, Length: 10, Exceptions: 0)
Write errors: 0
Non-2xx responses: 40
Total transferred: 14170 bytes
HTML transferred: 6850 bytes
Requests per second: 489.35 [#/sec] (mean)
Time per request: 102.177 [ms] (mean)
Time per request: 2.044 [ms] (mean, across all concurrent requests)
Transfer rate: 135.43 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 31 17.1 31 59
Processing: 15 31 17.4 25 79
Waiting: 0 24 21.3 19 78
Total: 44 62 10.1 62 82
Percentage of the requests served within a certain time (ms) 50% 62 66% 67 75% 70 80% 71 90% 75 95% 79 98% 82