简体   繁体   English

在apache部署do.net core API和angular 2 server url重写规则报错

[英]Deploy dotnet core API and angular in apache 2 server url rewrite rule error

I have a project having angular 11 as a front end and Do.net Core 5 API.我有一个以 angular 11 作为前端和 Do.net Core 5 API 的项目。

I have to deploy my project in a Ubuntu 20.04 using apache2 web server.我必须使用 apache2 web 服务器在 Ubuntu 20.04 中部署我的项目。

Steps what I followed to deploy and configure the apache server.我按照以下步骤部署和配置 apache 服务器。

Step1: I configured the Forward headers to handle the reverse proxy headers And in the next line If path is 404, redirecting to index.html page I used this because I want to deploy the application as a single unit deployment package Please refer to this link https://dzone.com/articles/publish-and-deploy-angular-and.net-core-applicatio第 1 步:我配置了转发标头来处理反向代理标头并且在下一行如果路径为 404,重定向到 index.html 页面我使用它是因为我想将应用程序部署为单个单元部署 package 请参考此链接https://dzone.com/articles/publish-and-deploy-angular-and.net-core-applicatio

           app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | 
             ForwardedHeaders.XForwardedProto
            });
            app.Use(async (context, next) =>
            {
                await next();
                
                if (context.Response.StatusCode == 404 && !System.IO.Path.HasExtension(context.Request.Path.Value))
                {                    
                    context.Request.Path = "/index.html";                   
                }
            });

Step2: I installed apache2 server and installed required do.net packages Step3: Created a sample.service file in etc/systemd/system directory and enabled it.步骤 2:我安装了 apache2 服务器并安装了所需的 do.net 包步骤 3:在 etc/systemd/system 目录中创建了一个 sample.service 文件并启用了它。

systemctl enable sample.service systemctl 启用 sample.service

Step4: Created a sample.config file in etc/apache2/sites-available directory and enabled it. Step4: 在 etc/apache2/sites-available 目录下创建一个 sample.config 文件并启用它。

a2ensite sample.conf a2ensite 示例.conf

please refer this link https://www.syncfusion.com/blogs/post/hosting-multiple-asp.net-core-apps-in-ubuntu-linux-server-using-apache.aspx请参考此链接https://www.syncfusion.com/blogs/post/hosting-multiple-asp.net-core-apps-in-ubuntu-linux-server-using-apache.aspx

and lastly restarted the apache server.最后重新启动 apache 服务器。

Everything seems to be working file but If a redirecting the url other than home I am getting 404. Typically url write issue.一切似乎都是工作文件,但如果重定向 url 而不是 home,我会得到 404。通常是 url 写入问题。

If I open www.example.com - it works If I Open www.example.com/login - throwing 404如果我打开www.example.com - 它有效如果我打开www.example.com/login - 抛出 404

Later I created the .htaccess file and written url rewrite rules后来我创建了 .htaccess 文件并编写了 url 重写规则

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
   RewriteRule ^index\.html$ - [L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_URI} !^/api/.*$
   RewriteRule . index.html [L]
 </IfModule>

and enabled config file again and restarted the apache2 server并再次启用配置文件并重新启动 apache2 服务器

a2ensite sample.conf a2ensite 示例.conf

But same issue occuring.但同样的问题发生。

My file structure looks like我的文件结构看起来像

File structure文件结构

So I am confusing that where to place the .htaccess file to enable url rewrite rules.所以我很困惑在哪里放置 .htaccess 文件以启用 url 重写规则。 In the root application path like / or inside wwwroot folder.在 / 之类的根应用程序路径中或在 wwwroot 文件夹中。

Or Am I going with the wrong process.还是我走错了流程。 Please suggest me if its not a proper way of doing如果这不是正确的做法,请建议我

Help me out with the other ways of doing so.帮助我解决其他方法。

Many thanks in advance.提前谢谢了。

I simply put my sample.conf file in /etc/apache2/conf-enabled instead of sites-available.我只是将我的 sample.conf 文件放在 /etc/apache2/conf-enabled 而不是 sites-available 中。 Then it worked然后它起作用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM