Rails 4 server takes port number on web url to open the site
I've setup a rails 4 server on digital ocean using capistrano, unicorn and nginx. Host service - bluehost
I'm able to access the url using example.com:3000 as the rails app is running on port 3000. But I would like to access it directly through the hostname - example.com
How can I do that?
nginx.conf
upstream app_server {
server unix:/tmp/unicorn.example_project.socket fail_timeout=0;
}
server {
listen 80;
server_name 'example_project-staging@example_domain.com';
root /home/deploy/example_project/current/public;
location / {
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;
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}
}
unicorn.rb
# Set environment to development unless something else is specified
env = ENV["RAILS_ENV"] || "development"
# Production specific settings
if env == "staging"
app_dir = "example_project"
worker_processes 4
end
# listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
listen "/tmp/unicorn.#{app_dir}.socket", :backlog => 64
# Preload our app for more speed
preload_app true
# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30
# Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up.
working_directory "/home/deploy/#{app_dir}/current"
# feel free to point this anywhere accessible on the filesystem
user 'deploy', 'deploy'
shared_path = "/home/deploy/#{app_dir}/shared"
stderr_path "#{shared_path}/log/unicorn.stderr.log"
stdout_path "#{shared_path}/log/unicorn.stdout.log"
pid "#{shared_path}/tmp/pids/unicorn.pid"
root = "/home/deploy/#{app_dir}/current"
# Force the bundler gemfile environment variable to reference the capistrano "current" symlink
before_exec do |_|
ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile')
end
before_fork do |server, worker|
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
end
# Before forking, kill the master process that belongs to the .oldbin PID.
# This enables 0 downtime deploys.
old_pid = "#{shared_path}/pids/unicorn.pid.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
# the following is *required* for Rails + "preload_app true",
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
end
This fixed my issue:
Create a symlink for a service called unicorn_example_project-
sudo ln -nfs ~/example_project/current/config/unicorn_init.sh /etc/init.d/unicorn_example_project
Add the service to the linux startup system-
sudo update-rc.d -f unicorn_example_project defaults
Start the service-
sudo service unicorn_example_project start
链接地址: http://www.djcxy.com/p/32460.html