【问题标题】:Accessing localhost from apache2 with proxypass使用 proxypass 从 apache2 访问 localhost
【发布时间】:2021-03-24 00:59:05
【问题描述】:

所以我对这方面很陌生,但我正在尝试构建一个 Web 服务器来托管我自己的网站。它目前正在 Ubuntu 20.04 上通过我桌上的树莓派运行。

我已经完成了从服务器运行我的 .net 5 Web 应用程序的所有步骤。但是,当我尝试通过 http://192.168.1.14 访问该站点时,我得到一个奇怪的 307 重定向,并将我重定向到 https://192.168.1.14:5001。当然我也得到一个站点无法访问的错误。

我确实有我的网站 .conf 文件:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName BuildItWithDan
    ServerAlias www.BuildItWithDan
    DocumentRoot /var/www/BuildItWithDan
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ErrorLog /var/log/apache2/error.log
    CustomLog /var/log/apache2/access.log common
</VirtualHost>

当我运行我的 .net 5 应用程序时,我可以看到它可以正确加载和收听。

ubuntu@ubuntu:~$ /home/ubuntu/dotnet-64/dotnet /var/www/BuildItWithDan/BuildItWithDan.Web.dll

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[62]
      User profile is available. Using '/home/ubuntu/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /home/ubuntu

因此,当导航到 http://192.168.1.14 时,我可以在我的服务上看到一些 ping。

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://192.168.1.14/ - -
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.1 GET http://192.168.1.14/ - - - 307 0 - 38.2497ms
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://192.168.1.14/ - -
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.1 GET http://192.168.1.14/ - - - 307 0 - 1.6625ms

也许有什么值得注意的。如果我删除(或注释掉).conf 文件中的 ProxyPass 行,我就可以使用http://192.168.1.14 连接到服务器,但它只是给了我一个目录列表。 然后,如果我更改我的 DocumentRoot 路径 DocumentRoot /var/www/html 这将使用相同的 http://192.168.1.14 加载默认的 apache 站点就好了

我还运行了sudo a2enmod proxy proxy_http 以及其他几个我想不到的。

Module proxy already enabled
Considering dependency proxy for proxy_http:
Module proxy already enabled
Module proxy_http already enabled

任何帮助将不胜感激!

编辑:我能够从日志中找到这些。

[Tue Mar 23 21:35:45.053007 2021] [proxy:error] [pid 19685:tid 281472865419664] (111)Connection refused: AH00957: HTTP: attempt to connect to 127.0.0.1:5000 (127.0.0.1) failed
[Tue Mar 23 21:35:45.053198 2021] [proxy_http:error] [pid 19685:tid 281472865419664] [client 80.82.77.192:57200] AH01114: HTTP: failed to make connection to backend: 127.0.0.1

【问题讨论】:

    标签: linux apache asp.net-core webserver web-hosting


    【解决方案1】:

    你不需要

    ProxyPreserveHost On
    

    除非您的应用程序需要特定的主机名 (localhost)。

    您的 .net 应用正在侦听 localhost:

    info: Microsoft.Hosting.Lifetime[0]
          Now listening on: http://localhost:5000
    info: Microsoft.Hosting.Lifetime[0]
          Now listening on: https://localhost:5001
    

    如果您在 *:5000 和 *:5001 上绑定 .net 应用程序,事情可能会更容易。

    您发布的错误意味着代理无法连接到 127.0.0.1:5000,这通常意味着应用程序没有运行或响应。如果您从浏览器连接到http://127.0.0.1:5000,您应该会收到类似的错误。您首先需要解决此问题(与 apache 无关),然后设置代理通行证。

    【讨论】:

    • 谢谢@Inco - 这对我有用,现在我只需要弄清楚为什么我所有的 css、js 和 img 都会抛出 404。
    • 检查什么(apache 或您的应用程序)正在处理静态内容:问题可能就在那里。根据静态内容所在的位置,您必须相应地配置 apache。
    猜你喜欢
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多