I just got VM which had nginx, unicorn and a fresh rails app installed. I looked at the nginx config file, and I do not understand how it connects to unicorn, especially since there doesn't seem to be any upstream setting (which is what most tutorial's say you need).
Here are my config settings:
/home/unicorn/unicorn.conf
listen "127.0.0.1:8080"
worker_processes 2
user "rails"
working_directory "/home/rails"
pid "/home/unicorn/pids/unicorn.pid"
stderr_path "/home/unicorn/log/unicorn.log"
stdout_path "/home/unicorn/log/unicorn.log"
/etc/nginx/sites-enabled/default - only default file within sites-enabled directory
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|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|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;
}
}
The server seems to be defining a location #app, and passing requests to http://app_server ... I am not sure where does it obtain the app_server ... would that be an upstream defined elsewhere?
Fore more info, this article has the exact same set up as my vps. https://www.digitalocean.com/community/articles/how-to-1-click-install-ruby-on-rails-on-ubuntu-12-10-with-digitalocean
Normally app_server would be defined in another file as an upstream. For example - on my debian system I have a location set up similar to yours pointing to an upstream called www
in my /etc/nginx/sites-enabled directory I have a file called upstream_www_app with the following content
upstream www {
server localhost:24910 fail_timeout=0 weight=5;
}
24910 is the port defined in my applications config/unicorn.rb and my main nginx.conf includes all files in the sites-enabled directory.
Related
I am trying to set up apache in production server. My nginx configuration is shown below.
upstream puma {
server unix:///var/www/rails/shared/tmp/sockets/puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /var/www/rails/current/public;
access_log /var/www/rails/current/log/nginx.access.log;
error_log /var/www/rails/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri #puma;
location #puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
But I couldn't find how to specify socket in httpd configuration. Please help.
To set the port an Apache server listens on, use the Listen directive.
Here’s what it says in the default httpd.conf file (found in /etc/apache2/ in the apache2 install I’m using — although different installs/systems put the config directory in other locations):
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
#Listen 66.18.208.111:80
#Listen 66.18.208.112:80
Listen 80
If you are using named virtual hosts (multiple websites on one server) you’ll want to see the instructions for the NameVirtualHost and VirtualHost directives in Apache 2.2 vhosts documentation
I am deploying an app with a React front end created using create-react-app and a Rails API as the backend. I do not know much about configuring web servers and proxies so I am not sure exactly how to get it to work in production. I am deploying the app to Ubuntu on an Amazon EC2 instance. Nginx is the web server. I need to configure it so that Nginx serves the static files from the client/build directory and then any requests made to /api go to the Rails app running on port 3001. Right now Nginx is serving the index.html and the Javascript is running properly but requests to /api are not going to the right place. Any thoughts on how to configure Nginx to do this? Here is my /etc/nginx/sites-enabled/default file:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name mydomain.com;
passenger_enabled on;
rails_env staging;
root /home/ubuntu/app-name/current/client/build;
index index.html;
location /api {
proxy_pass http://127.0.0.1:3001;
}
}
What am I missing? How do I get the Rails app to run on port 3001 and have all requests to /api go there? Do I need a separate server block in this config?
I don't know if you already solved your issue and if this solution will apply to you but I think it might be useful for other people searching on this issue.
I am using Puma as the application server to run my rails 5 api.
This is the configuration file for my dev environment:
upstream app {
# Path to Puma SOCK file, here is where you make the connection to your application server
server unix:/path/to/your/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name mydomain.com;
# this is where my react-app is located
root /var/www/development/ram/public/;
index index.html index.htm;
# Serve the static content (React app)
location / {
try_files $uri /index.html =404;
}
location /api {
# Insert your public app path
root /your/rails-app/path/public;
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;
}
So comparing, I think your problem might be solved by adding a root directive pointing to your rails api public directory
I hope this can give you some hints on how to configure yours
Here is my working nginx config
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80 default_server;
root /home/deploy/www/sublime/current/public;
index index.html;
server_name domain.com;
access_log /home/deploy/www/sublime/logs/access.log;
error_log /home/deploy/www/sublime/logs/errors.log;
server_name localhost;
passenger_enabled on;
passenger_app_env production;
location ~* ^.+\.(jpeg|gif|png|jpg) {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
}
location /api {
# Insert your public app path
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_buffering off;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri /index.html;
}
}
You can use this repos to see rails configuration
https://github.com/Eth3rnit3/rails-react
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.
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
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"