本文介绍了如何在Apache网站中托管ASP.NET Core Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的php应用程序是使用Yii2框架开发的,该应用程序托管在Centos服务器上的路径/var/www/html上.可以从http://somedomain.com/访问此应用程序.

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

我正在使用ASP.NET MVC Core开发的新API项目,需要在http://somedomain.com/v2/进行访问.

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

那么,是否可以将dotnet core应用程序托管在Apache站点内,并使它们同时工作?如果是,我该怎么做?

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?

推荐答案

您可以添加一个反向代理来做到这一点.假设您的ASP.NET Core应用通过以下方式在http://127.0.0.1:5000/上运行:

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

如果请求以http://somedomain.com/v2/开头,则代理到ASP.NET Core App.

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

首先,通过httpd.conf中取消注释这些行来配置代理模块:

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

然后为以/v2/ 开头的请求添加以下反向代理设置:

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.

有效的演示

这是一个在8089而不是80上监听的演示:

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

这篇关于如何在Apache网站中托管ASP.NET Core Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 00:41
查看更多