【发布时间】:2023-03-25 01:58:01
【问题描述】:
我需要一种使用 NGINX 为静态 html 网站和 SPA 提供服务的方法。
- https://website.com -> 应该是静态 index.html 页面。
- https://website.com/register -> 应该指向由 node.js 网络服务器提供支持的 SPA。
所以,如果有合适的静态 html 路由,它应该由 NGINX 提供,任何其他路由都应该由 node.js 网络服务器提供。
例如
https://website.com/about -> 由 NGINX 提供服务,因为有 about.html https://website.com/register -> 由 node.js 提供服务,因为该路由没有合适的静态页面(没有 register.html)。
我创建了以下配置,但它似乎无法正常工作。
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;
}
}
【问题讨论】:
标签: nginx