我正在设置 nginx 以使用 React SPA。当我不包含可变路径 (/rsvp/) 时,页面加载正常。但是当我尝试 (/rsvp/S0mEtH1NG) 时,它会下载索引、 list 、css 文件……但它实际上并没有将这些东西应用到 index.html。我几乎只看到我的 index.html,但实际上没有加载任何内容。
我的配置看起来像这样:
server {
listen 80;
server_name something.com;
location / {
root /srv/something.com/webroot;
index index.html;
}
location /rsvp {
alias /srv/rsvp/build;
try_files $uri $uri/ /rsvp/index.html;
}
}
如果我有 try_files $uri $uri//index.html
,它将加载根目录 (/) 的 index.html,但有一个类似的问题,即它不使用任何的 css。
感谢您的帮助,非常感谢!
最佳答案
有一个long standing issue使用 alias
和 try_files
。我会避免在同一个 block 中使用这些指令。尝试:
location /rsvp {
alias /srv/rsvp/build;
if (!-e $request_filename) { rewrite ^ /rsvp/index.html? last; }
}
参见 this caution关于 if
的使用。
关于Nginx try_files 在传递变量路径时加载空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45335781/