Rails 4服务器通过web url上的端口号来打开该站点

我已经使用capistrano,独角兽和nginx设置了数字海洋上的rails 4服务器。 主机服务 - bluehost

我可以使用example.com:3000访问url,因为rails应用程序在端口3000上运行。但是我想直接通过hostname - example.com访问它

我怎样才能做到这一点?

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

这解决了我的问题:

为名为unicorn_example_project的服务创建符号链接 -

sudo ln -nfs ~/example_project/current/config/unicorn_init.sh /etc/init.d/unicorn_example_project

将该服务添加到Linux启动系统 -

sudo update-rc.d -f unicorn_example_project defaults

启动服务 -

sudo service unicorn_example_project start
链接地址: http://www.djcxy.com/p/32459.html

上一篇: Rails 4 server takes port number on web url to open the site

下一篇: Start a new site on port 80