Search code examples
linuxapacheasp.net-corewebserverweb-hosting

Accessing localhost from apache2 with proxypass


So I am very new to a lot of this but I am trying to build a web server to host my own website. It is currently running on Ubuntu 20.04 from a raspberry pi on my desk.

I have gone through all the steps to get my .net 5 web app running from the server. however when I try to access the site through http://192.168.1.14 I get an odd 307 redirect and it redirects me to https://192.168.1.14:5001. Of course I also get a Site can not be reached error.

I do have my site .conf file:

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

and when I run my .net 5 app, i can see that it loads and listens correctly.

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

So when navigate to http://192.168.1.14 I can see some pings on my service.

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

Maybe something noteworthy. If I remove (or comment out) the ProxyPass lines in the .conf file I then am able to connect to the server with http://192.168.1.14, but it just gives me a directory listing. Then if I change my DocumentRoot path DocumentRoot /var/www/html This will load the default apache site just fine with the same http://192.168.1.14

I have also run sudo a2enmod proxy proxy_http as well as a couple others I can not think of.

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

Any help would be greatly appreciated!

EDIT: I was able to find these from the logs.

[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

Solution

  • You don't need

    ProxyPreserveHost On
    

    unless your application needs a specific hostname (localhost).

    Your .net app is listening on localhost:

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

    Things might be easier if you bind your .net app on *:5000 and *:5001.

    The error you posted means the proxy couldn't connect to 127.0.0.1:5000 which usually means the application is not running or responding. If you connect from your browser to http://127.0.0.1:5000 you should get a similar error. You first need to fix this problem (which is not related to apache) then setup the proxy pass.