gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:37:09 24 4
gpt4 key购买 nike

我想使用与虚拟位置相同的 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com