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 容器彼此连接在一起,支持跨主机部署及自动 ...
随机推荐
- Android学习之ItemTouchHelper实现RecylerView的拖拽以及滑动删除功能
今天在群里见大神们提到控件的拖动以及滑动删除的效果实现,就在网上找了资料ItemTouchHelper学习,并实现其功能.不胜窃喜之至,忍不住跟大家分享一下,如今就对学习过程做下简介.帮助大家实现这样 ...
- IE浏览器上传图片预览兼容(IE 7 8 9 10 11)
$("#file_upload").change(function () { var $file = $(this); ]; var windowURL = window.URL ...
- java 获取网页指定内容-2(实践+修改)
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; ...
- Hibernate每个具体类一张表映射(使用注释)
在每个类创建一张表的情况下, 表中不使用Null值的列. 这种方法的缺点是在子类表中创建了重复的列. 在这里,我们需要在父类中使用@Inheritance(strategy = Inheritance ...
- POJ 3480 & HDU 1907 John(尼姆博弈变形)
题目链接: PKU:http://poj.org/problem? id=3480 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1907 Descri ...
- Android UI开发第二十六篇——Fragment间的通信
为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把它们跟一个Activity关联,并 ...
- 前台传递给后台的JSON字符串中的引号 “” 在JAVA后台被转义为 "
前台传递给后台的JSON字符串中的引号 "" 在JAVA后台被转义为 " 1.问题: 前台数据,JSON字符串带有引号 "" ,数据被传递到后台 ...
- 20160924-1——mysql存储引擎
一.主要存储引擎 5.5以后的版本,默认存储引擎从myisam改成了innodb:线上推荐都用innodb 二.innodb存储引擎 (一)结构 INNODB存储引擎大致分三部分,图中已经(1)(2) ...
- MySql最左前缀原则
简单整理记录下,之前一直都没有关注过这个问题 最左前缀原则:顾名思义是最左优先,以最左边的为起点任何连续的索引都能匹配上, 注:如果第一个字段是范围查询需要单独建一个索引 注:在创建多列索引时,要根据 ...
- JdbcUtils 小工具
// 第一版 // src 目录下 dbconfig.properties 配置文件, 用来配置四大参数 // 注意 properties 配置文件中没有分号结尾, 也没有引号 driverClass ...