Nginx 不为我的测试 Rails 应用提供服务

Nginx not serving my test Rails app

我的 Ubuntu 14.04 VPS 上的 nginx 服务器有问题。我成功安装了 Passenger(最新)和 Nginx。现在我创建了测试 Rails 应用程序 (rails 4.2.3)。我的应用程序的路径是 /home/testapp/public;

在 /opt/nginx/conf/nginx.conf 中我有这个设置:

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    passenger_root /usr/local/rvm/gems/ruby-2.2.1/gems/passenger-5.0.15;
    passenger_ruby /usr/local/rvm/gems/ruby-2.2.1/wrappers/ruby;

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


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

 #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        root /home/testapp/public;
        passenger_enabled on;
        rails_env production;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    server {
        listen       8001;

        server_name  testapp.com;
        passenger_enabled on;
        root /home/testapp/public;


        location / {
            root   html;
            index  index.html index.htm;
        }
    }



    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

如您所见,端口 80 和 8001 上有两个服务器配置。转到这两个端口,我得到默认的 nginx 页面。

我没有从 Passenger、Nginx 收到任何错误,Rails 一切正常。

注意:在安装 Passenger 和 nginx 模块时,我没有获得 sites-available 和 sites-enabled 文件夹,所以我在 /opt/nginx/ 路径下创建了这两个文件夹。

我需要做什么才能显示我的 rails testapp 而不是 nginx 默认页面?

在你的80、8001端口配置去掉

location / {
        root   html;
        index  index.html index.htm;
    }

部分,因为它覆盖了 root /home/testapp/public; 指令

这就是我在服务器标签中所需要的全部内容:

server {
    listen       80;
    server_name  localhost;
    passenger_enabled on;
    root /home/testapp/public;
    rails_env production;
}

并且还删除了:

location / {
        root   html;
        index  index.html index.htm;
    }