基于浏览器实现分离案例 if ($http_user_agent ~ Firefox) { rewrite ^(.)$ /firefox/$1 break; } if ($http_user_agent ~ MSIE) { rewrite ^(.)$ /msie/$1 break; } if ($http_user_agent ~ Chrome) { rewrite ^(.*)$ /chrome/$1 break; }

防盗链案例: location ~* .(jpg|gif|jpeg|png)$ { valid_referer none clocked www.idfsoft.com; if ($invalid_referer) { rewrite ^/ http://www.idfsoft.com/403.html; } }

nginx反向代理负载均衡简述 nginx通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。 nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。 但是要注意,这种情况需要保证后端跟前端的程序保持一致,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。 nginx通过upstream模块来实现简单的负载均衡,upstream需要定义在http段内 在upstream段内,定义一个服务器列表,默认的方式是轮询,若要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash,如: upstream idfsoft.com { ip_hash; server 127.0.0.1:9080 weight=5; server 127.0.0.1:8080 weight=5; server 127.0.0.1:1111; }

实现简单的负载均衡

试验环境三台主机: 192.168.56.11 安装nginx,作反向代理 192.168.56.12 安装nginx,作后端服务器 192.168.56.13 安装htppd,作后端服务器 三台机器安装好服务后启动,并关闭防火墙,selinux。

192.168.56.11服务器上操作: 修改nginx配置文件 在http段内server段上边添加upstream模块: [root@heyuanjie ~]# vi /usr/local/nginx/conf/nginx.conf upstream web { server 192.168.56.12; server 192.168.56.13; } 定义好upstream后,需要在server段内添加如下内容: location / { proxy_pass http://web; }

以上两段内容中的web名可以自定义,但需要做到见名知意,并且两者要对应一样。 修改完配置文件检查语法错误,并重启服务 [root@heyuanjie ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@heyuanjie ~]# nginx -s reload

在192.168.56.12服务器上操作,创建网页文件输入内容,用于后续验证 [root@hyj ~]# cd /usr/local/nginx/html/ [root@hyj html]# echo 'you are my rose'> index.html 在192.168.56.13服务器上操作 ,创建网页文件输入内容,用于后续验证 [root@hejie ~]# echo 'ran,you are my love!' > /var/www/html/index.html

通过ip192.168.56.11访问

再次输入ip192.168.56.11访问,实现轮询