nginx实现请求转发

反向代理适用于很多场合,负载均衡是最普遍的用法。

nginx 作为目前最流行的web服务器之一,可以很方便地实现反向代理。

nginx 反向代理官方文档: NGINX REVERSE PROXY

当在一台主机上部署了多个不同的web服务器,并且需要能在80端口同时访问这些web服务器时,可以使用 nginx 的反向代理功能: 用 nginx 在80端口监听所有请求,并依据转发规则(比较常见的是以 URI 来转发)转发到对应的web服务器上。

例如有 webmail , webcom 以及 webdefault 三个服务器分别运行在 portmail , portcom , portdefault 端口,要实现从80端口同时访问这三个web服务器,则可以在80端口运行 nginx, 然后将 /mail 下的请求转发到 webmail 服务器, 将 /com下的请求转发到 webcom 服务器, 将其他所有请求转发到 webdefault 服务器。

假设服务器域名为example.com,则对应的 nginx http配置如下:

  1.  
    http {
  2.  
    server {
  3.  
    server_name example.com;
  4.  
     
  5.  
    location /mail/ {
  6.  
    proxy_pass http://example.com:protmail/;
  7.  
    }
  8.  
     
  9.  
    location /com/ {
  10.  
    proxy_pass http://example.com:portcom/main/;
  11.  
    }
  12.  
     
  13.  
    location / {
  14.  
    proxy_pass http://example.com:portdefault;
  15.  
    }
  16.  
    }
  17.  
    }

以上的配置会按以下规则转发请求( GET 和 POST 请求都会转发):

  • 将 http://example.com/mail/ 下的请求转发到 http://example.com:portmail/
  • 将 http://example.com/com/ 下的请求转发到 http://example.com:portcom/main/
  • 将其它所有请求转发到 http://example.com:portdefault/

需要注意的是,在以上的配置中,webdefault 的代理服务器设置是没有指定URI的,而 webmail 和 webcom 的代理服务器设置是指定了URI的(分别为 / 和 /main/)。 如果代理服务器地址中是带有URI的,此URI会替换掉 location 所匹配的URI部分。 而如果代理服务器地址中是不带有URI的,则会用完整的请求URL来转发到代理服务器。

官方文档描述:

If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter. If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).

以上配置的转发示例:

  • http://example.com/mail/index.html -> http://example.com:portmail/index.html
  • http://example.com/com/index.html -> http://example.com:portcom/main/index.html
  • http://example.com/mail/static/a.jpg -> http://example.com:portmail/static/a.jpg
  • http://example.com/com/static/b.css -> http://example.com:portcom/main/static/b.css
  • http://example.com/other/index.htm -> http://example.com:portdefault/other/index.htm

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/102471

(34)
单爆手单爆手
上一篇 2018-07-08 19:32
下一篇 2018-07-08 21:14

相关推荐

  • centos7 网络设定

    从CentOS7开始,网络设定可以不再通过修改各类配置文件,而使用近乎万能的nmcli命令。本文简单介绍该命令的重要使用方法,为后期查询复习做记录。

    Linux笔记 2018-06-12
  • 初识Nginx——晓以大意 明以细理

    什么是nginx?nginx is a free,open-source,high-performance http server and reverse proxy,as well as an IMAP/POP3 proxy.通俗的说Nginx提供web服务,反向代理,以及IMAP/POP3代理,那么什么是web服务?反向代理?IMAP/POP3代理? w…

    2017-09-10
  • 第一周总结

     ##### 存储网络: “` DAS—–直接连接存储(Direct attached storage) NAS—–网络连接存储(Network attached storage) SAN—–存储区域网络(storage area network) “` &#82…

    Linux笔记 2018-04-01
  • shell脚本

    shell脚本的练习题

    2018-04-18
  • Linux 的用户、文件管理和权限

    Linux文件系统上的特殊权限 可执行文件上SUID权限   目录上的SGID权限 sticky 特殊权限数字法: 权限位映射: 设定文件的特定属性: 访问控制列表: 访问控制列表: 访问控制列表:        

    Linux笔记 2018-04-08
  • 网络管理-第二篇

    IP地址 它们可唯一标识 IP 网络中的每台设备 每台主机(计算机、网络设备、外围设备)必须具有唯一的地址 IP地址由两部分组成: 网络ID: 标识网络 每个网段分配一个网络ID 主机 ID: 标识单个主机 由组织分配给各设备 MAC地址没有管理性 IP地址可管理性标识性高 子网掩码标识网络ID和主机ID 地址分类 二进制 A类地址:前八位是网络ID 1-1…

    2018-05-03