multiple net core apps same domain on nginx

Viewed 521

I want to use same url with virtual location to access from web browser to my apps. For example:

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

I'm using ubuntu server 20.04 and Nginx for deploy my .Net Core apps, the document root for app1 is /var/www/app1/wwwroot,the document root for app2 is /var/www/app2/wwwroot. My problem is serve the css, images and js archives for each app... this is my nginx configuration:

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;
#    }
}

whith the last part of my nginx configuration, only app1 can load statics files, not load the icons from font awesome, app2 can't load anything...

#Static Archhives
#location ~* \.(js|css|svg|jpg|png)$ {
#        root  /var/www/app1/wwwroot;
#        expires 24h;
#    }
}

somebody know how can i serve css, js, jpg files to each app?

Thanks in advance

1 Answers

Well, my solution was easier than i thought

This is my Nginx configuration:

#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;
        }

}

in app1 .Net Core project, i added these lines in Configure method from Startup Class:

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
            //...
        }

in app1 project, i added these lines in Program Class, outside the main method:

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>();
       //...

and i do the same in the app2 .Net Core project...

Related