设置带有子目录的ASP Net Core应用

时间:2018-11-11 19:10:13

标签: c# nginx asp.net-core

我正在尝试在子目录(http://<website>/app)中设置ASP Net Core应用程序,但是该应用程序有一个问题,即无法识别其URL库应以/ app /开头。因此,当我向静态内容或动作发出请求时,它的作用就像是“ /”而不是“ / app”。 (例如:http://<website>/app/<static_content>是我所需要的,但是应用程序请求http://<website>/<static_content>

NGINX的设置如下:

server {
listen 80 default_server;
server_name <IP_Address>;
location /app/ {
    proxy_pass         http://localhost:4000/;
    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;
}}

我的program.cs

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseWebRoot(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .UseUrls("http://localhost:4000");
    }

并且我的startup.cs包含以下内容:

app.UsePathBase("/app");
app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),
                RequestPath = "/app/wwwroot"
            });

编辑: 我通过删除proxy_pass上的结尾斜杠来使其正常工作!

server {
    listen 80 default_server;
    server_name <IP_Address>;

    location /app1/ {
        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;
    }

    location /app2/ {
        proxy_pass         http://localhost:5020;
        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;
    }
}

两个应用程序现在似乎都发出了正确的请求。

2 个答案:

答案 0 :(得分:0)

要响应从//app的请求,请尝试以下Startup.cs中的代码

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

            }
        );

答案 1 :(得分:0)

Configure中的核心3.1确保已设置app.UsePathBase("/api or whatever subdir");