安裝Nginx
nginx默認使用80端口,請確保80未被使用
Nginx
# wget http://www.nginx.org/download/nginx-[version].tar.gz
Nginx cache purge 模塊(可選)
# wget http://labs.frickle.com/files/ngx_cache_purge-[version].tar.gz
#編譯安裝
./configure \
--prefix=/usr/local/nginx-[version] \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--add-module=../ngx_cache_purge-1.3
# make
# make install
#啟動,停止,重載nginx
/usr/local/nginx-[version]/sbin/nginx #啟動
/usr/local/nginx-[version]/sbin/nginx -t #測試,檢測配置
/usr/local/nginx-[version]/sbin/nginx -s stop
/usr/local/nginx-[version]/sbin/nginx -s reload
打開瀏覽器,訪問nginx地址,出現welcome nginx則配置成功
nginx 負載
新建配置文件blance-test.conf然后在nginx.conf 中include blance-test.conf
upstream blance-test {
server 192.168.1.11:80;
server 192.168.1.22:80;
}
server
{
listen 80;
server_name www.example.com;
location / {
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;
proxy_pass http://blance-test;
}
access_log logs/blance-test_access.log;
}
這里需要講一下proxy_set_header....
重點看下proxy_set_header X-Forwarded-For
當使用了代理時,web服務器無法取得真實IP,為了避免這個情況,
代理服務器通常會增加一個叫做x_forwarded_for的頭信息,
把連接它的客戶端IP(即客戶端IP)加到這個頭信息里,
這樣就能保證網站的web服務器能獲取到真實IP
$host
#請求中的主機頭(Host)字段,如果請求中的主機頭不可用或者空,
則為處理請求的server名稱(處理請求的server的server_name的值)。小寫,不含端口。
$remote_addr;
#客戶端的IP地址(中間無代理則是真實客戶端IP,有代理則是代理IP)
$proxy_add_x_forwarded_for;
#就是$http_x_forwarded_for加上$remote_addr
官方解釋
$proxy_add_x_forwarded_for
the “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.
nginx 反向代理
可選配置,與http同級
client_max_body_size 50m; #緩沖區代理緩沖用戶端請求的最大字節數
client_body_buffer_size 256k;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
proxy_connect_timeout 300s; #nginx跟后端服務器連接超時時間(代理連接超時)
proxy_read_timeout 300s; #連接成功后,后端服務器響應時間(代理接收超時)
proxy_send_timeout 300s;
proxy_buffer_size 64k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
proxy_buffers 4 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置
proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大于這個值,將從upstream服務器傳遞請求,而不緩沖到磁盤
proxy_ignore_client_abort on; #不允許代理端主動關閉連接
新建配置文件reverse-proxy.conf然后在nginx.conf 中include reverse-proxy.conf
server {
listen 7001;
server_name 192.168.1.202:7001;
charset utf-8;
location /console {
# proxy_set_header Host $host:$proxy_port;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.1.11:7001;
}
access_log logs/192.168.1.11:7001_access.log;
}
server {
listen 8001;
server_name 192.168.1.202:8001;
charset utf-8;
location / {
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.1.11:8001;
}
access_log logs/192.168.1.11:8001_access.log;
}
這里要說明下
server_name
#主要用于配置基于名稱的虛擬主機,可匹配正則,如果沒有域名可填寫IP,不填或隨便填寫一個
#nginx 根據 server_name 匹配 HTTP 請求頭的 host,去決定使用那個 server
#如果都沒有匹配則使用默認的。如果沒有默認就是第一個server
$http_host 和 $host的區別
$host 上邊描述過,主機頭(Host)字段,如果不可用或空就是server_name的值。
$http_host 可以理解請求地址,即瀏覽器中你輸入的地址(IP或域名)
那么這兩個使用哪個好一些呢?
如果Host請求頭部沒有出現在請求頭中,則$http_host值為空,但是$host值為主域名,
一般而言,會用$host代替$http_host,避免http請求中丟失Host頭部的情況下Host不被重寫。
如果對端口又要求可加上 :$proxy_port
Nginx訪問日志 IP統計
awk '{print $1}' access.log | sort | uniq -c|sort -n