nGinx,多个地点为不同的温泉提供服务
我有一个非常简单的配置,我想用它来服务多个单页应用程序nGinx,多个地点为不同的温泉提供服务,nginx,single-page-application,Nginx,Single Page Application,我有一个非常简单的配置,我想用它来服务多个单页应用程序 server { root /var/www/; index index.html; location ~ ^/spa1/(.*?)$ { root /var/www/spa1/; } location ~ ^/spa2/(.*?)$ { root /var/www/spa2/; } error_page 404 /404.html; } /va
server {
root /var/www/;
index index.html;
location ~ ^/spa1/(.*?)$ {
root /var/www/spa1/;
}
location ~ ^/spa2/(.*?)$ {
root /var/www/spa2/;
}
error_page 404 /404.html;
}
/var/www/
的目录结构如下:
www/
|- 404.html (Generic 404)
|- index.html (A plain html page with links to the apps)
|- spa1/
|- index.html (the index page for single page app 1)
|- spa1.js
|- spa1.css
|- static/ (folder containing spa1 static files)
|- spa2/
|- index.html (the index page for single page app 2)
|- spa2.js
|- spa2.css
|- static/ (folder containing spa1 static files)
我的理解是,访问myserver.com/
将返回/var/www
中的index.html
页面,而访问myserver.com/spa1/
或myserver.com/spa2/
将返回每个页面应用程序的相应index.html
页面。然而,情况似乎并非如此——相反,如果我访问/spa1
或/spa2
我只会收到rootindex.html
页面,就好像它忽略了每个应用程序的root
指令一样
作为补充,有没有比我尝试的方式更正确的方式来服务多个单页应用程序?我不明白为什么URI
/spa1/
会返回文件/var/www/index.html
,以及您当前的配置。然而
对于所有三种情况,root
语句的值应为/var/www
。文件的路径是通过将root
语句的值与URI连接起来计算的
因此,您的目录结构和URI计划应该使用更简单的配置:
server {
root /var/www;
index index.html;
error_page 404 /404.html;
}
有关详细信息,请参阅。如果我错了,请纠正我的错误,但这不起作用,因为我的应用程序有
静态文件夹,其中包含index.html
和js/css文件中引用的图像-因为它们将相对于根进行搜索,所以/var/www/spa1.css
,而不是/var/www/spa1/spa1.css
。另外,如果spa1
和spa2
文件夹的名称与其位置uri不同,该怎么办?这些静态文件的uri是什么?您可以在nginx
访问日志中找到它。如果文件夹名称与URI不一致,则需要使用alias
指令。URI都是相对的,因此,例如
URI/static/js/app.js
仍然包含绝对路径,这意味着来自spa1和spa2的资源指向同一位置。您可以通过指定路径相对URI来获得成功,例如,src=static/js/app.js
,它相对于当前URI中的最后一个/
。啊,好吧,因此我想我的问题是:如何提供静态文件,以便index.js中的“/static/js/app.js”位于(例如)/spa1/
位置下?