Nginx上的dotnetcore 2.1 MVC-路由问题

时间:2019-04-24 13:18:50

标签: nginx asp.net-core-2.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 / sites-available / default

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

我希望这是很痛苦的事情,但是自从昨晚以来,我一直在想办法解决这个问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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;
}

无论如何,如果有人要添加任何内容,请随时