Apache reverse proxy to Kestrel not working
Asked Answered
O

1

8

In a Ubuntu 18.04 server I have Apache (currently running several PHP sites whit no problem). Now I need to host a .NET app (v 3.1).

Followed this tutorial https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-3.1

The app is working if I'm not wrong

# 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

My Apache virtualhost is defined this way:

<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>

The .service content is:

[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

When I go to XXXX-api.AAAA.com.ar it redirects me to XXXX-api.AAAA.com.ar:5001 and shows a ERR_CONNECTION_TIMED_OUT error

Error log shows this:

[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

I'm a bit lost here (I'm not even a .NET dev, my skills are in PHP)

Oscillator answered 10/3, 2020 at 19:11 Comment(2)
I defined a new subdomain testnet.XXXXX.com.ar <VirtualHost *:80> ProxyPreserveHost On ProxyPass / 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> When I open it in the browser it redirects (307) me to testnet.XXXXX.com.ar:5001 and shows ERR_CONNECTION_TIMED_OUTOscillator
Another update: edited ports.conf and added Listen 5000 now I get a different error: ERR_CONNECTION_REFUSED and the headers shows Location: https://testnet.XXXXX.com.ar:5001/ Server: KestrelOscillator
H
0

So, I was experiencing the same issue but I finally worked out a solution... it was tough especially because microsoft doesn't write proper documentation.

Basically, if you're getting a redirect from Apache it means Apache job is done and the reverse proxy is working. What is not working is actually Kestrel.

I ran

dotnet run

Inside my project on my raspbian machine. Then i found out the process is running on LOOPBACK (localhost, 127.0.0.1). Meaning, the site is only accessible from within the system but not remotely.

I edited my launchSettings.json accordingly, substituting

(If you don't want to access your development machine remotely you can omit editing launchSettings.json)

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

With

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

And then I also edited my service file, by adding a new environment variable, alongside the existing one (you can have multiple Environment variables in the service file):

Environment=ASPNETCORE_URLS=https://*:5001;http://*:5000
Hysterectomize answered 29/7, 2022 at 0:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.