nnetx上的dotnetcore 2.1 mvc - 路由问题

问题描述 投票:0回答:1

我一直致力于在ubuntu / nginx上托管一个dotnetcore 2.1 mvc应用程序。在我的本地(windows)开发环境中,一切都很好用。路由工作正常。我非常仔细地按照https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.1的说明。但是,当我尝试将应用程序发布到我配置的ubuntu nginx实例时,它将显示主页面。但是,如果我尝试通过它的路线转到任何其他页面,则会出现404错误。例如,我创建了一个应该位于/ home / success的页面,但是当我访问该页面时,它表示它不存在。如果我在登录实例时使用curl并将其指向http://localhost:5000/home/success,则会出现404错误。这是我正在使用的代码。

Startup.cs

        public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.Configure<appsettings>(Configuration.GetSection("appsettings"));
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        //app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

在/ etc / nginx的/网站可用/默认

server {
listen        80;
root          /var/www/cardnmore-customer/wwwroot;
server_name   ec2-54-149-68-43.us-west-2.compute.amazonaws.com
location / {
    proxy_pass         http://localhost:5000;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection keep-alive;
    proxy_set_header   Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
}

}

最后,这是我正在处理的应用程序的登台URL。 http://ec2-54-149-68-43.us-west-2.compute.amazonaws.com

我希望这是一个非常明显的事情,但是自从昨晚以来我一直在试图解决这个问题。任何帮助将不胜感激。

nginx asp.net-core-2.1
1个回答
0
投票

当然,在我发布这个问题之后,我发现重新启动nginx服务显然没有重新加载应用程序。我必须重新启动我的cardnmore-customer.service才能使我的更改成为可能。在我来到这里之前我做了一些事情,我怀疑其中一件事是有效的。我做的第一件事就是将它添加到startup.cs中的Configure void中

app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });

我做的另一件事是更新我的默认conf文件,以便它在server_name下有实际的url(原来是空白的)

server {
listen        80;
root          /var/www/cardnmore-customer/wwwroot;
server_name   ec2-54-149-68-43.us-west-2.compute.amazonaws.com
location / {
    proxy_pass         http://localhost:5000;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection keep-alive;
    proxy_set_header   Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
}

无论如何,如果有人有什么要添加到这里请随意。

© www.soinside.com 2019 - 2024. All rights reserved.