【问题标题】:Rails 4 server takes port number on web url to open the siteRails 4 服务器使用 web url 上的端口号来打开站点
【发布时间】:2014-07-14 08:07:03
【问题描述】:

我已经使用 capistrano、unicorn 和 nginx 在数字海洋上设置了一个 rails 4 服务器。 主机服务 - bluehost

我可以使用 example.com:3000 访问 url,因为 rails 应用程序在端口 3000 上运行。但我想直接通过主机名访问它 - 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;
    }
  }
}

独角兽.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

【问题讨论】:

  • 显示nginx配置文件。
  • unicorn 在 TCP 端口 3000 上运行(如果你使用 unicorn 我认为你使用默认的 Rails 服务器,因为独角兽默认在 8080 上运行)但 nginx 配置在 socket.Show config/unicorn.rb 告诉你如何启动服务器。
  • 添加了 unicorn.rb 文件。
  • 我使用 bundle exec unicorn -E staging 启动了服务器,网站打开时使用了 example.com:8080 之类的 url
  • upstream app_server { server localhost:8080; }

标签: ruby-on-rails ruby-on-rails-4 nginx capistrano unicorn


【解决方案1】:

这解决了我的问题:

为名为 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

【讨论】:

    猜你喜欢
    • 2014-08-26
    • 2013-06-14
    • 2015-09-08
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    相关资源
    最近更新 更多