Nginx高级应用--负载均衡与rewrite规则

    xiaoxiao2025-05-19  49

    Nginx高级应用–负载均衡与rewrite规则

    Nginx除了可以用作web服务器外,他还可以用来做高性能的反向代理服务器,它能提供稳定高效的负载均衡解决方案。nginx可以用轮询、IP哈希、URL哈希等方式调度后端服务器,同时也能提供健康检查功能。目前有众多公司均已经部署使用nginx实现基于七层的负载均衡功能。

    一、 Nginx负载均衡

    为了实现Nginx的反向代理以及负载均衡功能,应用中需要用到两个模块,HttpProxyModule和HttpUpstreamModule模块;其中HttpProxyModule模块的作用是将用户的数据请求转发到其他服务器上,HttpUpstreamModule模块是提供负载均衡技术。

    Nginx目前提供的负载均衡算法:

    ngx_http_upstream_round_robin,加权轮询,可均分请求,是默认算法,集成在框架中。

    ngx_http_upstream_ip_hash_module,IP哈希,可保持会话。

    ngx_http_upstream_least_conn_module,最少连接数,可均分连接。

    ngx_http_upstream_hash_module,一致性哈希,可减少缓存数据的失效 Nginx反向代理模块配置方法示例解析

    示例:

    location ~* \.(mp3|mp4)$ { #匹配URL以MP3或者MP4结尾的请求 proxy_pass http://localhost:8080 #转发到本机8080端口 } location / { #匹配任意URL proxy_pass http://localhost:8081 #转发到本机8081端口 proxy_set_header X-Forwarded-For $remote_addr #保留用户真实IP }

    location指令可以直接匹配字符串,也可以进行正则表达式匹配

    表示区分大小写,*表示不区分大小写匹配,=表示精确匹配

    proxy_pass指令可以根据location匹配的情况建立前后端的代理映射关系

    X-Forwarded-For用于实现定义数据包头,记录用户真实IP

    Nginx负载均衡模块配置方法示例解析

    http { upstream backend { ip_hash; server web1.test.com weight 1; server web2.test.com weight 2; server web3.test.com ; } server { listen 80; server_name web.test.com; location / { proxy_pass http://backend; }}}

    upstream定义后端服务器集合,backend是服务器组名

    proxy-pass和fastcgi_pass将请求转发给一组服务器

    ip_hash可以根据用户ip地址的hash值分配固定的后端服务器

    二、 Nginx负载均衡案例

    服务器名称 网路配置

    nginx.test.com eth0:122.126.152.183 eth1:192.168.1.2 web1.test.com eht0:192.168.1.3 web2.test.com eth0:192.168.1.4 web3.test.com eth0:192.168.1.5

    3台web机器配置

    在web1 web2 web3上安装httpd并配置网卡

    vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static IPADDR=192.168.1.3 NETMASK=255.255.255.0 GATEWAY=192.168.1.2 ONBOOT=yes TYPE=Ethernet service network restart yum install -y httpd iptables -F iptables -X service iptables save setenforce 0 sed -i s/enforcing/disabled/g /etc/sysconfig/selinux echo "web1 192.168.1.3" > /var/www/html/index.html service httpd restart chkconfig httpd on

    web2 web3机器上执行与web1相同步骤,注意修改部分参数

    Nginx代理服务器设置

    vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static IPADDR=122.126.152.183 NETMASK=255.255.255.0 GATEWAY=122.126.152.0 ONBOOT=yes TYPE=Ethernet vim /etc/sysconfig/network-scripts/ifcfg-eth1 DEVICE=eth1 BOOTPROTO=static IPADDR=192.168.1.2 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 ONBOOT=yes TYPE=Ethernet service network restart iptables -F iptables -X service iptables save setenforce 0 sed -i s/enforcing/disabled/g /etc/sysconfig/selinux wget http://nginx.org/download/nginx-1.6.3.tar.gz tar zxf nginx-1.6.3.tar.gz -C /usr/src/ yum install gcc pcre pcre-devel openssl openssl-devel gd gd-devel perl perl-ExtUtils-Embed cd /usr/src/nginx-1.6.3/ ./configure --prefix=/usr/local/nginx \ --with-ipv6 \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_dav_module \ --with-http_gzip_static_module \ --with-http_perl_module \ --with-mail_ssl_module make && make install

    修改配置文件

    vim /usr/local/nginx/conf/nginx.conf user nobody; worker_processes 1; error_log logs/error.log notice; pid logs/nginx.pid; events { worker_connections 5024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" '; sendfile on; tcp_nopush on; server_tokens off; keepalive_timeout 65; keepalive_requests 100; gzip on; #开启压缩 gzip_min_length 1000; #小于1000B内容不压缩 gzip_buffers 16 32k; #压缩缓存的个数和容量 gzip_types text/plain application/xml; #指定压缩文件类型 gzip_comp_level 2; #压缩级别为2,数字越大压缩效果越好 client_body_buffer_size 128k; #允许客户端请求缓存大小 client_max_body_size 100m; #允许请求的最大文件容量 large-client_header_buffers 4 8k; # proxy_buffering on; #开启代理缓冲功能 proxy_buffer_size 8k; #第一部分响应数据的缓存大小 proxy_buffers 8 128k; #响应数据的缓存个数和容量 proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=one:100m inactive=1d max_size=2G;

    #设置缓存目录,levels设置缓存个数,keys_zone定义缓存名字和容量,inactive定义缓存存活时间,max_size定义硬盘的缓存容量

    proxy_connect_timeout 60s; #与后端服务器建立TCP连接握手超时时间 upstream servers { #ip_hash; iphash确保相同客户端ip使用相同的后端服务器,不适用就默认轮询 server 192.168.1.3:80 max_fails=3 fail_timeout=30s weight=2; server 192.168.1.4:80 max_fails=3 fail_timeout=30s weight=2; server 192.168.1.5:80 max_fails=3 fail_timeout=30s weight=2; } server { listen 80; server_name web.test.com; access_log logs/host.access.log main; location / { proxy_pass http://servers; proxy_cache one; proxy_set_header X-Forwarded-For $remote_addr; } }}

    配置完成执行

    echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

    浏览器访问192.168.1.2 或者122.126.152.183 刷新将分别得到不同的web页面信息

    三、Nginx rewrite规则

    nginx的rewrite语法格式和apache非常相似,rewrite regex replacement [flag],其中flag可以被设置为last结束当前指令并重新搜索location匹配、break结束当前rewrite指令、redirect临时重定向302、permanent永久重定向301。

    rewrite地址重写及return应用的语法解析:

    ##根据浏览器标识,访问资源重定向到指定文件目录,下面用IE浏览器示例

    if ($http_user_agent ~ MSIE ) { rewrite ^(.*)$ /msie/$1 break; } ##将移动客户端的请求重定向到其他服务��� if ($http_user_agent ~* '(iphone|ipod)' ) { rewrite ^.+ http://mobile.site.com$uri; } ##用户使用POST方式请求数据时候,返回405 if ($request_method = POST ) { return 405; } rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; ##访问admin时候重定向到admin目录 location /php_admin { rewrite ^/php_admin/.*$ /admin permanent; }
    最新回复(0)