努力使 RoR 应用程序(passenger、nginx)在 EC2 上运行

努力使 RoR 应用程序(passenger、nginx)在 EC2 上运行

我在 EC2 Ubuntu 实例上安装了 RoR、ruby、passenger 和 nginx。现在,我正在努力使其工作,并且只能通过如下所示的 ec2 地址进行访问http://ec2-11-222-333-44.ap-southeast-2.compute.amazonaws.com/

我已经完成了所有需要做的事情。但是,我不知道如何设置 nginx。这是我的/etc/nginx/nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections 768;
  # multi_accept on;
}

http {

  ##
  # Basic Settings
  ##

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
  # server_tokens off;

  # server_names_hash_bucket_size 64;
  # server_name_in_redirect off;

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

  ##
  # Logging Settings
  ##

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

  ##
  # Gzip Settings
  ##

  gzip on;
  gzip_disable "msie6";

  # gzip_vary on;
  # gzip_proxied any;
  # gzip_comp_level 6;
  # gzip_buffers 16 8k;
  # gzip_http_version 1.1;
  # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  ##
  # nginx-naxsi config
  ##
  # Uncomment it if you installed nginx-naxsi
  ##

  #include /etc/nginx/naxsi_core.rules;

  ##
  # nginx-passenger config
  ##
  # Uncomment it if you installed nginx-passenger
  ##

  #passenger_root /usr;
  #passenger_ruby /usr/bin/ruby;

  ##
  # Virtual Host Configs
  ##

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}


#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
# server {
#   listen     localhost:110;
#   protocol   pop3;
#   proxy      on;
# }
# 
# server {
#   listen     localhost:143;
#   protocol   imap;
#   proxy      on;
# }
#}


server {
        listen       80;
        server_name  localhost;
        root   /home/ubuntu/my_app/public;
        passenger_enabled on;

        #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   /home/ubuntu/my_app/public;
        }
}

我的问题:

  1. 我是否必须在 /my_app/public 目录中有一个 index.html?如果是,那为什么?这是一个 Rails 应用程序,而不是静态应用程序?

  2. 我是否必须指定?root /home/ubuntu/my_app/public;如果是,它与 /public 目录有什么关系?再说一遍,它是一个 Rails 应用程序,而不是静态应用程序?

  3. 我该怎么做才能让它工作呢?

答案1

一些答案:

  1. 不。
  2. 仅在块开头指定 root server {,请查看Nginx 陷阱
  3. 我很确定这是您的评论部分nginx.conf

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;
    

您没有将它们指向 Passenger + Ruby。(nginx 在收到请求时会运行什么?什么也不运行,就是这样。)

查看此博客在 AWS 上设置 RoR + Passenger用于设置 RoR + Passenger

nginx.conf以下是工作和示例虚拟目录的副本,供您参考。

nginx.conf

user  deploy;
worker_processes  4;

error_log  logs/error.log;
events {
    worker_connections  1024;
}

http {
    server_names_hash_bucket_size 128;
    passenger_max_pool_size 200;
    rails_app_spawner_idle_time 600;
    passenger_pool_idle_time 300;
    passenger_debug_log_file /opt/nginx/logs/passenger-error.log;
    passenger_log_level 2;
    passenger_root /usr/local/rvm/gems/[email protected]/gems/passenger-3.0.19;
    passenger_ruby /usr/local/rvm/wrappers/[email protected]/ruby;
    client_max_body_size 15M;

    include       mime.types;
    log_format main     '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  20;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        error_page 403 /403.html;
        error_page 404 /404.html;
        location / {
            root   html;
            index  index.html index.htm index.php;
        }

#  Parse all .php file in the /var/www directory
    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(.*)$;
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_intercept_errors        on;
        fastcgi_ignore_client_abort     off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }   

#        Disable viewing .htaccess & .htpassword
    location ~ /.ht {
        deny  all;
    }
 }
upstream backend {
        server unix:/var/run/php5-fpm.sock;

}

虚拟的Host.conf

server {
    server_name domain.com.au;
    rewrite ^(.*) http://www.domain.com.au$1 permanent;
  }
  server  {
    listen 80;
    server_name www.domain.com.au;
    root "/home/deploy/apps/domain_com_au/current/public";
    passenger_enabled on;
    rails_env production;

    # Set custom access/error logs for awstats
    access_log  /opt/nginx/logs/domain.com.au-access.log;
    error_log  /opt/nginx/logs/domain.com.au-error.log;

    # Configure /cgi-bin/scripts to go through php-fastcgi
    location ~ ^/cgi-bin/.*.(cgi|pl|py|rb) {
        gzip off;
        fastcgi_pass  backend;
        fastcgi_index cgi-bin.php;
        fastcgi_param SCRIPT_FILENAME    /opt/nginx/cgi-bin.php;
        fastcgi_param SCRIPT_NAME        /cgi-bin/cgi-bin.php;
        fastcgi_param X_SCRIPT_FILENAME  /usr/lib$fastcgi_script_name;
        fastcgi_param X_SCRIPT_NAME      $fastcgi_script_name;
        fastcgi_param QUERY_STRING       $query_string;
        fastcgi_param REQUEST_METHOD     $request_method;
        fastcgi_param CONTENT_TYPE       $content_type;
        fastcgi_param CONTENT_LENGTH     $content_length;
        fastcgi_param GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param REQUEST_URI        $request_uri;
        fastcgi_param DOCUMENT_URI       $document_uri;
        fastcgi_param DOCUMENT_ROOT      $document_root;
        fastcgi_param SERVER_PROTOCOL    $server_protocol;
        fastcgi_param REMOTE_ADDR        $remote_addr;
        fastcgi_param REMOTE_PORT        $remote_port;
        fastcgi_param SERVER_ADDR        $server_addr;
        fastcgi_param SERVER_PORT        $server_port;
        fastcgi_param SERVER_NAME        $server_name;
        fastcgi_param REMOTE_USER        $remote_user;
    }
  }

相关内容