[英]Rails 6 Nginx reverse proxy: problem serving static files
I have been through several posts to find a solution to my problem: all but js scripts seem to be served by Nginx as reverse proxy.我已经浏览了几篇文章来找到我的问题的解决方案:除了 js 脚本之外的所有脚本似乎都由 Nginx 作为反向代理提供。
I have a standard monolith Rails 6 app running on localhost.我有一个在本地主机上运行的标准单体 Rails 6 应用程序。 As a first step before load balancing containers, I start to configure Nginx to serve static files.
作为负载均衡容器之前的第一步,我开始配置 Nginx 来服务 static 个文件。
On the Rails side (in development mode), I set config.public_file_server.enabled = false
and run rails assets:precompile
so that it builds /public/assets
and /public/pack/
folders with timestamped files.在 Rails 方面(在开发模式下),我设置
config.public_file_server.enabled = false
并运行rails assets:precompile
以便它构建带有时间戳文件的/public/assets
和/public/pack/
文件夹。
Nginx config file is shown below: the test nginx -t
is ok. Nginx配置文件如下图:测试
nginx -t
是可以的。 I run the nginx service and start rails s
listening to port 3000. Then I open localhost:8080 (or localhost:3000) and I get a 404 in the browser:我运行 nginx 服务并启动
rails s
监听端口 3000。然后我打开 localhost:8080(或 localhost:3000),我在浏览器中得到一个 404:
GET //localhost:8080/packs/js/application-c294f00fc4a07bb438b5.js.net::ERR_ABORTED 404 (Not Found) GET //localhost:8080/packs/js/application-c294f00fc4a07bb438b5.js.net::ERR_ABORTED 404(未找到)
The other static files of type css
or jpg
are correctly fetched whilst js script aren't.其他 static 类型的
css
或jpg
文件被正确获取,而 js 脚本则没有。 The Nginx access.log shows: Nginx access.log 显示:
GET /assets/application.debug-(timestamp).css HTTP/1.1" 200 GET /assets/application.debug-(timestamp).css HTTP/1.1" 200
GET /assets/db-schema-(timestamp).jpg HTTP/1.1" 200 GET /assets/db-schema-(时间戳).jpg HTTP/1.1" 200
GET /packs/js/application-c294f00fc4a07bb438b5.js HTTP/1.1" 404获取/packs/js/application-c294f00fc4a07bb438b5.js HTTP/1.1" 404
but nothing in the error.log.但 error.log 中没有任何内容。
What am I doing wrong?我究竟做错了什么?
The Nginx config is: 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;
}
and和
#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;
}
}
ok, I found the problem.好的,我发现了问题。 I had to add to the server the root:
我必须将根添加到服务器:
root /path-to-my-public-folder/public;
and performance is ok.性能还可以。 Now, I don't see why Nginx found the css and jpg but not the scripts.
现在,我不明白为什么 Nginx 找到了 css 和 jpg 而不是脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.