Apache反向代理到Kestrel无响应。

8
在 Ubuntu 18.04 服务器上,我安装了 Apache (目前运行多个 PHP 网站没有问题)。 现在我需要托管一个 .NET 应用程序(v 3.1)。
按照此教程操作 https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-3.1 如果没有错的话,应用程序正在运行。
# sudo systemctl status kestrel-helloapp.service
● kestrel-helloapp.service - Example .NET Web API App
   Loaded: loaded (/etc/systemd/system/kestrel-helloapp.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-03-10 15:27:21 UTC; 1h 33min ago
 Main PID: 70301 (dotnet)
    Tasks: 15 (limit: 4675)
   CGroup: /system.slice/kestrel-helloapp.service
           └─70301 /usr/bin/dotnet /var/www/helloapp/NetTest.dll

Mar 10 15:27:22 Clk-Dev-UbuntuApache-01 dotnet-example[70301]: info: Microsoft.Hosting.Lifetime[0]
Mar 10 15:27:22 Clk-Dev-UbuntuApache-01 dotnet-example[70301]:       Now listening on: http://localhost:5000
Mar 10 15:27:22 Clk-Dev-UbuntuApache-01 dotnet-example[70301]: info: Microsoft.Hosting.Lifetime[0]
Mar 10 15:27:22 Clk-Dev-UbuntuApache-01 dotnet-example[70301]:       Now listening on: https://localhost:5001
Mar 10 15:27:22 Clk-Dev-UbuntuApache-01 dotnet-example[70301]: info: Microsoft.Hosting.Lifetime[0]
Mar 10 15:27:22 Clk-Dev-UbuntuApache-01 dotnet-example[70301]:       Application started. Press Ctrl+C to shut down.
Mar 10 15:27:22 Clk-Dev-UbuntuApache-01 dotnet-example[70301]: info: Microsoft.Hosting.Lifetime[0]
Mar 10 15:27:22 Clk-Dev-UbuntuApache-01 dotnet-example[70301]:       Hosting environment: Production
Mar 10 15:27:22 Clk-Dev-UbuntuApache-01 dotnet-example[70301]: info: Microsoft.Hosting.Lifetime[0]
Mar 10 15:27:22 Clk-Dev-UbuntuApache-01 dotnet-example[70301]:       Content root path: /var/www/helloapp

我的Apache虚拟主机是这样定义的:
<VirtualHost *:80>
    ProxyPreserveHost On

    ProxyPass / http://localhost:5000/
    ProxyPassReverse / http://localhost:5000/
    ServerAdmin webmaster@localhost
    ServerName XXXX-api.AAAA.com.ar
    ServerAlias www.XXXX-api.AAAA.com.ar
    ErrorLog ${APACHE_LOG_DIR}/XXXX-api.error.log
    CustomLog ${APACHE_LOG_DIR}/XXXX-api.access.log combined
</VirtualHost>

.service 内容如下:

[Unit]
Description=Example .NET Web API App

[Service]
WorkingDirectory=/var/www/helloapp
ExecStart=/usr/bin/dotnet /var/www/helloapp/NetTest.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=afiori
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

当我访问XXXX-api.AAAA.com.ar时,它会将我重定向到XXXX-api.AAAA.com.ar:5001,并显示ERR_CONNECTION_TIMED_OUT错误。
错误日志显示如下:
[Tue Mar 10 15:06:49.538374 2020] [proxy_http:error] [pid 47754] [client 152.171.888.888:60836] AH01114: HTTP: failed to make connection to backend: 127.0.0.1
[Tue Mar 10 15:08:38.802652 2020] [proxy:error] [pid 9643] (111)Connection refused: AH00957: HTTP: attempt to connect to 127.0.0.1:5000 (127.0.0.1) failed
[Tue Mar 10 15:08:38.802698 2020] [proxy_http:error] [pid 9643] [client 152.171.888.888:60899] AH01114: HTTP: failed to make connection to backend: 127.0.0.1, referer: http://XXXX-api.AAAA.com.ar

我有点迷失了(我甚至不是.NET开发人员,我的技能在于PHP)


我定义了一个新的子域名testnet.XXXXX.com.ar`<VirtualHost *:80> ProxyPreserveHost OnProxyPass / http://127.0.0.1:5000/ ProxyPassReverse / http://127.0.0.1:5000/ ServerAdmin webmaster@localhost ServerName testnet.XXXXX.com.ar ServerAlias www.testnet.XXXXX.com.ar ErrorLog ${APACHE_LOG_DIR}/testnet.error.log CustomLog ${APACHE_LOG_DIR}/testnet.access.log combined</VirtualHost>`当我在浏览器中打开它时,它会重定向(307)到https://testnet.XXXXX.com.ar:5001/并显示ERR_CONNECTION_TIMED_OUT。 - drcrow
另一个更新:编辑了ports.conf并添加了Listen 5000,现在我得到了不同的错误:ERR_CONNECTION_REFUSED,头部显示 Location: https://testnet.XXXXX.com.ar:5001/ Server: Kestrel - drcrow
1个回答

0

所以,我遇到了相同的问题,但最终找到了解决方案... 这很困难,特别是因为微软没有编写适当的文档。

基本上,如果你从Apache得到重定向,这意味着Apache的工作已经完成,反向代理正在工作。实际上不起作用的是Kestrel。

我运行了

dotnet run

在我的树莓派机器上的项目中,我发现该进程正在 LOOPBACK(localhost,127.0.0.1)上运行。这意味着该网站只能从系统内部访问,而无法远程访问。

我相应地编辑了我的launchSettings.json,进行了替换。

(如果您不想远程访问开发机器,则可以省略编辑 launchSettings.json)

"applicationUrl": "https://localhost:5001;http://localhost:5000"

使用

"applicationUrl": "https://*:5001;http://*:5000"

然后我还编辑了我的服务文件,通过添加一个新的环境变量来补充现有的变量(在服务文件中可以有多个环境变量):

Environment=ASPNETCORE_URLS=https://*:5001;http://*:5000

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接