FreeBSD下Nginx反向代理实现访问不同的服务器

 Nginx这个东东真是不错哦。。。。
网内有2台web服务器,
一台192.168.1.18 系统是freebsd+nginx+php+mysql
此机器上跑三个网站,利用nginx配置域名格式的虚拟主机
进入系统目录:
# cd /usr/local/etc/nginx
# mkdir vhosts
# cd vhosts
#vhosts/ee xxx1.cn.conf
分别建立:  xxx2.com.conf xxx3.com.conf 配置文件
然后将下面对应的配置代码写入配置文件。然后编辑Nginx的配置文件 nginx.conf
在文件中加入如下包含:

对应的域名解析配置如下:
##########################################################FreeBSD Nginx :www.xxx1.com
server {
        listen  80;
        server_name  xxx1.com www.xxx.com;
        access_log  /usr/local/etc/nginx/logs/www.xxx1.com.log;
        location / {
                root   /usr/local/www/nginx/xxx1.com;
                index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   /usr/share/nginx/html;
        }
       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
           
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/www/nginx/xxx1.com/$fastcgi_script_name;
            include        fastcgi_params;
        }
        location ~ /.ht {
            deny  all;
        }
}
###########################################################FreeBSD Nginx :www.xxx2.cn
server {
        listen  80;
        server_name xxx2.cn www.xxx2.cn;
        access_log  /usr/local/etc/nginx/logs/www.xxx2.cn.log;
        location / {
                root   /usr/local/www/nginx/xxx2.cn;
                index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   /usr/share/nginx/html;
        }
       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
           
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/www/nginx/xxx2.cn/$fastcgi_script_name;
            include        fastcgi_params;
        }
        location ~ /.ht {
            deny  all;
        }
}
############################################################FreeBSD Nginx :www.xxx3.com
server {
        listen  80;
        server_name xxx3.com www.xxx3.com;
        access_log  /usr/local/etc/nginx/logs/www.xxx3.com.log;
        location / {
                root   /usr/local/www/nginx/xxx3.com;
                index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   /usr/share/nginx/html;
        }
       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
           
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/www/nginx/www.xxx3.com/$fastcgi_script_name;
            include        fastcgi_params;
        }
        location ~ /.ht {
            deny  all;
        }
}
################################################################
一台192.168.1.19 系统是 windows2003的web服务器,IISweb应用,由于只有一个外网固定IP
但是需要访问不同的服务器的web服务这就需要域名格式的虚拟主机,在nginx下配置一个反向代理指向此IIS的web服务
在上面的nginx下的etc/nginx/下建立一个代理配置目录:
#/usr/local/etc/nginx/mkdir proxys
#cd proxys          //建立配置文件目录
#ee proxys.conf  //新建反向代理配置文件,如果需要指向其他的服务器只需要建立不同的配置文件,类似上面虚拟主机的方式
配置如下:
#
 server
{
          resolver        dns9.hichina.com;//此为万网的域名解析服务器,如不填写nginx会报一个502错误无法找到主机
         access_log      off;          
         listen  80;                   
         server_name www.xxx5.com;     
         location / {                  
                                       
            proxy_pass            http://192.168.1.19:8086;
            //一下的$变量都是Nginx内置无需更改,直接使用就ok
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_buffers   256 4k;    
            proxy_max_temp_file_size        0k;
            proxy_redirect off;        
         }
}
保存后同样要包含在nginx的配置文件中:

好了,FreeHSD中nginx的反向代理配置完成了,重启nginx
服务:

最后一步:
登陆windows服务器 打开IIS,在网站上右键选择属性,然后点击
 
打开配置框添加上面nginx反向代理解析的域名,如下图:
 

ok 完成,此时不管是访问nginx下的虚拟主机,还是访问IIS的主机都正常指向不同的地址。 

在宽带接入路由器上面做一个端口映射:
外网固定IP:x.x.x.x 外网端口80 内网192.168.1.18 端口 80

转载于:https://www.cnblogs.com/zjxfly/p/4310753.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值