【发布时间】:2021-02-25 11:27:16
【问题描述】:
我已经通过几篇帖子找到解决问题的方法:除了 js 脚本之外的所有脚本似乎都由 Nginx 作为反向代理提供服务。
我有一个标准的单体 Rails 6 应用程序在 localhost 上运行。作为负载平衡容器之前的第一步,我开始配置 Nginx 以提供静态文件。
在 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(未找到)
css 或 jpg 类型的其他静态文件已正确获取,而 js 脚本则没有。 Nginx access.log 显示:
GET /assets/application.debug-(timestamp).css HTTP/1.1" 200
GET /assets/db-schema-(timestamp).jpg HTTP/1.1" 200
GET /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;
}
}
【问题讨论】:
标签: ruby-on-rails nginx nginx-reverse-proxy