ubuntu - nginx上的多个网络核心应用程序同一个域

标签 ubuntu nginx .net-core

我想使用与虚拟位置相同的 url 从网络浏览器访问我的应用程序。
例如:

  • samedomain.com/app1
  • samedomain.com/app2

  • 我正在使用 ubuntu 服务器 20.04 Nginx 部署我的 .Net 核心 应用程序,app1 的文档根目录是/var/www/app1/wwwroot,app2 的文档根目录是/var/www/app2/wwwroot。我的问题是为每个应用程序提供 css、图像和 js 文件......这是我的 nginx 配置:
    upstream app1 {
      server 192.168.1.1:5003;
    }
    upstream app2 {
      server 192.168.1.1:5004;
    }
    server {
      listen 80 default_server;
      listen [::]:80 default_server;
      server_name apps.domain.com;
      root /var/www/apps;
      location / {
      try_files $uri $uri/ /index.html;
      proxy_read_timeout 300s;
      proxy_connect_timeout 75s;
    }
    location /app1/ {
        proxy_pass http://app1/;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        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://app2/;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
        }
    #Static Archhives
    #location ~* \.(js|css|svg|jpg|png)$ {
    #        root  /var/www/app1/wwwroot;
    #        expires 24h;
    #    }
    }
    
    我的 nginx 配置的最后一部分 , 只有 app1 可以加载静态文件,不能从 font awesome 加载图标, app2 无法 加载任何东西...
    #Static Archhives
    #location ~* \.(js|css|svg|jpg|png)$ {
    #        root  /var/www/app1/wwwroot;
    #        expires 24h;
    #    }
    }
    
    有人知道我如何为每个应用程序提供 css、js、jpg 文件吗?
    提前致谢

    最佳答案

    好吧,我的解决方案比我想象的要容易
    这是我的 Nginx 配置:

    #APPS DOMAIN
    upstream app1 {
        server 192.168.1.1:5003;
    }
    upstream app2 {
        server 192.168.1.1:5004;
    }
    
    
    server {
        listen 80;
        listen [::]:80;
        server_name apps.domain.com;
    
    location / {
            index index.html;
            root /var/www/apps;
    
            }
    
    #APP1
    location /app1 {
            proxy_pass http://app1;
            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-Real-IP   $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
            }
    
    #APP2
    location /app2 {
            proxy_pass http://app2;
            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-Real-IP   $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
            }
    
    }
    
    应用程序1 .Net Core 项目,我在 Startup Class 的 Configure 方法中添加了这些行:
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                //This Lines
                app.UsePathBase("/app1"); // DON'T FORGET THE LEADING SLASH!
                app.UseStaticFiles(); //DEFAULT STATIC FILES IN wwwroot
                //...
            }
    
    应用程序1 项目,我在主方法之外的程序类中添加了这些行:
    public static void Main(string[] args)
            {
                CreateWebHostBuilder(args).Build().Run();          
    
            }
    
            //This lines        
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseContentRoot(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
                    .UseStartup<Startup>();
           //...
    
    我在 中做同样的事情应用程序2 .Net 核心项目...

    关于ubuntu - nginx上的多个网络核心应用程序同一个域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69947346/

    相关文章:

    linux - 在 Ubuntu 上并行化进程

    ubuntu - Visual Studio 代码 clang 可执行文件

    php - file_get_contents 得到错误的结果

    nginx 位置根始终被位置/覆盖

    .net-core - 如何为控制台应用程序设置 ElmahCore?

    c# - 构建 : error MSB4062 Microsoft. Build.Tasks.ResolveComReference 时出现 .NET Core 错误

    database - 在 Ubuntu 18.04 bionic 上安装 Adminer

    security - 看似恶意的用户代理后服务器崩溃

    docker - 使用Dockerfile进行构建时如何解决 “No such file or directory”错误

    mongodb - 将 Parse.com 应用程序迁移到 AWS EC2 Ubuntu 时出现的问题