反向代理(reverse proxy)方式是指用代理服务器来接受Internet上的连接请求,然后 将请求转发给内部网络中的上游服务器,并将从上游服务器上得到的结果返回给Internet请 求连接的客户端,此时代理服务器对外的表现就是一个web服务器。

    一般情况下,nginx在前端抗负载和处理静态页面请求,上游服务器可以挂接Apche/ Tomcat等处理复杂业务的动态Web服务器。

    服务器A:IP 172.16.0.35 作为一台代理服务器,大致配置如下。

    1. http {
    2. include mime.types;
    3. default_type application/octet-stream;
    4. sendfile on;
    5. keepalive_timeout 65;
    6. #配置均衡服务器
    7. upstream backup.com {
    8. server 172.16.0.138:80;#服务器B
    9. server 172.16.0.70:80; #服务器C
    10. }
    11. server {
    12. listen 80;
    13. server_name localhost;
    14. location / {
    15. #配置反向代理功能
    16. proxy_pass http://backup.com;
    17. root html;
    18. index index.html index.htm;
    19. }
    20. error_page 500 502 503 504 /50x.html;
    21. location = /50x.html {
    22. root html;
    23. }
    24. }

    服务器B:IP 172.16.0.138

    服务器C:IP 172.16.0.70

    均作为普通的web服务器。

    在浏览器输入 服务器A的ip地址,也就是反向代理的地址。
    172.16.0.35

    会发现,通过访问服务器A,最终处理客户端请求的确实服务器B和服务器C。

    这种就是利用Nginx实现了反向代理。