Tuesday, November 2, 2021

[SOLVED] How to host ASP.NET Core web-app inside an Apache website?

Issue

I have an old php application developed using the Yii2 framework hosted on a Centos server at the path /var/www/html. This application is accessible at http://somedomain.com/.

I am working on new API project developed using ASP.NET MVC Core which needs to be accessible at http://somedomain.com/v2/.

So, is it possible to host the dotnet core application inside an Apache site and have both of them work at the same time? If yes, how can I accomplish it?


Solution

You could add a reverse proxy to do that. Let's say your ASP.NET Core app runs on http://127.0.0.1:5000/ by:

dotnet run --urls=http://localhost:5000

if the request start with http://somedomain.com/v2/, then proxy to the ASP.NET Core App.

+-------+
|       +----------------------------------------+
|       |                                        |
|       |           PHP module                   |
|       |                                        |
|       +----------------------------------------+
|Apache2|
|       |
|  (80) |                   +--------------------+
|       | start with /v2/   |                    |
|       |                   |   Asp.Net Core App |
|       +------------------->                    |
|       |                   |     (5000)         |
|       |  reverse proxy    |                    |
+-------+                   +--------------------+

Firstly, configure proxy module by uncomment these lines in httpd.conf:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_http2_module modules/mod_proxy_http2.so

And then add the following reverse proxy settings for requests that start with /v2/:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass /v2/ http://127.0.0.1:5000/
    ProxyPassReverse / http://somedomain.com/
    ServerName somedomain.com
    ErrorLog ${APACHE_LOG_DIR}webapp1-error.log
    CustomLog ${APACHE_LOG_DIR}webapp1-access.log common
</VirtualHost>

Now it should work as expected.

A working Demo

Here's a demo that listens on 8089 instead of 80:

enter image description here



Answered By - itminus