Reputation: 315
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.
I have a standard monolith Rails 6 app running on localhost. As a first step before load balancing containers, I start to configure Nginx to serve static files.
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.
Nginx config file is shown below: the test nginx -t
is ok. 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:
GET //localhost:8080/packs/js/application-c294f00fc4a07bb438b5.js net::ERR_ABORTED 404 (Not Found)
The other static files of type css
or jpg
are correctly fetched whilst js script aren't. The Nginx access.log shows:
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
but nothing in the error.log.
What am I doing wrong?
The Nginx config is:
#/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;
}
}
Upvotes: 3
Views: 1746
Reputation: 315
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.
Upvotes: 2