我想使用具有虚拟位置的相同url从web浏览器访问我的应用程序。例如:
- samedomain.com/app1
- samedomain.com/app2
我正在使用ubuntu服务器20.04和Nginx部署我的.Net Core应用程序,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;
# }
}
whith我的nginx配置的最后一部分,只有app1可以加载静态文件,不能从字体加载图标真棒,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;
}
}
在app1.Net Core项目中,我在启动类的配置方法中添加了这些行:
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
//...
}
在app1项目中,我在主方法之外的程序类中添加了这些行
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>();
//...
我在app2.Net Core项目中也这样做。。。