【问题标题】:Express NodeJS server through Apache Proxy, error 404 for route with express parameters通过 Apache 代理 Express NodeJS 服务器,使用 express 参数的路由出现错误 404
【发布时间】:2018-08-27 07:53:42
【问题描述】:

我在将 Apache 设置为我的 Express/Node 应用程序的反向代理时遇到问题。可以通过正确的 URL 访问应用程序,但找不到带有 Express 参数的路由 404。

这是我的 server.js 路由配置:

app.get('/quiver/note/:path', (req, res, next) => {
   const note = getQuiverNote(req.params.path)
   res.json(note)
})

这里是我用于反向代理的 Apache conf:

<VirtualHost *:80>
   ServerName quiver-node-reader.local
   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full
   ErrorLog "/var/log/httpd/quiver-node-reader.local-error_log"
   CustomLog "/var/log/httpd/quiver-node-reader.local-access_log" common

   <Proxy *>
      #Require all granted
      Order deny,allow
      Allow from all
   </Proxy>

  <Location />
     ProxyPass http://127.0.0.1:8080/
     ProxyPassReverse http://127.0.0.1:8080/
  </Location>
</VirtualHost>

当我使用 http://127.0.0.1:8080 浏览我的应用程序时,一切正常。 当我通过 Apache 使用 http://quiver-node-reader.local 浏览我的应用程序时,我可以毫无问题地访问我的应用程序,但是当调用带有参数的路由时,这次我得到了 404 :(

示例: http://quiver-node-reader.local/quiver/note/data%2Fformat-z%20(dev).qvnotebook%2FC9EBA21F-D425-4C99-BCAB-8B7B20235FF0.qvnote -> 此路线无法到达 404 错误 http://127.0.0.1:8080/quiver/note/data%2Fformat-z%20(dev).qvnotebook%2FC9EBA21F-D425-4C99-BCAB-8B7B20235FF0.qvnote -> 可以联系到

这是来自 Apache 的日志:

127.0.0.1 - - [27/Aug/2018:09:48:25 +0200] "GET /quiver/note/data%2Fformat-z%20(dev).qvnotebook%2FC9EBA21F-D425-4C99-BCAB -8B7B20235FF0.qvnote HTTP/1.1" 404 75

【问题讨论】:

    标签: node.js apache express reverse-proxy


    【解决方案1】:

    终于弄清楚我的问题是什么。 我的 URL 参数是 URLENCODED 并且里面有 / 如果选项 AllowEncodedSlashes 关闭,Apache 将给出 404 not found。 更多信息在这里https://httpd.apache.org/docs/2.2/en/mod/core.html#allowencodedslashes

    这是我的配置以使其正常工作

    <VirtualHost *:80>
      ServerName quiver-node-reader.local
      ProxyRequests Off
      ProxyPreserveHost On
      ProxyVia Full
      ErrorLog "/var/log/httpd/quiver-node-reader.local-error_log"
      CustomLog "/var/log/httpd/quiver-node-reader.local-access_log" common
      AllowEncodedSlashes On      
    
      <Proxy *>
         #Require all granted
         Order deny,allow
         Allow from all
      </Proxy>
    
      <Location />
         ProxyPass http://127.0.0.1:8080/ nocanon
         ProxyPassReverse http://127.0.0.1:8080/        
      </Location>
    

    【讨论】:

      猜你喜欢
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 2017-11-29
      • 2015-05-22
      • 2017-11-18
      • 1970-01-01
      相关资源
      最近更新 更多