1、准备环境
准备一个nginx代理 两个http 分别处理动态和静态。
location / {
root /var/www/html/upload;
index index.php index.htm;
}
2、本地配置
location ~ .*.(html|gif|jpg|png|bmp|swf|jpeg) {
root /var/www/html/move;
index index.php;
}
3、代理配置
location ~ .*.(html|gif|jpg|png|bmp|swf|jpeg) {
proxy_pass http://172.17.14.2:80;
}
4、配置项说明
location / 的作用
定义了请求代理的时候nginx去/var/www/html/upload 下寻找index.php 当他找到index.php的时候匹配了下面的正则 location ~ .php$。
location ~ .php$ 的作用
以php结尾的都以代理的方式转发给web1(172.17.14.2),http1 去处理,这里http1要去看自己的配置文件 在自己的配置文件中定义网站根目录 /var/www/html/upload 找.index.php 然后处理解析返回给nginx 。
location ~ .*.(html|gif|jpg|png|bmp|swf|jpeg)$ 的作用
以html等等的静态页面都交给web2(172.17.14.3)来处理 ,web2 去找自己的网站目录 然后返回给nginx 。
两个 web 放的肯定是一样的目录,只不过每个服务器的任务不一样。
代理本身要有网站的目录,因为最上面的 location / 先生效 如果没有目录 会直接提示找不到目录 不会再往下匹配。