nginx + passenger + rails:我需要启动rails服务器还是只需启动nginx?

时间:2013-06-21 21:18:37

标签: ruby-on-rails nginx passenger

我正在尝试让我的rails服务器在两个不同的端口上侦听。向我提出的一个解决方案是使用nginx。我用sudo passenger-install-nginx-module安装了nginx并将以下内容添加到/etc/nginx/conf.d:

server {
    listen       80;
    listen       10000;
    server_name  www.myapp.com
    passenger_enabled on;
    root /root/myapp/public;}

当我去www.myapp.com时,我收到403 Forbidden错误。我想是因为/ public中没有静态的html文件。我在那里删除了一个简单的“hello world”html页面并正确加载。然后我开始使用passenger start -e production启动我的rails应用程序,这导致它在端口3000上以独立的phusion乘客模式运行。我去myapp.com:3000然后我得到了应用程序。但是,myapp:80和myapp:10000仍然无法正常工作。我对如何让我的nginx指向我正在运行的rails服务器感到困惑。我这样做完全错了吗?谢谢!

1 个答案:

答案 0 :(得分:0)

使用此https://gist.github.com/jeffrafter/1229497

将nginx设置为转发到我的rails服务器
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;
    }            
}