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 容器彼此连接在一起,支持跨主机部署及自动 ...
随机推荐
- iOS 学习笔记三【segmentedControl分段控制器详细使用方法】
在iOS开发过程中,分段控制器的使用频率还是蛮高的,下面是我写的一个简单的demo,大家可以把代码直接复制过去,就可以使用,ios9最新支持. // // ViewController.m // 03 ...
- UIView 实例方法 Instance Methods(转)
好了,我接着上篇,开始我们的对UIView 实例方法的探索 UIView 实例方法 Instance Methods 初始化一个视图 - (id)initWithFrame:(CGRect)aRect ...
- linux 一行一行的读取文件
#!/bin/bash #reading data from a file count= cat test1 | while read line do echo "Line $count: ...
- ueditor 控制上传图片的显示尺寸
使用UEditor的编辑框插入图片的时候,如果图片尺寸比较大,则图片会超出编辑器边框出现滚动条,特别不方便. 解决办法:在ueditor 的 themes 文件夹下有个iframe.css 文件,在该 ...
- Audiosink design
Audiosink的设计,需要满足下列一些需求: 良好的chain_based 支持.绝大多数简单playback pipelines都是将音频数据从decoder直接push给audiosink; ...
- Eclipse 运行程序
运行 Java 程序 我们可以在 Package Explorer 视图 可以在 Package Explorer 视图中快速运行 Java 程序. Package Explorer 视图: 鼠标右击 ...
- js金钱分割,正则
``` var test1 = '1234567890'var format = test1.replace(/\B(?=(\d{3})+(?!\d))/g, ',')"1,234,567, ...
- linq to sql 动态构建查询表达式树
通过Expression类进行动态构造lamda表达式. 实现了以下几种类型,好了代码说话: public Expression<Func<T, bool>> GetAndLa ...
- 单线程爬虫VS多线程爬虫的效率对比
单线程爬虫: import re import requests import time url_EB = 'http://www.amazon.com/gp/search/other/ref=sr_ ...
- Laravel5.1 模型--查询作用域
所谓的查询作用域就是允许你自定义一个查询语句 把它封装成一个方法. 1 定义一个查询作用域 定义查询作用域就是在模型中声明一个scope开头的方法: public function scopeHotA ...