nginx 反向代理,负载均衡,重定向,读写分离
2017-09-30 11:55
483 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
在server里面增加一条location:
location /test {
proxy_pass http://192.168.141.170:80/dashboard;
proxy_set_header X-Real-IP $remote_addr;
}
然后使用nginx -t 测试成功后,重启nginx,如果server端需要记录访问者ip则需要在对面做相应配置。
如果后端为apache则修改httpd.conf中的Logformat。
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
-> %(x-real-ip)i
添加内容:
upstream webserv {
server 192.168.140.78 weight=1 max_fails=2 fail_timeout=2;
server 192.168.141.170 weight=1 max_fails=2 fail_timeout=2;
}
注意:weight为权重调度可以轮巡,max_fails,fail_timeout设置自我健康检查。
在location处,改变内容:
proxy_pass http://webserv/;
如果,全部宕机则显示一个web错误页面:
首先定义一个server:
例如:
在代理服务器设置errorpage:
server{
listen 8080;
server_name localhost;
root /web/errpagers;
index index.html;
}
然后在创建目录/web/errpagers,并在目录下创建index.html文件,然后在upstream处添加为backup。
upstream webserv {
#server 192.168.140.78 weight=1 max_fails=2 fail_timeout=2;
server 192.168.141.170 weight=1 max_fails=2 fail_timeout=2;
server 192.168.140.78:8080 backup;
}
如果前面的server全部宕机则页面显示自定义错误index.html
nginx支持三种负载均衡模式:
round-robin(默认,需要权重),ip_hash,least_conn
定义缓存:
proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g;
然后再location中引用此缓存:
proxy_cache first;
proxy_cache_valid 200 10m;
如果测试是否命中在server中添加:
add_header X-cache $upstream_cache_status;
rewite模块:
if(condition){}
例子:
location /img/ {
rewrite http://192.168.140.78/img/;
}
location / {
proxy_pass http://192.168.140.170/;
if ($request_method = "PUT") {
proxy_pass http://192.168.140.78/;
}
}
原文链接:http://blog.csdn.net/zongyimin/article/details/64441522
目录(?)[+]
一.nginx反向代理
配置文件nginx.conf在server里面增加一条location:
location /test {
proxy_pass http://192.168.141.170:80/dashboard;
proxy_set_header X-Real-IP $remote_addr;
}
然后使用nginx -t 测试成功后,重启nginx,如果server端需要记录访问者ip则需要在对面做相应配置。
如果后端为apache则修改httpd.conf中的Logformat。
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
-> %(x-real-ip)i
二.nginx负载均衡
编辑nginx.conf文件添加内容:
upstream webserv {
server 192.168.140.78 weight=1 max_fails=2 fail_timeout=2;
server 192.168.141.170 weight=1 max_fails=2 fail_timeout=2;
}
注意:weight为权重调度可以轮巡,max_fails,fail_timeout设置自我健康检查。
在location处,改变内容:
proxy_pass http://webserv/;
如果,全部宕机则显示一个web错误页面:
首先定义一个server:
例如:
在代理服务器设置errorpage:
server{
listen 8080;
server_name localhost;
root /web/errpagers;
index index.html;
}
然后在创建目录/web/errpagers,并在目录下创建index.html文件,然后在upstream处添加为backup。
upstream webserv {
#server 192.168.140.78 weight=1 max_fails=2 fail_timeout=2;
server 192.168.141.170 weight=1 max_fails=2 fail_timeout=2;
server 192.168.140.78:8080 backup;
}
如果前面的server全部宕机则页面显示自定义错误index.html
nginx支持三种负载均衡模式:
round-robin(默认,需要权重),ip_hash,least_conn
三.nginx缓存
cache:共享内存(存储键和缓存对象元数据)磁盘空间:存储数据定义缓存:
proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g;
然后再location中引用此缓存:
proxy_cache first;
proxy_cache_valid 200 10m;
如果测试是否命中在server中添加:
add_header X-cache $upstream_cache_status;
四.nginx重定向URL
rewite模块:if(condition){}
例子:
location /img/ {
rewrite http://192.168.140.78/img/;
}
五.nginx读写分离
找到direction,然后后面添加 Dav on。location / {
proxy_pass http://192.168.140.170/;
if ($request_method = "PUT") {
proxy_pass http://192.168.140.78/;
}
}
原文链接:http://blog.csdn.net/zongyimin/article/details/64441522
相关文章推荐
- nginx 反向代理,负载均衡,重定向,读写分离
- nginx+Tomcat 配置负载均衡集群。反向代理。
- 前端反向代理nginx的负载均衡、读写分离及其缓存功能
- Nginx服务器搭建, 反向代理, 负载均衡, 状态监控
- 反向代理概念以及apache,nginx配置反向代理实现负载均衡方法
- 反向代理概念以及apache,nginx配置反向代理实现负载均衡方法
- nginx之rewrite重写,反向代理,负载均衡
- nginx反向代理服务器地址重写、负载均衡、证书认证、虚拟主机
- web负载均衡与反向代理之nginx
- nginx配置(负载均衡 反向代理 缓存 还有一些基础配置)
- nginx反向代理tomcat集群达到负载均衡,同时使用proxy_cache做web缓存 推荐
- 简单聊聊不可或缺的Nginx反向代理服务器--实现负载均衡【上篇】
- Nginx安装配置,简单构建LNMP环境配置反向代理,实现动静分离,以及实现负载均衡。
- 读写分离 URL重写 页面缓存 Nginx 反向代理
- Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
- Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
- lvs + keepalived + nginx + tomcat高可用负载反向代理服务器配置(三) Nginx
- Nginx反向代理后端多个Tomcat、Nginx+PHP服务器(Nginx的代理和负载功能) 推荐
- nginx 反向代理+重定向
- Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解