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

相关推荐

  • 第二周

    第二周: Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 1 touch   可以创建一个空的新文件,若文件存在改变文件时间戳。 2 cat     查看文件内容,一次性输出所有内容, 如cat  filename -n :输出所有行编号 -b 对非空输出行号 3 more  可以分屏显示  按回车翻下一屏 4 less   可以分…

    Linux笔记 2018-07-02
  • linux学习练习

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。[root@localhost etc]# cp -r /etc/skel /home/tuser1[root@localhost ~]# getfacl /home/tuser1/getfacl: Removing lead…

    Linux笔记 2018-07-15
  • at和crontab命令简述

      at用于计划任务的执行,不过只能执行一次: 1 确定时间 at time 2 确定时刻想要执行的内容 command …     crontab 也用于执行计划任务,可以设定确定的时间多次执行计划任务,时间精确的分钟,可将计划任务按照规定的格式写入配置文件内,配置文件分为六个字段,分别是:分 时 日 月 周 要执…

    Linux笔记 2018-04-08
  • 第二周博客作业-N31-Linux文件、目录及其相关命令

    本篇文章主要围绕Linux的文件、目录及其相关的操作命令,如:mkdir、rmdir、tree、cat、tail、more、less、mv、cp、rm等命令

    2018-07-27
  • 使用Systemd把自作脚本服务化(加入开机启动)

    Systemd的出现,使得自己编写的脚本可更容易的添加进系统服务,进而实现开机启动。

    例如,我们可以把最简单的hello world脚本进行扩展,将其添加进系统服务,使之开机自启动。

    Linux笔记 2018-06-12