Nginx反向代理+负载均衡简单实现
一、基础环境:
负 载 机:A机器: 192.168.71.223
后端机器1:B机器:192.168.71.224
后端机器2:C机器:192.168.71.226
需求:
1)访问A机器的8080端口,反向代理到B机器的8080端口;
访问A机器的8088端口,反向代理到C机器的8088端口;
访问http://192.168.71.223:8090/ios,反向代理到B机器http://192.168.1.102:8090/ios/ 2)访问A机器的80端口,负载均衡到后端的两台机器B和C的80端口
二、安装配置:
1、三个机器共同执行:
1)编译安装nginx
yum install -y pcre* openssl* gcc gcc+
mkdir /opt/src && cd /opt/src
wget http://nginx.org/download/nginx-1.8.0.tar.gz && tar -zxvf nginx-1.8.0.tar.gz && cd nginx-1.8.0
useradd www -M -s /sbin/nologin #添加www用户,其中-M参数表示不添加用户家目录,-s参数表示指定shell类型
vi /opt/src/nginx-1.8.0/auto/cc/gcc
#CFLAGS="$CFLAGS -g" #将这句注释掉 取消Debug编译模式 大概在179行
./configure --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module #我们再配置下nginx编译参数
make && make install clean
2、机器A配置:
cd /opt/nginx/conf
vi nginx.conf
将配置改为:
worker_processes 2; events {
worker_connections 65535;
} http {
include mime.types;
default_type application/octet-stream;
charset utf-8; log_format main '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_cookie" $host $request_time';
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65; fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on; client_header_timeout 600s;
client_body_timeout 600s; client_max_body_size 100m;
client_body_buffer_size 256k; gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
gzip_vary on; include vhosts/*.conf;
}
nginx.conf
ulimit -n 65535
mkdir vhosts && cd vhosts
vi 8080.conf
将配置改为:
server {
listen 8080;
server_name localhost;
index index.html index.php index.htm;
root /var/www/html; access_log /usr/local/nginx/logs/8080-access.log main;
error_log /usr/local/nginx/logs/8080-error.log; location / {
proxy_pass http://192.168.71.224:8080;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 300; #跟后端服务器连接超时时间,发起握手等候响应时间
proxy_send_timeout 300; #后端服务器回传时间,就是在规定时间内后端服务器必须传完所有数据
proxy_read_timeout 600; #连接成功后等待后端服务器的响应时间,已经进入后端的排队之中等候处理
proxy_buffer_size 256k; #代理请求缓冲区,会保存用户的头信息以供nginx进行处理
proxy_buffers 4 256k; #同上,告诉nginx保存单个用几个buffer最大用多少空间
proxy_busy_buffers_size 256k; #如果系统很忙时候可以申请最大的proxy_buffers
proxy_temp_file_write_size 256k; #proxy缓存临时文件的大小
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
}
8080.conf
server {
listen 8088;
server_name localhost;
index index.html index.php index.htm;
root /var/www/html; access_log /usr/local/nginx/logs/8088-access.log main;
error_log /usr/local/nginx/logs/8088-error.log; location / {
proxy_pass http://192.168.71.226:8088;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 600;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
}
8088.conf
server {
listen 8090;
server_name localhost;
index index.html index.php index.htm;
root /var/www/html; access_log /usr/local/nginx/logs/8090-access.log main;
error_log /usr/local/nginx/logs/8090-error.log; location /ios/ { #这种情况,这里一定要匹配的是/ios/,不能是/ios
proxy_pass http://192.168.71.224:8090; #一定要保证192.168.1.102机器8090端口站点目录下有ios目录!否则访问会报错404!
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 600;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
}
8090.conf
upstream lb {
server 192.168.71.224:80 max_fails=3 fail_timeout=30s; #max_fails = 3 为允许失败的次数,默认值为1
server 192.168.71.226:80 max_fails=3 fail_timeout=30s; #fail_timeout = 30s 当max_fails次失败后,暂停将请求分发到该后端服务器的时间
} server {
listen 80;
server_name localhost;
index index.html index.php index.htm;
root /var/www/html; access_log /usr/local/nginx/logs/80-access.log main;
error_log /usr/local/nginx/logs/80-error.log; location / {
proxy_pass http://lb;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 600;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
}
LB.conf
启动Nginx:
启动nginx:
/opt/nginx/sbin/nginx -t 检查配置是否正确 #如果报缺少某个日志文件,创建目录及文件就可以
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
/opt/nginx/sbin/nginx 启动nginx
3、机器B配置:
cd /opt/nginx/conf && cp nginx.conf nginx.conf.bak
vi nginx.conf
将配置改为:
user www;
worker_processes 2; events {
worker_connections 65535;
} http {
include mime.types;
default_type application/octet-stream;
charset utf-8; log_format main '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_cookie" $host $request_time';
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65; fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on; client_header_timeout 600s;
client_body_timeout 600s; client_max_body_size 100m;
client_body_buffer_size 256k; gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
gzip_vary on; include vhosts/*.conf;
}
nginx.conf
ulimit -n 65535
mkdir vhosts && cd vhosts
vi 8080.conf
将配置改为:
server {
listen 8080;
server_name localhost;
index index.html index.php index.htm; access_log /usr/local/nginx/logs/8080-access.log main;
error_log /usr/local/nginx/logs/8080-error.log; location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}
8080.conf
server {
listen 8090;
server_name localhost;
index index.html index.php index.htm; access_log /usr/local/nginx/logs/8090-access.log main;
error_log /usr/local/nginx/logs/8090-error.log; location ~ / {
root /var/www/html/8090; #针对上面匹配ios的path代理,要保证站点目录/var/www/html/8080下有ios目录存在
index index.html index.php index.htm;
}
}
8090.conf
server {
listen 80;
server_name localhost;
index index.html index.php index.htm; access_log /usr/local/nginx/logs/80-access.log main;
error_log /usr/local/nginx/logs/80-error.log; location ~ / {
root /var/www/html;
index index.html index.php index.htm;
}
}
80.conf
启动nginx:
/opt/nginx/sbin/nginx -t 检查配置是否正确 #如果报缺少某个日志文件,创建目录及文件就可以
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
/opt/nginx/sbin/nginx 启动nginx
4、机器C配置:
cd /opt/nginx/conf && cp nginx.conf nginx.conf.bak
vi nginx.conf
将配置改为:
user www;
worker_processes 2; events {
worker_connections 65535;
} http {
include mime.types;
default_type application/octet-stream;
charset utf-8; log_format main '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_cookie" $host $request_time';
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65; fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on; client_header_timeout 600s;
client_body_timeout 600s; client_max_body_size 100m;
client_body_buffer_size 256k; gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
gzip_vary on; include vhosts/*.conf;
}
nginx.conf
ulimit -n 65535
mkdir vhosts && cd vhosts
vi 8080.conf
将配置改为:
server {
listen 8088;
server_name localhost;
index index.html index.php index.htm; access_log /usr/local/nginx/logs/8088-access.log main;
error_log /usr/local/nginx/logs/8088-error.log; location ~ / {
root /var/www/html/8088;
index index.html index.php index.htm;
}
}
8088.conf
server {
listen 80;
server_name localhost;
index index.html index.php index.htm; access_log /usr/local/nginx/logs/80-access.log main;
error_log /usr/local/nginx/logs/80-error.log; location ~ / {
root /var/www/html/;
index index.html index.php index.htm;
}
}
80.conf
启动nginx:
/opt/nginx/sbin/nginx -t 检查配置是否正确 #如果报缺少某个日志文件,创建目录及文件就可以
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
/opt/nginx/sbin/nginx 启动nginx
5、创建访问目录:
机器B的目录结构为:
[root@localhost var]# tree www/
www/
└── html
├── 8080
│ └── index.html
├── 8090
│ ├── index.html
│ └── ios
│ └── index.html
└── index.html
echo '192.168.71.224:/var/www/html/' >> index.html
mkdir -p /var/www/html/8080
echo '192.168.71.224:/var/www/html/8080' >> /var/www/html/8080/index.html
mkdir -p /var/www/html/8090
echo '192.168.71.224/var/www/html/8090' >> /var/www/html/8090/index.html
mkdir -p /var/www/html/ios
echo 'http://192.168.71.224:8090/ios/' >> /var/www/html/8090/ios/index.html
chown www:www -R /var/www/html
机器C的目录结构为:
[root@localhost var]# tree www/
www/
└── html
├── 8088
│ └── index.html
└── index.html
mkdir -p /var/www/html/8088
echo '192.168.71.226:/var/www/html' >> index.html
echo '192.168.71.226:/var/www/html/8088/' >> /var/www/html/8080/index.html
chown www:www -R /var/www/html
6、验证:
1、访问http://192.168.71.223/显示的是:192.168.71.224:/var/www/html/,再刷新变为:192.168.71.226:/var/www/html
2、访问http://192.168.71.223:8080/显示的是:192.168.71.224:/var/www/html/8080
3、访问http://192.168.71.223:8088/显示的是:192.168.71.226:/var/www/html/8088/
4、访问http://192.168.71.223:8090/ios/显示的是:http://192.168.71.224:8090/ios/
参考:http://www.cnblogs.com/kevingrace/p/5839698.html
Nginx反向代理+负载均衡简单实现的更多相关文章
- Nginx反向代理+负载均衡简单实现(http方式)
1)nginx的反向代理:proxy_pass2)nginx的负载均衡:upstream 下面是nginx的反向代理和负载均衡的实例: 负载机:A机器:103.110.186.8/192.168.1. ...
- Nginx反向代理+负载均衡简单实现(https方式)
背景:A服务器(192.168.1.8)作为nginx代理服务器B服务器(192.168.1.150)作为后端真实服务器 现在需要访问https://testwww.huanqiu.com请求时从A服 ...
- Nginx反向代理+负载均衡简单实现(手动申请https证书,申请免费https证书,http强转https)
背景:A服务器(192.168.1.8)作为nginx代理服务器B服务器(192.168.1.150)作为后端真实服务器 现在需要访问https://testwww.huanqiu.com请求时从A服 ...
- Centos7.4 Nginx反向代理+负载均衡配置
Ningx是一款高性能的HTTP和反向代理服务器,配置起来也比较简单. 测试环境: 172.16.65.190 Nginx-反向代理 172.16.65.191 Ningx-Web 172.16.65 ...
- Nginx 反向代理 负载均衡 虚拟主机配置
Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代理服务器的作用,区分正向代理和反向代理的区别,搭建使用Nginx反向搭理和负载均衡,了解Nginx常 ...
- Nginx 反向代理 负载均衡 虚拟主机
Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代理服务器的作用,区分正向代理和反向代理的区别,搭建使用Nginx反向搭理和负载均衡,了解Nginx常 ...
- 十.nginx反向代理负载均衡服务实践部署
期中集群架构-第十章-nginx反向代理负载均衡章节章节====================================================================== 0 ...
- 【转】Nginx 反向代理 负载均衡 虚拟主机配置
原文:http://www.cnblogs.com/itdragon/p/8059000.html Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代 ...
- 如何使用Weave以及Docker搭建Nginx反向代理/负载均衡服务器
Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动 ...
随机推荐
- Call to a member function cellExists() on a non-object /phpexcel/Classes/PHPExcel/Calculation.php on line 3241
PHP Fatal error: Call to a member function cellExists() on a non-object in /home/edata/eframework/we ...
- CSS学习(二)- 有关 hasLayout 和 BFC
1. hasLayout 概念说明 ‘Layout’ 可以被某些 CSS property(特性)不可逆的触发,而某些 HTML 元素本身就具有 layout . ‘Layout’ 在 IE 中可以通 ...
- 安装webpack最新版本出现错误
安装完了webpack出现了以下错误 解决办法 cnpm install webpack-cli -D 安装之后,就可以正常使用webpack了
- printf,sprintf,vsprintf
printf,sprintf比较常用,vsprintf不常用. 1. 三个函数的声明: int printf (const char * szFormat, ...); int sprintf (ch ...
- nginx访问css js 图片等静态资源,报404或无法定向访问到
配置完nginx,把php的项目放上去后,发现css,js和图片全部访问不到,一直重定向到根目录执行index.php,郁闷的在网上查了半天,原来不同后缀名的文件访问时都要在nginx.conf中声明 ...
- nginx 日志参数说明
$args #请求中的参数值 $query_string #同 $args $arg_NAME #GET请求中NAME的值 $is_args #如果请求中有参数,值为"?",否则为 ...
- mvc ajax给control传值问题
jquery中的ajax操作给后台传值 $.ajax({ type: 'POST', url: '<%=Url.Action("test","testIndex ...
- hdu 4419 线段树 扫描线 离散化 矩形面积
//离散化 + 扫描线 + 线段树 //这个线段树跟平常不太一样的地方在于记录了区间两个信息,len[i]表示颜色为i的被覆盖的长度为len[i], num[i]表示颜色i 『完全』覆盖了该区间几层. ...
- linux压缩、解压缩命令
tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...
- JPA概述以及它和Hibernate之间的关系
http://www.cnblogs.com/Kevin-ZhangCG/p/8996491.html 一.JPA概述以及它和Hibernate之间的关系 1.1.Hibernate 概述 JPA J ...