项目

概览

个人资料

操作

代理接口

模块:mod_proxy

描述

lighttpd 的 mod_proxy 是一个反向代理。lighttpd 的 mod_proxy 目前不支持到后端服务器的 SSL/TLS 连接。

选项

mod_proxy 配置选项

  • proxy.server:用于发送请求的主机的后端服务器定义;每个后端主机的选项。每个文件扩展名都可以有自己的处理器。通过为同一扩展名指定多个主机来实现负载均衡。
  • proxy.debug:调试级别(介于 0 和 65535 之间的值)注意:在 v1.4.13 中使用 'enable|disable'。
  • proxy.balance:可能是 'fair'(默认)、'hash'、'round-robin' 或 'sticky' 之一。
    • 'fair' 或 'least-connection' 是基于负载的普通被动均衡。
    • 'round-robin' 为每个请求选择另一个主机。
    • 'hash' 对请求 URI 生成哈希值,并确保相同的请求 URI 始终发送到相同的主机。这可以由于更高的缓存局部性而大大提高后端服务器的性能。
    • 'sticky'(自 1.4.44 起)将来自相同(客户端)IP 的请求发送到相同的后端。
  • proxy.map-extensions | 将多个扩展名映射到同一个后端
  • proxy.forwarded:将 "Forwarded" 头部 (RFC7239) 附加到代理请求(自 1.4.51 起)
  • proxy.header:在代理的 HTTP 头部中执行主机和 URL 路径简单重映射的选项(自 1.4.46 起)
  • proxy.replace-http-host:启用/禁用在发送到后端服务器的请求中用 proxy.server 标签替换 Host 头部(默认:禁用)(自 1.4.51 起)(在 1.4.44 和 1.4.45 中存在,但在 1.4.46-1.4.50 中意外被忽略)

proxy.server 部分的结构

    ( <extension> => 
      ( [ <label> => ]
        ( "host" => <string> ,
          "port" => <integer> ),
        ( "host" => <string> ,
          "port" => <integer> )
      ),
      <extension> => ... 
    )
  • <extension>:是文件扩展名或前缀(如果以“/”开头);可以为空("")以匹配所有请求
  • <label>:在 mod_status 生成的统计信息中显示的可选名称,有助于指示哪个后端处理器处理了此扩展名
  • “host”:代理服务器的 IP(自 1.4.46 起,DNS 名称在 lighttpd 启动时解析为第一个 IP)
    (自 1.4.36 起:)如果主机以 “/” 开头,lighttpd 将尝试连接到 Unix 域套接字
  • “port”:代理服务器在“host”上使用的 TCP 端口(默认值:80)

例如:

    proxy.server = ( ".jsp" =>
                       ( ( 
                           "host" => "10.0.0.242",
                           "port" => 81
                         ) )
                     )

proxy.forwarded(自 1.4.51 起)是“Forwarded”头部中包含的参数列表。默认情况下未启用。要提供与 X-Forwarded-For 和 X-Forwarded-Proto 相同的信息,请启用“for”和“proto”。“remote_user”是 Forwarded 头部的一个 lighttpd 扩展,如果启用,它会添加由 mod_auth 设置的已认证用户。

    proxy.forwarded = ( "for"          => 1,
                        "proto"        => 1,
                        #"host"        => 1,
                        #"by"          => 1,
                        #"remote_user" => 1
    )

proxy.header(自 1.4.46 起)是用于在代理的 HTTP URL 路径和 Host 头部中执行简单前缀匹配以重新映射主机和 URL 路径的选项列表(提交 036d3d3d

    proxy.header = (
        #"map-host-request" => (
            #"-" => "...",#replace provided given Host request authority
            #"..." => "-",#preserve existing authority (no further matching)
            #"..." => "", #preserve existing authority (no further matching)
            #             #(equivalent to "xxx" => "xxx")
            #"xxx" => "yyy", #map one string ("xxx") to another ("yyy")
        #),
        #"map-host-response" => (
            #"-" => "...",#replace authority used in backend request
            #"..." => "-",#replace with original authority
            #"..." => "", #preserve existing authority (no further matching)
            #             #(equivalent to "xxx" => "xxx")
            #"xxx" => "yyy", #map one string ("xxx") to another ("yyy")
        #),
        #"map-urlpath" => (
            #"/xxx"  => "/yyy",#map one urlpath prefix to another
            #"/xxx/" => "/",   #map one urlpath prefix to another
            #"/xxx"  => "",    #map one urlpath prefix to another
            #"/key"  => "/value",
            # Note: request headers have matching "key" prefix replaced with
            # "value", and response headers have matching "value" prefix
            # replaced with "key", with a pre-test of the "value" from the
            # first-matched "key" in request headers (if there was a match)
        #),
        #"https-remap" => "enable",
            # For https requests from client, map https:// to http://
            # when map-host-request matches URI in request, and map http://
            # to https:// when map-host-response matches URI in response.
            # (mod_proxy currently sends all backend requests as http)
        #"upgrade" => "enable",
            # enable support for Upgrade: websocket
            # Depending on the websocket application, please also review
            # settings for server.max-read-idle and server.max-write-idle
        #"connect" => "enable",
            # permit HTTP CONNECT method to target backend (since 1.4.49)
        #"force-http10" => "enable",
            # force downgraded HTTP/1.0 requests to backends (since 1.4.57)
            # (mod_proxy sends HTTP/1.1 requests to backends (since 1.4.56))
    )

示例

使用 lighttpd + mod_proxy 作为 8 个 Squid 服务器的前端,这些 Squid 服务器为您处理动态内容的缓存。所有针对主机 www.example.org 的请求都应转发到代理。所有代理都在端口 80 监听请求。

  $HTTP["host"] == "www.example.org" {
    proxy.balance = "hash" 
    proxy.server  = ( "" => ( ( "host" => "10.0.0.10" ),
                              ( "host" => "10.0.0.11" ),
                              ( "host" => "10.0.0.12" ),
                              ( "host" => "10.0.0.13" ),
                              ( "host" => "10.0.0.14" ),
                              ( "host" => "10.0.0.15" ),
                              ( "host" => "10.0.0.16" ),
                              ( "host" => "10.0.0.17" ) ) )
  }

如果其中一个主机宕机,则所有针对该服务器的请求将平均分配到其他服务器。如果您想了解更多关于此处使用的算法,请“谷歌”搜索“Microsoft CARP”。

故障排除

如果您遇到

2007-05-02 09:45:48: (mod_proxy.c.397) connect failed: 8 Network is unreachable 101 
2007-05-02 09:45:48: (mod_proxy.c.871) proxy-server disabled: blabla.com 80 8
2007-05-02 09:45:48: (mod_proxy.c.1229) no proxy-handler found for: /

检查您是否为代理地址使用了 IP 地址。在 lighttpd 1.4.46 之前,允许使用主机名。自 lighttpd 1.4.46 起,DNS 名称在 lighttpd 启动时会解析为该主机从 DNS 返回的第一个 IP 地址。

示例:websocket 代理到 noVNC (自 1.4.46 起)

按照说明下载和安装,或使用适用于您的 Linux/*BSD 发行版的可用软件包。
https://github.com/novnc/noVNC
https://github.com/novnc/websockify

如果 noVNC 文件位于 /usr/share/novnc,请运行 novnc_server 并带上:$ novnc_server --web /usr/share/novnc
novnc_server 默认在端口 6080 上运行 websockify,并期望 VNC 服务器在端口 5900 上,除非另有配置。lighttpd 的 mod_proxy 可以配置在 websockify 前面,以提供可选的 SSL、身份验证、日志记录等功能。

server.modules += ( "mod_proxy" )
$HTTP["url"] =~ "^/websockify" {
    proxy.server = ( "" => ( ( "host" => "127.0.0.1", "port" => "6080" ) ) )
    proxy.header = ( "upgrade" => "enable" )
}

以上假设 VNC 服务器已在运行。请采取适当的预防措施来限制对 VNC 服务器的访问,可能包括要求适当的身份验证和限制对某些源 IP 的访问。请参阅 Docs_ModWStunnel 以获取 x11vnc 服务器的示例命令,并考虑使用 mod_wstunnel 以更直接的方式通过 lighttpd 访问 VNC 服务器。

gstrauss4 年多前 更新 · 43 次修订