使用Nginx将Mvc Net Core托管在root中并将暂存版本托管在子目录中

时间:2019-12-05 20:04:54

标签: c# asp.net-mvc nginx .net-core nginx-config

该想法是在网站根目录中提供生产mvc网络核心应用程序,并在子目录中提供暂存版本:

当前,nginx中的配置是这样的:

upstream myapp{
        server localhost:5000;
    }

upstream premyapp{
    server localhost:6000;
}

server {
    listen *:80;
    add_header Strict-Transport-Security max-age=15768000;
    return 301 https://$host$request_uri;
}

server {
    listen *:443    ssl;
    server_name my.website.com;
    ssl_certificate /home/vhost/ssl/myapp.crt;
    ssl_certificate_key  /home/vhost/ssl/myapp-decrypted.key;
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
    ssl_ecdh_curve secp384r1;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;
    ssl_stapling on; #ensure your cert is capable
    ssl_stapling_verify on; #ensure your cert is capable

    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;

    location / {
        proxy_pass  http://myapp;
    }

    location /pre {
        proxy_pass  http://premyapp;
    }
}

startup.cs文件中的配置具有以下配置:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ...

        if (env.IsStaging())
        {
            app.UseStaticFiles("/pre");
            app.UsePathBase("/pre");
        }
        else app.UseStaticFiles();

       ...
     }

但是它不起作用,生产应用程序不会重定向到任何控制器,并且暂存应用程序不会加载静态文件。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

问题出在静态文件配置中,现在是:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ...

        if (env.IsStaging())
        {
            app.UsePathBase("/pre");
            app.UseStaticFiles();
        }

       ...
     }

成功了!