我的 nginx 配置文件如下所示:
passenger_enabled on;
passenger_ruby /home/inspection/.rbenv/shims/ruby;
rails_env production;
root /home/inspection/app/current/public;
location /build/ {
root /home/inspection/app/current/public;
autoindex off;
}
if (-f $document_root/under_maintenance.html) {
return 503;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /under_maintenance.html break;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
但是构建目录中的所有资产都没有出现并给出 404。例如https://docking-new.dockingpro.com/build/static/js/runtime-main.1f1f19a3.js如果你尝试,你会看到 404 错误。
可以直接访问根文件夹内的所有文件。
我需要在这里改变什么?
答案1
错误似乎出在当您在位置块内location /build/
指定时,将路径和请求连接起来以生成最终路径。让我们从块中删除该指令root
Nginx
root
URI
root
location /build/
这是更新后的代码:
passenger_enabled on;
passenger_ruby /home/inspection/.rbenv/shims/ruby;
rails_env production;
root /home/inspection/app/current/public;
location /build/ {
autoindex off;
}
if (-f $document_root/under_maintenance.html) {
return 503;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /under_maintenance.html break;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
重新启动sudo systemctl restart nginx
它就可以工作了。