nginx 反向代理,负载均衡,重定向,读写分离

目录(?)[+]

一.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.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


猜你喜欢

转载自blog.csdn.net/lin06051180/article/details/78141397
今日推荐