Rails Nginx config can not reach root url - ruby-on-rails

Here is my nginx config
server {
listen 80;
server_name localhost;
root /home/user/app/public;
try_files $uri/index.html $uri #app;
location #app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Let's say my public ip: 111.111.111.111
When I try to enter the site 111.111.111.111/path, it works fine but when I try to enter only the root url 111.111.111.111 it gives me the "Welcome to nginx" message. I looked at the nginx log, but there is nothing about the root url.
I m also using unicorn, but I do not think there was a problem about that, I assume I ve added the necessary part of config here.

I think it might be missing the unicorn .sock file: see example nginx/unicorn/rails config here: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04
You're referencing an upstream in your proxy_pass line but it's not defined. Try adding this to the top:
upstream app {
# Path to Unicorn SOCK file
server unix:/path/to/your/app/shared/sockets/unicorn.sock fail_timeout=0;
}
I just looked at an app i did a while ago with rails, unicorn and nginx, and my entry for this was
server unix:/tmp/.sock fail_timeout=0;
It just depends how your unicorn is configured, as to where the sock file lives, so you'll need to tweak it to point to the right place.

Related

Rails app not appear at a server's IP (just a blank page)

I have stucked with production server. Already trying to resolve it for 2 days but do not understand why it happens like this.
The problem is:
I have tried to deploy Rails application with nginx/unicorn/capistrano. Finally, I got the process working. Nginx and unicorn seem ok on my production server, but when I enter to my server's IP, there is just a blank screen. Before I had there at least a message that nginx is runned. I suppose that the problem may be that nginx and unicorn are searching the app in a wrong place, but I checked the routes and they seem correct.
The application has a root welcome page, so it should be displayed there...
Could you help me with your thougts what might be a problem?
P.S. if I create a test rails app at server and run it using webricks, is showed at MY_SERVER_IP:3000, so the problem is definetly with unicorn/nginx.
My nginx.conf file:
upstream unicorn {
server unix:/tmp/unicorn.foreignernetwork.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name justforeign.com;
root /var/www/foreignernetwork/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
My unicorn.rb file:
root = "/var/www/foreignernetwork/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
listen "/tmp/unicorn.foreignernetwork.sock"
worker_processes 2
timeout 30
# Force the bundler gemfile environment variable to
# reference the capistrano "current" symlink
before_exec do |_|
ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile')
end

rails 4 nginx and unicorn serves only public files (html)

Running Ubuntu 12.10 x64, Nginx, Unicorn, Rails 4
bundle exec rake routes shows all the routes correctly, but when I access any routes (controller/action) it times out (nginx 504). When I open any static HTML files (public directory), it works fine.
Tried restarting server, nginx, unicorn, installed all the gems. What else am I missing?
This is my unicorn log
http://pastebin.com/5BHzqCA9
You have to add socket file to unicorn.rb config file and set it for listener. Something like this:
sock_file = "path/to/unicorn.sock"
......
listen sock_file
In your nginx conf you need:
upstream rails_server {
server unix:/path/to/sock;
}
server {
listen 80;
server_name example.com;
root /path/to/public;
access_log /path/to/log/nginx_access.log;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /path/to/public;
}
try_files $uri, #rails;
location #rails {
proxy_pass http://rails_server;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
}

Deploying sinatra app on rails app sub uri using( unicorn and nginx)

I have rails app running on unicorn+nginx. below is the nginx.conf and unicorn.rb configuration.
nginx.conf
upstream unicorn {
server unix:/tmp/unicorn.todo.sock fail_timeout=0;
}
server{
listen 80 default deferred;
#server_name localhost;
root /var/www/demo/public;
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
unicorn.rb
working_directory "/var/www/demo"
pid "/var/www/demo/tmp/pids/unicorn.pid"
stderr_path "/var/www/demo/unicorn.log"
stdout_path "/var/www/demo/unicorn.log"
listen "/tmp/unicorn.todo.sock"
worker_processes 2
timeout 30
It's working fine for me.
Now i wanted to deploy another small sinatra app rails app sub uri(localhost:3000/sinatraapp). DETAILS: As we know rails app running on localhost:3000, Now i am trying to configure sinatra app on localhost:3000/sinatraapp.
Please suggest me, How will i configure above requirement.
You can simply mount your Sinatra app in Rails' routes.rb:
mount SinatraApp, :at => "/sinatraapp"

nginx + passenger + rails: do I need to start the rails server or just start nginx?

I'm trying to have my rails server listen on 2 different ports. One solution proposed to me was to use nginx. I installed nginx with sudo passenger-install-nginx-module and added the following to /etc/nginx/conf.d:
server {
listen 80;
listen 10000;
server_name www.myapp.com
passenger_enabled on;
root /root/myapp/public;}
When I went to www.myapp.com I got a 403 Forbidden error. I figured it was because there were no static html files in /public. I dropped a simple "hello world" html page in there and it loaded correctly. I then proceeded to start my rails app using passenger start -e production, which caused it to run in standalone phusion passenger mode on port 3000. I go to myapp.com:3000 and I get the app. However, myapp:80 and myapp:10000 still don't work. I'm confused on how to get my nginx to point to the rails server I'm running. Am I doing this completely wrong? Thanks!
Set nginx to forward to my rails server using this https://gist.github.com/jeffrafter/1229497
worker_processes 1;
error_log /usr/local/var/log/nginx.error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream dev {
server 127.0.0.1:3000;
}
server {
listen 80;
# You could put a server_name directive here (or multiple) if
# you have not setup wildcard DNS for *.dev domains
# See http://jessedearing.com/nodes/9-setting-up-wildcard-subdomains-on-os-x-10-6
# If we choose a root, then we can't switch things around easily
# Using /dev/null means that static assets are served through
# Rails instead, which for development is okay
root /dev/null;
index index.html index.htm;
try_files $uri/index.html $uri.html $uri #dev;
location #dev {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://dev;
}
error_page 500 502 503 504 /50x.html;
}
}

setting up a virtual host with unicorn, nginx and capistrano in rails

I have been able to deploy my rials app into a vps system using nginx, unicorn and capistrano with no errors. Now, i want to deploy another rails app using the same nginx config(the two scripts are below) inside the same vps server and after running cap deploy:setup and cap deploy:cold it sets up correctly and the rails app is sent to the server. The problem i get is this. when i type service nginx restart i get the following error
nginx: [emerg] duplicate upstream "unicorn" in /etc/nginx/sites-enabled/cf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
my nginx script for the first app which is currently running is
upstream unicorn {
server unix:/tmp/unicorn.cf.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name cfmagazineonline.com;
root /home/deployer/apps/cf/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
my nginx config for the second rails app which fails to run but instead trows an error for the first rails app and makes it to crash is
upstream unicorn {
server unix:/tmp/unicorn.gutrees.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name gutrees.com;
root /home/deployer/apps/gutrees/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
any ideas how i can fix this issue and set up the virtual host correctly. Thank you
Name your second apps upstream differently (upstream names need to be unique). So instead of unicorn use i.e. the name #my_shiny_app_server. Then use this name proxy_pass directive http://my_shiny_app_server. Replace the my_shiny string with the name of your app e.g. gutrees, cf.

Resources