nginx_3_反向代理负载均衡缓存URL重写

LEMP
  nginx(FastCgi)+php-fpm

location ~* \.php$ {
        fastcgi_pass  127.0.0.1:9000;
       }

nginx配置文件
main,
  worker_process
  error_log
  user
  group

events {
  }

http {
  server{
       listen 80;
       server_name test.hale.com;
       location / {
         root html;
     index index.html;
     allow 192.168.21.85;
     deny all;
         auth_basic    "The authentication"
     auth_basic_user_file /usr/local/nginx/.user
        }
   }
 }

#定义默认主机头,即没有明确定义虚拟主机的都使用此server
  server {
       listen 80 default;
       return 500;
      }

nginx反向代理:

proxy_pass

server {
       listen 80;
       server_name test.hale.com;
       location / {
         proxy_pass http://192.168.21.123:80;
     proxy_set_header X-Real-IP $remote_addr;
    }
      }

nginx负载均衡:

upstream

#定义上游服务器组
   upstream 123test {
        #ip_hash;
        server 192.168.21.123:80 weight=1 max_fails=2 fail_timeout=3;
        server 192.168.21.124:80 weight=1 max_fails=2 fail_timeout=3;
        server 127.0.0.1:8080 backup;  #定义sorry页面,不能与ip_hash算法一起使用
    }

server {
        listen 80;
        server_name 123test.hale.com;
        location / {
          proxy_pass http://123test;
          proxy_set_header X-Real-IP $remote_addr;
        }
     }

#定义sorry虚拟主机,在所有上游服务器不可用时,代理到该虚拟主机
   server {
        listen  8080;
        server_name localhost;
        location /{
          root /usr/local/nginx/html;
          index sorry.html;
         }
       }

nginx缓存:
  共享内存: 存储键和缓存对象元数据
  磁盘空间: 存储数据

proxy_cache_path: 不能定义在server{}中
       
   proxy_cache_path /nginx/cache/first levels=1:2:1 keys_zone=first:32m max_size=1g;

upstream 123test {
        #ip_hash;
        server 192.168.21.123:80 weight=1 max_fails=2 fail_timeout=3;
        server 192.168.21.124:80 weight=1 max_fails=2 fail_timeout=3;
        #server 127.0.0.1:8080 backup;
    }

server {
        listen 80;
        server_name 123test.hale.com;
        location / {
          proxy_pass http://123test;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_cache first;
          proxy_cache_valid 200 10m;
          add_header X-Via $server_addr;
          add_header X-Cache $upstream_cache_status;
        }
     }

另外三种常用的缓存:
     open_log_cache: 日志缓存
     open_file_cache:
     fastcgi_cache:

将特定的请求发送到特定的服务器

upstream phpser {
       server 192.168.1.10;
       server 192.168.1.11;
     }

upstream imgser {
       server 192.168.1.20;
       server 192.168.1.21;
     }
     
upstream staticser {
       server 192.168.1.30;
       server 192.168.1.31;
     }

server {
    listen 80;
    server_name test.test.com;
    
    location / {
         proxy_pass http://staticser;
     }
     
     location ~* \.php$ {
         fastcgi_pass http://phpser;
     }
     
     location ~* \.(jpg|jpeg|gif|png)$ {
         proxy_pass http://imgser;
     }
     }

rewrite: URL重写
   支持正则表达式
 
    if(condition) {
    }

测试:
    双目测试:
    ~, !~
    =, !=
    ~*, !~*

# if 语句尽量只在location上下文中使用
    if ($request_method = "POST") {
      }

if ($request_uri ~* "/forum") {
      }

location /images/ {
              rewrite http://192.168.1.20;
     }

location / {
              root html;
          index index.html;
              rewrite "^/bbs(.*)" http://192.168.1.41/forum$1 last;
         }
    
        location / {
              root html;
          index index.html;
              rewrite "^/bbs.*" "bbs.html" last;
         }

last : 本次重写完成后,重启下一轮检查;对重写的URL再次进行重写检查
    break : 本次重写完成后,直接执行后续操作

nginx读写分离:
  上游服务器使用httpd
  在httpd.conf中的<Directory>段中修改
     <Directory "/var/www/html">
     Dav on
     Options None
     AllowOverride None
     Order allow,deny
     Allow from all
     </Directory>

并给予此目录apache的读写执行权限
   # setfacl  -m u:apache:wrx /var/www/html/

在nginx上定义读写分离

upstream 123test {
        #ip_hash;
        server 192.168.21.123:80 weight=1 max_fails=2 fail_timeout=3;
        server 192.168.21.124:80 weight=1 max_fails=2 fail_timeout=3;
        #server 127.0.0.1:8080 backup;
    }

server {
        listen 80;
        server_name 123test.hale.com;
        location / {
          proxy_pass http://123test;
            if ($request_method = "PUT") {
              proxy_pass http://192.168.21.124:80;
             }
          proxy_set_header X-Real-IP $remote_addr;
         # proxy_cache first;
         # proxy_cache_valid 200 10m;
         # add_header X-Via $server_addr;
         # add_header X-Cache $upstream_cache_status;
        }
     }

时间: 2024-08-13 15:26:38

nginx_3_反向代理负载均衡缓存URL重写的相关文章

编译安装nginx并实现反向代理负载均衡和缓存功能

一.编译安装nginx 1.下载 [[email protected] ~]# wget http://nginx.org/download/nginx-1.10.0.tar.gz 2.解压 [[email protected] ~]# tar xf nginx-1.10.0.tar.gz [[email protected] ~]# cd nginx-1.10.0 3.安装依赖组件 [[email protected] nginx-1.10.0]# yum install pcre-devel

Nginx实现反向代理负载均衡与静态缓存

介绍: Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.在连接高并发的情况下,Nginx是Apache服务器不错的替代品,能够支持高达50000个并发连接数的响应. 实验环境: Hostname IP 系统 规划 n2.preferred 192.168.1.2 Centos 6.5 Web server n3.preferred 192.168.1.3 Centos 6.5 Web server n6.preferred 192.168.1.6

项目实战2.2—nginx 反向代理负载均衡、动静分离和缓存的实现

实验一:实现反向代理负载均衡且动静分离 1.环境准备: 机器名称 IP配置 服务角色 备注 nginx VIP:172.17.11.11 反向代理服务器 开启代理功能 设置监控,调度 rs01 RIP:172.17.22.22 后端服务器 stasic-srv 组 rs02 RIP:172.17.1.7 后端服务器 stasic-srv 组 rs01 RIP:172.17.77.77 后端服务器 defautl-srv 组 rs02 RIP:172.17.252.111 后端服务器 defaut

项目实战02:nginx 反向代理负载均衡、动静分离和缓存的实现

目录 实验一:实现反向代理负载均衡且动静分离 1.环境准备: 2.下载编译安装tengine 3.设置代理服务器的配置文件 4.启动tengine服务 5.开启后端的web服务 6.测试 实验二:nginx实现缓存功能 1.环境准备:同上实验,实验结构图如下: 2.设置代理服务器的配置文件 3.测试:访问 http://172.17.11.11/ 总项目流程图,详见http://www.cnblogs.com/along21/p/7435612.html 回到顶部 实验一:实现反向代理负载均衡且

反向代理负载均衡

使用代理服务器可 以将请求转发给内部的Web服务器,使用这种加速模式显然可以提升静态网页的访问速度.因此也可以考虑使用这种技术,让代理服务器将请求 均匀转发给多台内部Web服务器之一上,从而达到负载均衡的目的.这种代理方式与普通的代理方式有所不同,标准代理方式是客户使用代理访问多个外部Web 服务器,而这种代理方式是多个客户使用它访问内部Web服务器,因此也被称为反向代理模式. 中文名 反向代理负载均衡 外文名 reverse-proxy load balancing 目录 1 概念 2 ngi

反向代理负载均衡之nginx

一.集群 1.1 什么是集群 集群是一组相互独立的.通过高速网络互联的计算机,它们构成了一个组,并以单一系统的模式加以管理.一个客户与集群相互作用时,集群像是一个独立的服务器.集群配置是用于提高可用性和可缩放性. 和传统的高性能计算机技术相比,集群技术可以利用各档次的服务器作为节点,系统造价低,可以实现很高的运算速度,完成大运算量的计算,具有较高的响应能力,能够满足当今日益增长的信息服务的需求. 而集群技术是一种通用的技术,其目的是为了解决单机运算能力的不足.IO能力的不足.提高服务的可靠性.获

Tomcat反向代理+负载均衡的四种方法配置

环境拓扑 环境说明: IP地址 功能描述 nginx or httpd 172.16.4.100 代理服务器,将用户请求分发到后端的Tomcat服务器实现反向代理和负载均衡 Tomcat-01 172.16.4.101 后端实际提供服务的服务器 Tomcat-02 172.16.4.102 后端实际提供服务的服务器 系统环境都如下所示: [[email protected] ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [[emai

反向代理负载均衡-----nginx

一:集群 1.1:集群的概念 集群是一组相互独立的.通过高速网络互联的计算机,他们构成了一个组,并以单一系统的模式加以管理.一个客户与集群相互作用时,集群像是一个独立的服务器.集群配置是用于提高高可用和可伸缩性.和传统的高性能计算机技术相比,集群技术可以利用各档次的服务器作为节点,系统造价低,可以实现很高的运算速度,完成大运算量的计算,具有较高的响应能力,能够满足当今日益增长的信息服务的需求.而集群技术是一种通用的技术,其目的是为了解决单机运算能力不足.IO能力的不足,提高服务的可靠性.获得规模

如何使用Weave以及Docker搭建Nginx反向代理/负载均衡服务器

Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动发现.它可以让我们更加专注于应用的开发,而不是基础架构.Weave 提供了一个如此棒的环境,仿佛它的所有容器都属于同个网络,不需要端口/映射/连接等的配置.容器中的应用提供的服务在 weave 网络中可以轻易地被外部世界访问,不论你的容器运行在哪里.在这个教程里我们将会使用 weave 快速并且简单