实验环境
三台服务器:
一台Nginx作为前端反向代理服务器,IP地址192.168.2.73
一台apache作为后端的web服务器(apache用的系统自带的),IP地址192.168.5.54
一台apache作为后端的web服务器(apache用的系统自带的),IP地址192.168.5.57
nginx服务器配置:
1、安装步骤很简单。
make
make install
2、修改nginx.conf文件,设置proxy相关参数,在httpd字段中增加如下内容:
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;
client_max_body_size 300m;
client_body_buffer_size 128k;
client_body_temp_path /dev/shm/client_body_temp;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /dev/shm/proxy_temp;
#keepalive_timeout 0;
keepalive_timeout 65;
upstream server_pool {
server 192.168.5.54:8080 weight=8 max_fails=2 fail_timeout=30s;
server 192.168.5.57:8080 weight=8 max_fails=2 fail_timeout=30s;
}
#gzip on;
继续修改nginx.conf文件,在server中lication /配置中做如下修改:
location / {
proxy_pass http://server_pool/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
root html;
index index.html index.htm;
}
然后保存,检查配置文件是否有问题
[root@hadoop3 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3、启动nginx
[root@hadoop3 ~]# /usr/local/nginx/sbin/nginx
配置两台apache服务器
登录192.168.5.54上操作:
[root@hadoop5 ~]# echo 'this is 192.168.5.54!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
[root@hadoop5 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@hadoop5 ~]# /etc/init.d/httpd start
登录192.168.5.57上操作:
[root@service ~]# echo 'Hello,This is 192.168.5.57!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
[root@service ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@service ~]# /etc/init.d/httpd start
测试:
[root@hadoop3 ~]# for i in $(seq 20); do curl http://192.168.2.73/; done
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
这样基本就完成了nginx反向代理服务器的配置,如果我没记错应该比lvs的负载强一点,lvs只支持80端口转发到80端口,而nginx可以80端口转发到任意不一样的端口。
您可能感兴趣的文章:
Nginx负载均衡与反向代理的例子(图文)
Nginx Proxy 代理配置图片缓存的实例参考
nginx正向代理配置简单一例
nginx反向代理配置简单示例
学习Nginx反向代理实现简单负载均衡(图文)
nginx缓存html静态文件 解析php及反向代理IIS的配置
nginx中配置proxy正向代理
Nginx实现简单的反向代理
nginx创建反向代理和虚拟主机的例子
nginx的反向代理配置与优化
nginx反向代理与varnish缓存配置
Nginx 反向代理的小例子
nginx反向代理与缓存详解
nginx反向代理配置一例
Nginx反向代理Nginx
nginx反向代理配置和优化
Nginx Proxy代理和图片缓存配置
nginx配置反向代理的简单示例
网站更新AAF后,www站点Server(IIS)负载不停的攀升,从当初的平均30%攀升到70%;CPU一直保持这么高的负载,会有问题的;便开始尝试用Nginx来挂在IIS服务器前面,来降低IIS服务器负载;
Nginx来做反响代理,架构这样的,Nginx 放在F5下面一个VIP Pool里面,Nginx从IISPoo里面反响代理,配置文件:
user daemon daemon;
worker_processes 8;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /var/run/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
upstream myproject {
server x.x.x.x weight=3;
}
server {
listen 80;
server_name www.xxxx.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://myproject;
}
}
server {
listen 80;
server_name xxxx.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://myproject;
}
}
server
{
listen 80;
server_name x.x.x.x;
location /Nginxstatus {
stub_status on;
access_log off;
auth_basic “NginxStatus”;
#auth_basic_user_file htpasswd;
}
}
}
nginx负载均衡302跳转设置,基于perl的一致性哈希。
目的:降低 后端大文件服务器由于新增和故障调整的影响
通过nginx的perl_module来实现302,根据请求的哈希跳转crc32校验
工作环境: perl版本在>=5.10.0 nginx版本 >==0.8.33 二点必要
rewrite.pm 配置
package rewrite;
use nginx;
use Hash::ConsistentHash;
use String::CRC32;
sub handler {
my $r = shift;
my $uri = $r->uri;
my $domain = $r->header_in(host);
my @crr = qw( 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 10.0.0.5 );
my $chash = Hash::ConsistentHash->new(
buckets => [@crr],
hash_func => \&crc32
);
my $url = $chash->get_bucket($uri);
$url = "http://".$url."/".$domain.$uri;
$r->header_out(Location => $url);
$r->status(302);
$r->send_http_header;
return OK;
}
1;
__END__
nginx server 配置
listen 80 ;
server_name www.hello.com;
root /tmp/hello;
location ~ \.exe {
perl rewrite::handler;
}
}
nginx conf配置(加2行)
perl_modules perl/lib;
perl_require rewrite.pm;
由于Hash::ConsistentHash对perl要求版本>=5.10.0,如果你系统perl较低需升级perl版本并重新编译nginx;
Hash::ConsistentHash 下载地址
http://search.cpan.org/CPAN/authors/id/K/KA/KARAVELOV/Hash-ConsistentHash-0.05.tar.gz
String::CRC32下载地址
http://search.cpan.org/CPAN/authors/id/S/SO/SOENKE/String-CRC32-1.4.tar.gz