nginx的反向代理配置

    首先说明一下,nginx的反向代理和nginx的负载均衡很类似,nginx通常被做为反向代理实现负载均衡,有的人也把nginx的反向代理当作负载均衡,把nginx的负载均衡当作反向代理。nginx有不少功能和高性能,而它的强大的反向代理功能是它火的最主要缘由。
php

    这里nginx的安装就略去了,首先查看配置文件:
html

[root@nginx conf]# cat nginx.confnginx

user  nobody;apache

worker_processes  1;服务器

error_log  logs/error.log;app

pid        logs/nginx.pid;负载均衡


events {ide

    worker_connections  1024;性能

    use epoll;网站

}


http {

    include       mime.types;

    default_type  application/octet-stream;


    sendfile        on;

    keepalive_timeout  65;

    include extra/fanxiangdaili.conf;  ///引用fanxiangdaili.conf这个文件

}


而后查看一下我配置的反向代理文件里面都是哪些内容:

[root@nginx extra]# cat fanxiangdaili.conf 

server {

    listen 80;   ///监听本机的全部80端口

    server_name 10.10.16.29;  ///客户端要访问本服务器时输入的域名,因为这里没有配置dns服务器,就用ip地址代替相应的域名,效果是同样的。


    location / {

proxy_pass http://127.0.0.1:88;    ///将客户端发出的请求所有送到127.0.0.1这台机器的88端口,也就是本机的88端口,这里的88端口,我用的是Apache,通常nginx做为反向代理时,代理的是动态页面(好比php),而后将动态页面请求扔给Apache处理,扔的过程就是靠这句代码实现

    } 

}


[root@nginx conf]# yum install httpd -y   ///搭建Apache服务器

[root@nginx conf]# vi httpd.conf   ///修改Apache的监听端口,将默认的80端口改为88端口

  134 Listen 88

[root@nginx html]# cat index.html  ///建立Apache网站的主页(/var/www/html/index.html)

hehe apache

[root@nginx sbin]# ./nginx -s reload  ///重启nginx服务,使更改生效


ok,到这里,客户端能够访问了,看以下效果:

wKioL1PhpiGjatewAABxvGJRtmc956.jpg

用户访问10.10.16.29,返回了Apache的主页,说明nginx的反向代理功能作成功了。

这里还能够将要反向代理的机器配成其余机器而不是本机,在fanxiangdaili.conf配置文件改就好了,不过,通过验证,一台nginx服务器只能代理一个ip,想代理多个ip的话就用负载均衡吧(即upstream模块)。

[root@nginx extra]# cat fanxiangdaili.conf 

server {

    listen 80;

    server_name 10.10.16.29;


    location / {

# proxy_pass http://127.0.0.1:88;

proxy_pass http://10.10.16.91;

    } 

}


[root@nginx sbin]# ./nginx -s reload 

如今客户端再访问,见效果:

wKioL1Php3aSS-DbAACPZYJ06eA181.jpg

结果显示,访问同一个地址10.10.16.29,获得的是另一个页面,这代表nginx的反向代理功能作成功了!

相关文章
相关标签/搜索