Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble serving Rails 3 app with nginx + unicorn on EC2
    text
    copied!<p>When I point my browser to "rails_app.com", I can view rails_app/public/index.html on the EC2 instance, but when I delete rails_app/public/index.html I get the nginx 403 error, </p> <pre><code> directory index of "/home/www-data/rails_app/public/" is forbidden, </code></pre> <p>instead of being able to see my Rails 3.0.3 rails_app. I'm using</p> <pre><code>Nginx Version 0.8.54 Unicorn_rails Version 3.4.0 </code></pre> <p>One post I read said that I need to make sure that the correct version of ruby is available for user www-data, which I did. I have the directory owners for the rails_app set for user and group to www-data, and the permissions set to 775. I can see that the HTTP requests are reaching nginx by looking at the access log. I have DNS setup for my domain name, "rails_app.com" to point to an AWS Elastic IP address which is the Elastic IP of the EC2 instance that I am running nginx + unicorn with my rails app on. </p> <p><strong>I think I solved this 403 error above as described in Update 2 below. Now I get a 500 error. I fixed the 500 error, as described in Update 4 below.</strong></p> <p>I am runnng nginx with </p> <pre><code>/usr/sbin/nginx -c /home/www-data/rails_app/config/nginx.conf </code></pre> <p>and I am running unicorn with</p> <pre><code>bundle exec unicorn_rails -p 8000 -E production -c /home/www-data/rails_app/config/unicorn.rb -D </code></pre> <p>Here is my unicorn.rb:</p> <pre><code>worker_processes 1 preload_app true main_dir = '/home/www-data/rails_app' working_directory '/home/www-data/rails_app' listen 8000, :tcp_nopush =&gt; true listen "/tmp/shop.socket", :backlog =&gt; 2048 timeout 30 user 'www-data' shared_path = "#{main_dir}/shared" pid "#{shared_path}/pids/unicorn.pid" stderr_path "#{shared_path}/log/unicorn.stderr.log" stdout_path "#{shared_path}/log/unicorn.stdout.log" </code></pre> <p><strong>UPDATE 1 -</strong> I simplified my nginx.conf, after reading this <a href="http://wiki.nginx.org/Pitfalls" rel="nofollow">link</a> on Nginx Pitfalls. Here is my nginx.conf:</p> <p><strong>UPDATE 2 -</strong> This <a href="http://www.ruby-forum.com/topic/2437227" rel="nofollow">post</a>, said that having $uri/ in try_files causes the 403 error, because nginx can't list the directory. So I removed $uri/ from try_files and now I get a 500 error, with nothing showing up in the nginx or unicorn error logs. Any ideas how to debug this? </p> <pre><code>user www-data www-data; worker_processes 2; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; accept_mutex on; } http { include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay off; keepalive_timeout 65; client_body_timeout 120; upstream app_server { # server 127.0.0.1:8000 fail_timeout=0; server unix:/tmp/shop.socket fail_timeout=0; } log_format main '\$remote_addr - \$remote_user [\$time_local] \$request ' '"\$status" \$body_bytes_sent "\$http_referer" ' '"\$http_user_agent" "\$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; server { listen 0.0.0.0:80 ; server_name rails_app.com *.rails_app.com; index index.html index.htm index.php; root /home/www-data/rails_app/public; client_max_body_size 50M; location / { try_files $uri/index.html $uri.html $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; } # location / error_page 500 502 503 504 /500.html; location = /50x.html { root /home/www-data/rails_app/public; } } # server } # http </code></pre> <p>Using a TCP port for unicorn, I can see that unicorn is listening using</p> <pre><code>netstat -natp </code></pre> <p>Now the error I get is the 500.html page displayed:</p> <pre><code> We're sorry, but something went wrong. </code></pre> <p>There are no errors in the nginx error logfile or in the unicorn error log file.</p> <p><strong>Update 3 -</strong></p> <p>As I dig into this more, I think the problem might be with unicorn. When I run the rails app at this <a href="http://sirupsen.com/setting-up-unicorn-with-nginx/" rel="nofollow">example</a>, it works fine, but that rails app is using sqlite3 instead of mysql. Also this example allows for zero downtime deployment, by using the before_fork and after_fork code here:</p> <pre><code> before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end </code></pre> <p>When I try to run my rails_app with mysql and using the before_fork, after_fork code above in my unicorn.rb file, it fails and I get the error:</p> <pre><code>active_record/connection_adapters/abstract/connection_pool.rb:316:in `retrieve_connection': ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished) </code></pre> <p><strong>Update 4 -</strong></p> <p>I added -d (debug) to the unicorn_rails command line as</p> <pre><code>bundle exec unicorn_rails -p 8000 -E production -c /home/www-data/rails_app/config/unicorn.rb -d -D </code></pre> <p>and found a number of errors, the first being that config/application.rb was missing config.secret_token. Added that and continue to debug.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload