大网数据广告活动
服务器运维问答

Apache ProxyPass only one file to a different localhost port?

· 52CE 我爱测

I haven't been able to find a solution to this exact problem: I have one app running on localhost:3000 I have a proxy set up for my localhost:3000 app with qa.mysite.com (for other js) I added a second app running on localhost:33…

问题原文

原帖链接 · Bobert · CC BY-SA 3.0

I haven't been able to find a solution to this exact problem:

  • I have one app running on localhost:3000
  • I have a proxy set up for my localhost:3000 app with qa.mysite.com (for other js)
  • I added a second app running on localhost:3333

The first app needs to reference some localhost:3333 css and js files:

<link rel="stylesheet" href="/css/my-css.css"/>
<script src="/js/my-js.js" defer></script>

I tried this conf (and variations):

<VirtualHost *:443>
    ServerName qa.mysite.com
    SSLEngine on
    SSLProxyEngine on
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off
    SSLCertificateFile "/etc/apache2/server.crt"
    SSLCertificateKeyFile "/etc/apache2/server.key"
    ProxyVia Full
    ProxyPreserveHost On
    ProxyPass "/" "http://localhost:3000/"
    ProxyPassReverse "/" "http://localhost:3000/"

    ProxyPass "/css/my-css.css" "http://localhost:3333/css/my-css.css"
    ProxyPassReverse "/css/my-css.css" "http://localhost:3333/css/my-css.css"
    ProxyPass "/js/my-js.js" "http://localhost:3333/js/my-js.js"
    ProxyPassReverse "/js/my-js.js" "http://localhost:3333/js/my-js.js"

</VirtualHost>
  • Hitting qa.mysite.com yields the first app running at localhost:3000.
  • But also CORS errors or refused connections.

Any idea what I'm doing wrong? :)

已采纳解答

原帖链接 · Aiwa · CC BY-SA 3.0

You need to put the more specific ProxyPass rules first. "/css/my-css.css" will evaluate "/" first. Since that holds true, you'll get port 3000.

Put the ProxyPass to "/" last as it's the all-inclusive fallback.

<VirtualHost *:443>
    ServerName qa.mysite.com
    SSLEngine on
    SSLProxyEngine on
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off
    SSLCertificateFile "/etc/apache2/server.crt"
    SSLCertificateKeyFile "/etc/apache2/server.key"
    ProxyVia Full
    ProxyPreserveHost On

        #Specific /css
    ProxyPass "/css/my-css.css" "http://localhost:3333/css/my-css.css"
    ProxyPassReverse "/css/my-css.css" "http://localhost:3333/css/my-css.css"

        #Specific /js
    ProxyPass "/js/my-js.js" "http://localhost:3333/js/my-js.js"
    ProxyPassReverse "/js/my-js.js" "http://localhost:3333/js/my-js.js"

       #Catch ALL for servername
    ProxyPass "/" "http://localhost:3000/"
    ProxyPassReverse "/" "http://localhost:3000/"
</VirtualHost>

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-02-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。