如何使用websockets + mod_proxy_wstunnel + Tomcat7运行Apache2 ReversProxy?

时间:2013-10-22 15:51:22

标签: websocket apache2 tomcat7 reverse-proxy mod-proxy

我在Apache上使用websockets和我的反向代理配置存在很大问题。当我直接访问Tomcat时,带有websockets的应用程序可以正常工作。但是,一旦我尝试使用Apache,websockets就无法正常工作。

我用:

  • Windows上的Tomcat 7.0.42
  • Windows上的
  • Apache 2.4.6

<VirtualHost _default_:80>
  ServerName myserver

  RewriteEngine on 
  RedirectMatch ^/$ /myserver/ 
  RewriteRule ^/myserver$ /myserver/ [R]
  RewriteRule ^/myserver/active$ /myserver/active/ [R]

  ProxyRequests Off
  ProxyPreserveHost On
  ProxyVia On
  <Proxy *>
   AddDefaultCharset off
   Order deny,allow
   Allow from all
  </Proxy>

  LogLevel debug

  ProxyHTMLEnable On
  ProxyHTMLBufSize  102400
  ProxyHTMLExtended On
  ProxyHTMLStripComments Off
  ProxyHTMLDocType "<!DOCTYPE html>" XML
  ProxyHTMLMeta On

  DocumentRoot "${SRVROOT}/htdocs/"

  <Location /myserver/active/ws/atmsphr/>
    ProxyPass ws://localhost:8080/myapp/ws/atmsphr/
    ProxyPassReverse ws://localhost:8080/myapp/ws/atmsphr/
  </Location>


  ProxyPass /myserver/active/ ajp://localhost:8009/myapp/
  ProxyHTMLURLMap ajp://localhost:8009/myapp /myserver/active/
  <Location /myserver/active/>
   ProxyPassReverse ajp://localhost:8009/myapp/
   SetOutputFilter proxy-html
   ProxyHTMLURLMap /myapp/ /myserver/active/
   ProxyPassReverseCookiePath /myapp/ /myserver/active/
  </Location>

  ProxyPass /myserver/ ajp://localhost:8009/mylogin/
  ProxyHTMLURLMap ajp://localhost:8009/mylogin /myserver/
  <Location /myserver/>
   ProxyPassReverse ajp://localhost:8009/mylogin/
   SetOutputFilter proxy-html
   ProxyHTMLURLMap /mylogin/ /myserver/
   ProxyPassReverseCookiePath /mylogin/ /myserver/
  </Location>
</VirtualHost>

在Apache日志中,我可以看到工作人员已初始化:

[Tue Oct 22 17:25:21.625342 2013] [proxy:debug] [pid 4116:tid 164] proxy_util.c(1693): AH00925: initializing worker ws://localhost:8080/myapp/ws/atmsphr/ shared
[Tue Oct 22 17:25:21.625342 2013] [proxy:debug] [pid 4116:tid 164] proxy_util.c(1733): AH00927: initializing worker ws://localhost:8080/myapp/ws/atmsphr/ local

我遵循了Ordering ProxyPass指令,但第一个请求由mod_proxy_ajp处理,而不是由mod_proxy_wstunnel处理:

[Tue Oct 22 17:26:19.283043 2013] [proxy_http:debug] [pid 4116:tid 840] mod_proxy_http.c(1891): [client 192.168.5.68:49451] AH01113: HTTP: declining URL ajp://localhost:8009/myapp/websock/atmsphr?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=2.0.2-jquery&X-Atmosphere-Transport=websocket&X-Atmosphere-TrackMessageSize=true&X-Cache-Date=0&Content-Type=application/json&X-atmo-protocol=true                     
[Tue Oct 22 17:26:19.283043 2013] [proxy_ajp:debug] [pid 4116:tid 840] mod_proxy_ajp.c(713): [client 192.168.5.68:49451] AH00895: serving URL ajp://localhost:8009/myapp/ws/atmsphr?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=2.0.2-jquery&X-Atmosphere-Transport=websocket&X-Atmosphere-TrackMessageSize=true&X-Cache-Date=0&Content-Type=application/json&X-atmo-protocol=true

我的配置有什么问题?

2 个答案:

答案 0 :(得分:2)

经过长时间的研究,我发现了一种满足我要求的解决方法。我将通过HTTPS和端口443运行此Web应用程序,它可以完美地运行。我无法解释为什么默认HTTP端口80存在问题,但如果我通过端口443访问webapp则没有问题。此外,我通过HTTP尝试端口8000,它也有效。

要点:

Port 80 / HTTP  --> not working
Port 8000 / HTTP --> working
Port 443 / HTTPS --> working

总之,我有一个端口80的虚拟主机配置,其永久redirct为443.

答案 1 :(得分:0)

它与您的ProxyPass指令的有效顺序有关。查看server-status页面,看看它到底是什么。当您将它们嵌入Location块时,有效订单会从您编写它们的顺序更改。请参阅mod_proxy_wstunnel文档。