Question

How to configure Nginx with Spree 3? Images not showing in Rail's production mode

I am trying to integrate Spree a popular ecommerce extension for Rails into my app. Every works fine except the images don’t show when run in production mode. So I searched and found that people are running into the same problem and the way to solved it is by reconfiguring the Nginx config file in /etc/nginx/sites-enabled

Now, my default config file is like so …

upstream app_server { server [YOUR IP ADRESS] fail_timeout=0; }

server {
        listen   80;
        root /home/rails/public;
        server_name _;
        index index.htm index.html;

        location / {
                try_files $uri/index.html $uri.html $uri @app;
        }

        location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
                        try_files $uri @app;
                }
         location @app {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_pass http://app_server;
    }
}

However, it was solution was solved by add this line in the nginx cofig file above

  location ~ ^/(system|assets|spree)/  {
    root /home/spree/[application's name]/current/public;
    expires max;
    break;
  }

Here are the references of the solution I found… 1st link from google group 2nd link from google group 3rd link from spree documentation

My quick solution, which works right now, is to set my Rails application in production mode to serve static files (go to production.rb and make serve_static_files to true)… but it was said that this would increases the work load of rails unnecessarily instead of having nginx do it.

So I am trying to find the proper solution to this. I tried add the suggested lines below my nginx config in sites-enabled … still nothing shows up … how do I go about this? I am stuck. Please help!

Btw, I am running on Ubuntu Ruby on Rails on 14.04 (Postgres, Nginx, Unicorn)

Subscribe
Share

I found a guide to deploy Spree which specifically talks about the common problem of serving static files in Spree. Here is the link.

This is what it says…

" Configure the Web Server to Use the public Directory

The recommended approach for handling static assets is to allow your web server to handle serving these files. If you want to follow this approach just make sure that it’s configured properly in the config/environments/production.rb of your Rails app.

config.serve_static_assets = false This is the default setting of Rails so it’s also fine if this setting is missing or commented out.

The following is an example of how to configure Apache so that its document root is pointing to the public folder.

<VirtualHost *:80>
ServerName www.mystore.com
DocumentRoot /webapps/mystore/public
<Directory /webapps/mystore/public>
Allow from all
Options ~~MultiViews
</Directory>
</VirtualHost>

Each web server will have its own method for doing this so please consult the appropriate documentation for more details. "

How do I configured my nginx to use the public folder?


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

To serve static assets directly with Nginx, you must make sure that the root is pointed to the location of your application’s public/ folder. So in your configuration above, you would need to change:

root /home/rails/public;

to:

root /home/spree/[application's name]/current/public;

For more info, check out the section on configuring Nginx in this tutorial: