基于Windows 配置 nginx 集群 & 反向代理

1、下载 nginx

    下载页面 :

     http://nginx.org/en/download.html

   具体文件:

     http://nginx.org/download/nginx-1.7.0.zip

   

2、运行 nginx

    解压第一步下载的 nginx-1.7.0.zip 压缩包 解压到 c:/nginx路径

2.1、修改监听端口

  由于 80 端口已经配置IIS ,现修改nginx 监听端口 

    server {

     listen      80;

  修改为

      listen    5000;

2.2 、修改 host

   修改系统 host (路径:C:WindowsSystem32driversetcHOSTS):

   添加配置:

   127.0.0.1 wangkun.com

  2.3 、启动 cmd 命令窗口

   

cd C:
ginx

//  启动  nginx
start  nginx

/*
   常用命令
    nginx -s stop          // 停止nginx
    nginx -s reload       // 重新加载配置文件
    nginx -s quit          // 退出nginx

*/

  在浏览器中 浏览 http://wangkun.com:5000 即可查看 nginx 欢迎界面

2.4  配置nginx 集群

     2.4.1  配置 IIS 站点:

        web1:  127.0.0.1:5069

        web2:  127.0.0.1:5070

    2.4.2  调整nginx配置

        

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;


       upstream  wangkun.com 
	   { 
           server   127.0.0.1:5069;
           server   127.0.0.1:5070;
       }
	   
    server 
	{
        listen       5000;
        server_name  localhost;

   
        location / {
      
			proxy_pass http://wangkun.com;
			proxy_redirect default;
			
        }

       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }


}

 2.4.3 验证:

       通过浏览器浏览: http://wangkun:5000 

   

现在停止 IIS web01  ,则浏览的页面就一直显示  web02

 备注:

    在生产环境中 ,可以将nginx 部署在linux上 ,有独立的linux nginx 主机转化请求 映射到  windows IIS上

  

 3、反向代理

  添加配置:

    

server {

    listen       9000;
    server_name  localhost;
  
    location ~^/blog/{
         
        proxy_pass http://127.0.0.1:8003;
        
        index  index.html index.htm;
    }
}    

此时请求地址:

http://127.0.0.1:9000/blog/

实际请求地址将为:

http://127.0.0.1:8003/blog/


调整配置:
    
        location ~^/blog/{
                rewrite /blog/(.+)$ /$1 break; 
            proxy_pass http://127.0.0.1:8003;
            
            index  index.html index.htm;
        }

此时请求地址:

http://127.0.0.1:9000/blog/

实际请求地址将为:

http://127.0.0.1:8003/


4、 设置报头
 location /abc/ {
     proxy_set_header Host $host;
     proxy_set_header ProxyAlias "abcvalue";
     proxy_pass http://127.0.0.1:8092/;
 
后端代码就可以通过
 request.heard["ProxyAlias "];


原文地址:https://www.cnblogs.com/rhythmK/p/3731567.html