服务于静态网站和;使用NGINX的SPA(node.js)
我需要一种方法来服务静态html网站和SPA使用NGINX服务于静态网站和;使用NGINX的SPA(node.js),nginx,Nginx,我需要一种方法来服务静态html网站和SPA使用NGINX ->应该是static index.html页面 ->应指向node.js webserver支持的SPA 因此,如果有一个合适的静态html路由,它应该由NGINX提供,任何其他路由都应该由node.js webserver提供 e、 g ->由NGINX提供,因为有about.html ->由node.js提供服务,因为此路由没有合适的静态页面(没有register.html) 我创建了以下配置,但它似乎无法正常工作 upstr
- ->应该是static index.html页面
- ->应指向node.js webserver支持的SPA
upstream backend {
server localhost:8080;
}
server {
listen 80;
server_name website.com www.website.com;
root /var/www/website.com;
location = / {
try_files /index.html =404;
}
location = /index {
return 404;
}
location / {
try_files $uri/index.html =404 @backend;
}
location @backend {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
您的配置可能会简化为:
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ @backend;
}
location @backend {
...
}
你可以试试类似的东西
location / {
try_files $uri $uri.html $uri/ @backend;
}
为你把它分解。try\u文件
指令
按指定顺序检查文件是否存在,并使用找到的第一个文件进行请求处理
所以
$uri
-查找路径中定义的确切文件。对于css、图像、js等静态文件非常有用
$uri.html
查找扩展名为.html
的确切文件。这将获得您的about.html
$uri/
-查找它是一个目录,然后使用index
指令查找index
或index.html
@backend
-发送到代理应用程序将负责返回404此答案是否解决了问题?听起来你只是在简化他的配置文件。