[英]Rails 6 Nginx reverse proxy: problem serving static files
我已经浏览了几篇文章来找到我的问题的解决方案:除了 js 脚本之外的所有脚本似乎都由 Nginx 作为反向代理提供。
我有一个在本地主机上运行的标准单体 Rails 6 应用程序。 作为负载均衡容器之前的第一步,我开始配置 Nginx 来服务 static 个文件。
在 Rails 方面(在开发模式下),我设置config.public_file_server.enabled = false
并运行rails assets:precompile
以便它构建带有时间戳文件的/public/assets
和/public/pack/
文件夹。
Nginx配置文件如下图:测试nginx -t
是可以的。 我运行 nginx 服务并启动rails s
监听端口 3000。然后我打开 localhost:8080(或 localhost:3000),我在浏览器中得到一个 404:
GET //localhost:8080/packs/js/application-c294f00fc4a07bb438b5.js.net::ERR_ABORTED 404(未找到)
其他 static 类型的css
或jpg
文件被正确获取,而 js 脚本则没有。 Nginx access.log 显示:
GET /assets/application.debug-(timestamp).css HTTP/1.1" 200
GET /assets/db-schema-(时间戳).jpg HTTP/1.1" 200
获取/packs/js/application-c294f00fc4a07bb438b5.js HTTP/1.1" 404
但 error.log 中没有任何内容。
我究竟做错了什么?
Nginx 配置为:
#/nginx.conf
worker_processes auto;
worker_rlimit_nofile 1024;
error_log /usr/local/etc/nginx/logs/error.log warn;
events {
worker_connections 1024;
# accept_mutex on; # "on" if nginx worker_processes > 1
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options SAMEORIGIN;
include /servers*.;
include /usr/local/etc/nginx/rails.conf;
}
和
#rails.conf
#upstream app {
# server http://localhost:3000;
#}
server {
listen 8080;
server_name localhost;
gzip on;
gzip_proxied no-cache no-store private expired auth;
gzip_types
"application/json;charset=utf-8" application/json ..."
location ~ ^/(assets|packs)/{
try_files $uri @rails;
access_log off;
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
break;
}
location / {
try_files $uri @rails;
}
location @rails {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://localhost:3000; # http://app
proxy_http_version 1.1;
proxy_set_header Connection ‘’;
proxy_buffering off;
proxy_cache off;
chunked_transfer_encoding off;
}
}
好的,我发现了问题。 我必须将根添加到服务器:
root /path-to-my-public-folder/public;
性能还可以。 现在,我不明白为什么 Nginx 找到了 css 和 jpg 而不是脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.