部署 rails 用 ngnix + passenger 部署,没有成功,求问原因

beyondyuqifeng · 2013年09月08日 · 最后由 aptx4869 回复于 2013年09月10日 · 4728 次阅读

环境:ubuntu 12.10 x64 server 背景: 根据http://ruby-china.org/wiki/install-rails-on-ubuntu-12-04-server 这个 wiki 在 ununtu 上部署 rails 4 nginx 和 passenger 都正确安装没有提示错误“Nginx with Passenger support was successfully installed.” nginx 的配置文件也是按照 wiki 的教程配置的,不过就多了 passenger_root 和 passenger_ruby:

passenger_root /home/shhuqi/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.16;
passenger_ruby /home/shhuqi/.rvm/wrappers/ruby-2.0.0-p247/ruby;

当前遇到两个问题: 1、用 sudo /etc/init.d/nginx stop 无法停止 nginx 服务(当前我用 sudo /opt/nginx/sbin/nginx -s stop 才能停止)

2、访问域名一直是 ngnix 的 403 页面,貌似 passenger 没有启动?rails 服务也没有启动?

各位求解啊

建议核对下 ruby 的版本号和 passenger 的版本号,wiki 上的不一定是最新的

403 一般是权限不对 你看下运行 Nginx 的 user,有没有访问你这个目录的权限

#1 楼 @shanks 这两个版本也有关系么?我这边的版本如下: ruby 2.0.0p247 Phusion Passenger 4.0.16

#2 楼 @o_0 我这边的权限都给 shhuqi 这个帐号了么,这个给我感觉是 passenger 没有起来,nginx 都起来了,就是一个 nginx 页面啊

第二个问题,已经解决,我把 nginx 配置文件 (nginx.conf) 中的下面 5 行去掉,rails 就能够起来了,不然 rails 死活起不来,一直是 nginx 的 403 页面:

server {    
    # 此处用于防止其他的域名绑定到你的网站上面
    listen 80 default;
    return 403;
  }

这个什么原因啊,求解?@huacnlee 大神这个能否解答一下啊?

给的信息不够,无法解答,请切之前完整的 nginx.conf

你的注释不是写清了,# 此处用于防止其他的域名绑定到你的网站上面吗 然后你要让 nginx.conf 里的 user 行的用户和组和你的 rails 原码的所有者是一样的,而且这个用户对文件夹有读写权限

你现在用 ps aux | grep nginx 应该看到用户是 nobody 这是因为源码和 nginx.conf 的 user 不一致,nginx 实例的执行用户(变成了 nobody) 权限不足

#6 楼 @huacnlee rails 不能起来的 nginx.conf

user  shhuqi;
worker_processes  1;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    passenger_root /home/shhuqi/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.16;
    passenger_ruby /home/shhuqi/.rvm/wrappers/ruby-2.0.0-p247/ruby;

    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 64;

    access_log  /var/log/nginx/access.log;
    error_log   /var/log/nginx/error.log;

     client_max_body_size 50m;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;
    gzip_disable "msie6";

    server {    
        # 此处用于防止其他的域名绑定到你的网站上面
        listen 80 default;
        return 403;
    }

    server {
        listen       80;
        server_name  www.mysite.com;
        root         /home/shhuqi/www/enterpriseshow/public;
        passenger_enabled on;
        rails_env    production;

        location ~ ^(/assets) {
           access_log        off;
           expires           max;
        }

    }
}

rails 可以起来的 nginx.conf

user  shhuqi;
worker_processes  1;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    passenger_root /home/shhuqi/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.16;
    passenger_ruby /home/shhuqi/.rvm/wrappers/ruby-2.0.0-p247/ruby;

    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 64;

    access_log  /var/log/nginx/access.log;
    error_log   /var/log/nginx/error.log;

     client_max_body_size 50m;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;
    gzip_disable "msie6";

    server {
        listen       80;
        server_name  www.mysite.com;
        root         /home/shhuqi/www/enterpriseshow/public;
        passenger_enabled on;
        rails_env    production;

        location ~ ^(/assets) {
           access_log        off;
           expires           max;
        }

    }
}

麻烦看看,多谢!

相当可能是因为你没用域名访问……你是不是直接用 localhost 或者 ip 地址来访问啊……

#7 楼 @ZombieCoder 都是在 shhuqi 这个用户下的,rails 代码也在这个用户下,nginx 配置文件中也是 shhuqi 这个用户的

#10 楼 @aptx4869 域名我这里是故意隐去了的,用的是可用的域名

可以看这里 http://nginx.org/en/docs/http/request_processing.html

In this configuration nginx tests only the request’s header field “Host” to determine which server the request should be routed to. If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port. In the configuration above, the default server is the first one — which is nginx’s standard default behaviour. It can also be set explicitly which server should be default, with the default_server parameter in the listen directive:

server { listen 80 default_server; server_name example.net www.example.net; ... }

The default_server parameter has been available since version 0.8.21. In earlier versions the default parameter should be used instead.

#12 楼 @beyondyuqifeng 反正域名肯定没匹配对,不然不会进到那个 default 里面去的

需要 登录 后方可回复, 如果你还没有账号请 注册新账号