nginx 反向代理配置 upstream

   最近项目要写后台,用nodejs写服务接口,然后研究了下nginx反向代理,各种坑下来,也总算把代理配了下来。

 我本地用nodejs起了两个服务,一个端口是8888,一个端口是8889,在启动nginx服务后,可以直接访问localhost来访问不同的端口。

 在操作页面跳转和加载数据的server上,我给router同意前缀,比如:/oper/add,/oper/save等,而作为读流的服务器8889则直接明明后者使用其他前缀,我直接使用了/upload.

  在nginx的nginx.config中就可以添加三句upstream,

  upstream nodesvr1{

  ip_hash;

  server 127.0.0.1:8888;

  }

  

  upstream nodesvr2{

  ip_hash;

  server 127.0.0.1:8889;

  }

 

然后在80端口的server中配置

location ~^/(oper/){
proxy_pass http://nodesvr1;
root path;
index index.html index.htm;
}
location /iconUpload{
proxy_pass http://nodesvr2;
}

这样即可访问,但是,对于要加载的资源,如css,图片,js文件等,需要做一些其他配置,

比如,我在项目中存放资源的地址是js/ css/ img/等文件夹,那么在location中接着如下配置:

location ~^/(static|img|css|js|fonts) {
index index.html index.htm;
root path;   //绝对路径
}

 

然后重启nginx服务,即可访问

posted @ 2015-04-11 13:36  Greensoon  阅读(1335)  评论(0编辑  收藏  举报