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/

    相关文章:

    python - 在 Ubuntu 14.04 可信服务器上使用 nginx 和 Gunicorn 的多个 Django 应用程序

    .net-core - Azure Devops - dotnetcore 构建始终失败 - 进程无法启动

    c# - Azure 应用服务在应用程序之前终止 https?

    ubuntu - 将 SubDomain 指向 EC2 实例

    VMWare guest 上的 Git 状态缓慢但主机上没有

    java - 我尝试在 Ubuntu 20.04 中的 eclipse 中创建一个新项目,但它给了我这个错误 Errors generated during the build

    Docker - 无法构建简单的 dotnet webapi 容器 : COPY failed: stat/var/lib/docker/overlay2 error

    java - Headless Chrome --print-to-pdf 与亚洲字体

    php - 如何使nginx和php容器之间进行通信

    docker - docker中的nginx无法与localhost对话