網(wǎng)上整理的關于Nginx 學習材料 2
一 Nginx 簡單的負載均衡配置示例[原創(chuàng)](鏈接地址:http://zyan.cc/post/306/)www.zyan.cc 和 blog.zyan.cc 域名均指向 Nginx 所在的服務器I
一 Nginx 簡單的負載均衡配置示例[原創(chuàng)]
(鏈接地址:http://zyan.cc/post/306/)
www.zyan.cc 和 blog.zyan.cc 域名均指向 Nginx 所在的服務器IP 。
用戶訪問http://www.zyan.cc,將其負載均衡到192.168.1.2:80、192.168.1.3:80、192.168.1.4:80、192.168.1.5:80四臺服務器。
用戶訪問http://blog.zyan.cc,將其負載均衡到192.168.1.7服務器的8080、8081、8082端口。
以下為配置文件nginx.conf :(windows 中配置參數(shù)有點不同,測試過的) user www www;
worker_processes 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#最大文件描述符
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
{
include conf/mime.types;
default_type application/octet-stream;
keepalive_timeout 120;
tcp_nodelay on;
upstream www.zyan.cc {
1 / 93
,server 192.168.1.2:80;
server 192.168.1.3:80;
server 192.168.1.4:80;
server 192.168.1.5:80;
}
upstream blog.zyan.cc {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
}
server
{
listen 80;
server_name www.zyan.cc;
location / {
proxy_pass http://www.zyan.cc;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
log_format www_s135_com '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data1/logs/www.log www_s135_com;
}
server
{
listen 80;
server_name blog.zyan.cc;
location / {
2 / 93
,proxy_pass http://blog.zyan.cc;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
log_format blog_s135_com '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data1/logs/blog.log blog_s135_com;
}
}
附:Nginx 的安裝方法可參照《Nginx 0.5.31 PHP 5.2.4(FastCGI )搭建可承受3萬以上并發(fā)連接數(shù),勝過Apache 10倍的Web 服務器》文章的以下段落(僅做負載均衡,無需支持PHP 的安裝方法):
二、安裝PHP 5.2.4(FastCGI 模式)
4、創(chuàng)建www 用戶和組,以及其使用的目錄:
三、安裝Nginx 0.5.31
1、安裝Nginx 所需的pcre 庫:
2、安裝Nginx
3、創(chuàng)建Nginx 日志目錄
5、啟動Nginx
問答:
1. 如果負載均衡后面的機器都是要用到session 的話,如何很好的解決這個問題呢?
2. 有沒有類似的負載均衡功能? 另外還有一個疑問:
客戶端直接訪問web 服務器時,可以用HTTP_CLIENT_IP、REMOTE_ADDR 這幾個變量看到客戶端的IP ,經(jīng)過squid 或者ngrnx 之后,還能看到客戶端的IP 嗎? 是不是要用HTTP_X_FORWARDED_FOR
—BIG-IP 就是用來做負載均衡的。經(jīng)過squid 或者ngrnx 之后,看到客戶端的IP ,請用HTTP_X_FORWARDED_FOR
3 / 93
,3.A. 如果均衡的域名中有泛域名,或有幾百個域名的話可以配置嗎?
B.ngnix 在做均衡的時候是否已經(jīng)包含了cache 功能?
C.ngnix---squid----apache 取的的HTTP_X_FORWARDED_FOR是squid 服務器的ip 地址,如果想取真實客戶端地址可以做到嗎?
回復:(1)nginx.conf
server_name .s135.com; 即可支持***.s135.com泛域名
(2)nginx負載均衡只做反向代理,有簡單的緩沖,但不像Squid 那樣將cache 存在本機。
(3)我已經(jīng)在nginx.conf 配置文件中增加:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
編譯squid 時加上--enable-follow-x-forwarded-for
然后在squid.conf 中輸入一行:
follow_x_forwarded_for allow all
后端的Apache 取日志(httpd.conf ):
LogFormat "{X-Forwarded-For}i l u t "r" >s b "{Referer}i" "{User-Agent}i" h T" combined
取到的就是用戶真實IP
4. 這個軟件是不錯,但是有一個硬傷,做負載均衡時,好像無法檢測節(jié)點健康狀態(tài)?
5.follow_x_forwarded_for allow all 全開是不是會引起安全問題?
6. 請問,NGINX 是不是基于域名的負載均衡 能不能基于IP 做負載均衡
7. 請問:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
4 / 93
,請問這三個是什么意思啊?
8. 有請張兄指教:
server1與server2都安裝web.
為了省下一臺主機. 在server1上安裝nginx, 做反向代理, 輪循到server1, server2的web 上實現(xiàn)負載均衡.
請問這樣做與單獨使用一臺server3做負載均衡器的區(qū)別是不是非常大?
9. 我想問下,轉發(fā)到后端那幾臺服務器,程序怎么同步?假如PHP 呢?做負載均衡時,好像無法檢測節(jié)點健康狀態(tài)?想問一下,為什么本機不能也接受訪問呢? 例如:a 、b 、c 三臺機器,我在a 上面如同上面配置的,為什么訪問總是在b 、c 來回換,為什么a 上也有網(wǎng)站卻不被訪問到呢?如果我也想訪問a 服務器上的網(wǎng)站需要如何處理?
10. 誠心請教,在php-fpm 模式下,
可以做一組
upstream blog.s135.com {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
}
負載均衡么?
我查閱了一些資料,好像php-fpm 同時只能打開一個端口,如果想實現(xiàn)可行么?
11. 誠心請教,在php-fpm 模式下,
可以做一組
upstream blog.s135.com {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
}
負載均衡么?
我查閱了一些資料,好像php-fpm 同時只能打開一個端口,如果想實現(xiàn)可行么?
12.include conf/mime.types;
5 / 93
,引用mime.types 文件的路勁前面應該不用加conf/,指定的類型文件應該自動會到conf 目錄中去找
13. 請教一下,經(jīng)過Nginx 轉發(fā)后,是不是會轉Http1.1損失為Http1.0?請教一下,經(jīng)過Nginx 轉發(fā)后,是不是會轉Http1.1損失為Http1.0?
14. 安裝php 必須要用fastcgi 模式嗎?我用的是fpm 的,但發(fā)現(xiàn)做負載均衡的時候發(fā)現(xiàn)訪問/message/目錄下的php 文件的時候總是失效,系統(tǒng)自動在/下面去訪問了,不知是什么原因的?
解答:解決了, 剛開始準備將負載均衡器也做為web 使用, 啟用了以下代碼 ,將這幾行刪除即可。
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params;
}
6 / 93
,二 nginx upstream的分配方式
(鏈接地址:http://onlyzq.blog.51cto.com/1228/557848/)
1、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down 掉,能自動剔除。
2、weight
指定輪詢幾率,weight 和訪問比率成正比,用于后端服務器性能不均的情況。 例如:
upstream bakend {
server 192.168.159.10 weight=10;
server 192.168.159.11 weight=10;
}
3、ip_hash
每個請求按訪問ip 的hash 結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session 的問題。
例如:
upstream resinserver{
ip_hash;
server 192.168.159.10:8080;
server 192.168.159.11:8080;
}
4、fair (第三方)
按后端服務器的響應時間來分配請求,響應時間短的優(yōu)先分配。
upstream resinserver{
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按訪問url 的hash 結果來分配請求,使每個url 定向到同一個后端服務器,后端服務器為緩存時比較有效。
例:在upstream 中加入hash 語句,server 語句中不能寫入weight 等其他的參數(shù),hash_method是使用的hash 算法
upstream resinserver{
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
7 / 93
,}
tips:
upstream resinserver{#定義負載均衡設備的Ip 及設備狀態(tài)
ip_hash;
server 127.0.0.1:8000 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6801;
server 127.0.0.1:6802 backup;
}
在需要使用負載均衡的server 中增加
proxy_pass http://resinserver/;
每個設備的狀態(tài)設置為:
1.down 表示單前的server 暫時不參與負載
2.weight 默認為1.weight 越大,負載的權重就越大。
3.max_fails :允許請求失敗的次數(shù)默認為1. 當超過最大次數(shù)時,返回proxy_next_upstream 模塊定義的錯誤
4.fail_timeout:max_fails次失敗后,暫停的時間。
5.backup : 其它所有的非backup 機器down 或者忙的時候,請求backup 機器。所以這臺機器壓力會最輕。
nginx 支持同時設置多組的負載均衡,用來給不用的server 來使用。
client_body_in_file_only 設置為On 可以講client post過來的數(shù)據(jù)記錄到文件中用來做debug client_body_temp_path 設置記錄文件的目錄 可以設置最多3層目錄
location 對URL 進行匹配. 可以進行重定向或者進行新的代理 負載均衡
8 / 93
,三 關于Nginx 的server_name
(鏈接地址:http://onlyzq.blog.51cto.com/1228/535279)
Nginx 中的server_name指令主要用于配置基于名稱的虛擬主機,server_name指令在接到請求后的匹配順序分別為:
1、準確的server_name匹配,例如:
server {
listen 80;
server_name domain.com www.domain.com;
...
}
2、以*通配符開始的字符串:
server {
listen 80;
server_name *.domain.com;
...
}
3、以*通配符結束的字符串:
server {
listen 80;
server_name www.*;
...
}
4、匹配正則表達式:
server {
listen 80;
9 / 93
,server_name ~^(?. ).domain.com$;
...
}
nginx 將按照1,2,3,4的順序?qū)erver name 進行匹配,只有有一項匹配以后就會停止搜索,所以我們在使用這個指令的時候一定要分清楚它的匹配順序(類似于location 指令)。
server_name指令一項很實用的功能便是可以在使用正則表達式的捕獲功能,這樣可以盡量精簡配置文件,畢竟太長的配置文件日常維護也很不方便。下面是2個具體的應用:
1、在一個server 塊中配置多個站點:
server
{
listen 80;
server_name ~^(www.)?(. )$;
index index.php index.html;
root /data/wwwsite/$2;
}
站點的主目錄應該類似于這樣的結構:
/data/wwwsite/domain.com
/data/wwwsite/nginx.org
/data/wwwsite/baidu.com
/data/wwwsite/google.com
這樣就可以只使用一個server 塊來完成多個站點的配置。
2、在一個server 塊中為一個站點配置多個二級域名。
實際網(wǎng)站目錄結構中我們通常會為站點的二級域名獨立創(chuàng)建一個目錄,同樣我們
10 / 93