user2403255
user2403255

Reputation: 1

nginx cant connect to rails app

nginx error log says: failed (2: No such file or directory) and i see a 404 error in the browser. I am getting "welcome to nginx" page when visiting my application homepage (www.myapp.com).

I've tried lot of configuration variants. Here's the current one:

config file

user  mo;
worker_processes  4;
error_log /opt/nginx/logs/error.log;
pid   /opt/nginx/logs/nginx.pid;
events {
worker_connections  1024;
}

http {
passenger_root /home/mo/gemshome/gems/passenger-4.0.2;
passenger_ruby /usr/local/bin/ruby193;

rails_env production;

include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;


server {
listen       80;
    server_name  www.myapp.com;
    root /home/mo/myapp/public;
passenger_enabled on;
    location / {
        root   html;
        index  index.html index.htm;
    }

    # redirect server error pages to the static page /50x.html              

Yet, i am not sure if rails production environment is set up correctly (corresponding line at nginx config file).

I followed all or most of the phusion HOWTOs.

Application works when I run the webrick server. So I assumed my app works, but that is being run on port 3000 so i need to know how to run nginx and passenger together.

Upvotes: 0

Views: 2411

Answers (2)

Mohd Anas
Mohd Anas

Reputation: 644

In your server block after the *passenger_enabled on* write *rails_env development;* to the next line and remove the rails_env production; from the http block then reload the server.

Upvotes: 0

shybovycha
shybovycha

Reputation: 12235

Short answer

We are using thin webserver. The nginx is configured to redirect all requests to the running thin instance and that is the secret.

The idea is:

  • Rails is running at port, let's say, 3000
  • nginx handles all the statics at application' directory directly
  • nginx redirects all requests (which are not static ones) to localhost:3000

Example configurations

Here is nginx config file:

server {
    listen       80;
    server_name myapp.com;
    client_max_body_size 800M;
    client_header_timeout 23m;
    client_body_timeout 23m;
    send_timeout 23m;

    root /home/user/myapp/public/;
    error_log /home/user/myapp/log/nginx_errors.log;
    access_log /home/user/myapp/log/nginx_access.log;

# One more statics route
#    location /assets/(.+-[a-z0-9]+\.\w+) {
#        root /home/user/myapp/public/assets/$1;
#        internal;
#    }

    location /images/(.+)(\?.*)? {
        root /home/user/myapp/public/images/$1;
        internal;
    }

    location / {
        proxy_read_timeout 120;
        proxy_connect_timeout 120;

        proxy_redirect          off;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Sendfile-Type X-Accell-Redirect;

        if (-f $request_filename) {
            expires max;
            break;
        }

        if (!-f $request_filename) {
            proxy_pass http://127.0.0.1:3000;
            break;
        }
    }
}

And here is thin config file:

chdir: /home/user/myapp/
environment: production
address: 0.0.0.0
port: 3000
timeout: 30
log: log/thin.log
pid: tmp/pids/thin.pid
max_conns: 1024
max_persistent_conns: 100
require: []
wait: 30
servers: 1
daemonize: true
threaded: true

Getting application with thin and nginx run

So, to run your application where nginx handles all the statics and nicely redirects you from myapp.com to your Rails application' instance, do the following:

  1. Add thin gem dependency to your Gemfile: gem 'thin'
  2. Install all the bundles: bundle install
  3. Copy-and-paste and then edit the nginx config file to /etc/nginx/sites-available/myapp.conf
  4. Enable your website for nginx: ln -s /etc/nginx/sites-enabled/myapp.conf /etc/nginx/sites-available/myapp.conf
  5. Create your thin configuration file at /etc/thin, copy-and-paste content from this post and then correct it: mkdir /etc/thin && touch /etc/thin/myapp.yml
  6. Install thin globally for your system: thin install (and then just follow the instructions)
  7. Run thin and then run nginx: /etc/init.d/thin start && /etc/init.d/nginx restart

NOTE: do not forget to add a route to /etc/hosts if you are running the server locally and want to test your application on myapp.com domain:

127.0.0.1 myapp.com

Afterword

nginx, as far as I know, is mostly used as a proxy server or a statics-handling server. That happened because of its minimality and speed. nginx is really good at tasks like those.

Yet, Phusion Passenger is a really, really slow one. That I found out by myself at work. And that's why we switched to thin.

So my answer is based on my own experience, nothing more.

Still, you could use passenger instead of thin - nginx will still redirect you to your application. But it may take a lot more time to process your requests.

Hope this will help someone someday. Thank you for your question and happy coding!

Upvotes: 2

Related Questions