使用弹性beanstalk部署的Rails应用程序响应"站点无法到达"

时间:2017-01-17 18:44:54

标签: ruby-on-rails ruby nginx elastic-beanstalk puma

我已经使用以下堆栈构建了一个rails应用程序:

  1. Puma(应用程序服务器)3.0
  2. Ruby on Rails 5.0
  3. Postgresql 9.5.3
  4. 我尝试将此应用程序部署到aws弹性beanstalk并成功完成。但是,当我尝试访问beanstalk生成的Web地址时,应用程序服务器上没有命中。几分钟后,浏览器会抛出"网站无法到达"。

    以下是必要的日志:

    puma.log

    === puma startup: 2017-01-17 18:10:06 +0000 ===
    === puma startup: 2017-01-17 18:10:06 +0000 ===
    [1501] - Worker 0 (pid: 1504) booted, phase: 0
    

    /var/log/nginx/error.log

    2017/01/17 18:03:08 [warn] 703#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
    2017/01/17 18:25:00 [warn] 2043#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
    2017/01/17 18:25:00 [warn] 2050#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
    

    /var/app/containerfiles/logs/production.log

    D, [2017-01-17T18:10:02.564332 #1397] DEBUG -- :    (1.0ms)  SELECT pg_try_advisory_lock(6778635641374256100);
    D, [2017-01-17T18:10:02.574041 #1397] DEBUG -- :   ActiveRecord::SchemaMigration Load (1.4ms)  SELECT "schema_migrations".* FROM "schema_migrations"
    D, [2017-01-17T18:10:02.591019 #1397] DEBUG -- :   ActiveRecord::InternalMetadata Load (1.7ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", :environment], ["LIMIT", 1]]
    D, [2017-01-17T18:10:02.596200 #1397] DEBUG -- :    (1.0ms)  BEGIN
    D, [2017-01-17T18:10:02.598152 #1397] DEBUG -- :    (1.0ms)  COMMIT
    D, [2017-01-17T18:10:02.599249 #1397] DEBUG -- :    (0.9ms)  SELECT pg_advisory_unlock(6778635641374256100)
    

    尽管有多次重新加载,但这个production.log保持不变。我无法在任何日志中收到任何错误。

    供进一步参考:

    以下是我的其他一些文件:

    配置/ puma.rb

    workers Integer(ENV['WEB_CONCURRENCY'] || 2)
    threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
    threads threads_count, threads_count
    
    preload_app!
    
    rackup      DefaultRackup
    port        ENV['PORT']     || 3000
    environment ENV['RACK_ENV'] || 'development'
    
    on_worker_boot do
      # Worker specific setup for Rails 4.1+
      # See: https://devcenter.heroku.com/articles/
      # deploying-rails-applications-with-the-puma-web-server#on-worker-boot
      ActiveRecord::Base.establish_connection
    end
    

    的Gemfile

    source 'https://rubygems.org'
    
    # Ruby gem
     ruby '2.3.1'
    # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
    gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
    # Gemfile for password hashing
    gem 'bcrypt', '3.1.11'
    # Use postgresql as the database for Active Record
    gem 'pg', '~> 0.18'
    # Use Puma as the app server
    gem 'puma', '~> 3.0'
    # Use SCSS for stylesheets
    gem 'sass-rails', '~> 5.0'
    # Use Uglifier as compressor for JavaScript assets
    gem 'uglifier', '>= 1.3.0'
    # Use CoffeeScript for .coffee assets and views
    gem 'coffee-rails', '~> 4.2'
    # See https://github.com/rails/execjs#readme for more supported runtimes
    # gem 'therubyracer', platforms: :ruby
    gem 'jquery-ui-rails', '~> 5.0', '>= 5.0.5'
    # Use jquery as the JavaScript library
    gem 'jquery-rails'
    # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
    gem 'turbolinks', '~> 5'
    # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
    gem 'jbuilder', '~> 2.5'
    # Use Redis adapter to run Action Cable in production
    # gem 'redis', '~> 3.0'
    # Use ActiveModel has_secure_password
    # gem 'bcrypt', '~> 3.1.7'
    gem 'select2-rails'
    # Use Capistrano for deployment
    # gem 'capistrano-rails', group: :development
    # Upload files
    gem 'carrierwave', '>= 1.0.0.rc', '< 2.0'
    # Image resizing
    gem 'mini_magick', '~> 4.5', '>= 4.5.1'
    # Image upload in production
    gem 'fog-aws'
    # Autoprefix CSS
    gem "autoprefixer-rails"
    # Google Maps API
    gem 'geocoder'
    # Redis
    gem 'redis', '~> 3.3', '>= 3.3.1'
    # For postgres full text search
    gem 'pg_search'
    # Font awesome support
    gem "font-awesome-rails"
    # Google oauth-2
    gem "omniauth-google-oauth2"
    # Facebook omniauth
    gem 'omniauth-facebook'
    
    group :production do
      gem 'remote_syslog_logger'
    end
    
    group :development, :test do
      # Call 'byebug' anywhere in the code to stop execution and get a debugger console
      gem 'byebug', platform: :mri
      gem 'better_errors', '~> 2.1', '>= 2.1.1'
    end
    
    group :development do
      # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
      gem 'web-console'
      gem 'listen', '~> 3.0.5'
      # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
      gem 'spring'
      gem 'spring-watcher-listen', '~> 2.0.0'
      gem 'rails_layout'
      gem 'seed_dump', '~> 3.2', '>= 3.2.4'
      gem 'rubocop', '~> 0.44.1', require: false
      gem 'binding_of_caller', '~> 0.7.2'
      gem 'meta_request'
      gem 'rack-mini-profiler', '~> 0.10.1'
      gem 'brakeman', :require => false
      gem 'bullet'
    end
    
    group :test do
      gem 'rails-controller-testing', '0.1.1'
      gem 'minitest-reporters',       '1.1.9'
      gem 'guard',                    '2.13.0'
      gem 'guard-minitest',           '2.4.4'
    end
    
    # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
    gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
    

    除此之外,我在google和stackoverflow上进行了个人研究之后尝试了一些事情:

    我将以下内容添加到我的puma配置文件( puma.rb )中:

    bind "unix:///var/run/puma/my_app.sock"
    pidfile "/var/run/puma/my_app.sock"
    

    我甚至尝试更改我的控制器以获取根路径以呈现简单的问候语,而不是尝试从数据库中获取数据。但主要问题似乎是请求无法到达应用程序,因为 production.log 中的零日志条目显而易见。

    我还尝试删除 Gemfile 中提供的puma的版本号,希望更新的版本可以解决此问题。

    我还尝试多次重启nginx服务器以及应用服务器。但似乎没有什么能够引发有意义的错误。

    为了确保它不是数据库问题,我将ssh-ed放入eb cli,并使用rails控制台。控制台运行得很好。所以,这不是数据库问题。

    如果有人可以帮助我,我真的很感激,至少在日志中给我一个有意义的错误。

    编辑1: 正如Sahil所说,nginx.conf文件是:

    # For more information on configuration, see:
    #   * Official English Documentation: http://nginx.org/en/docs/
    #   * Official Russian Documentation: http://nginx.org/ru/docs/
    
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /var/run/nginx.pid;
    
    # Load dynamic modules. See /usr/share/nginx/README.fedora.
    include /usr/share/nginx/modules/*.conf;
    
    events {
        worker_connections 1024;
    }
    
    http {
        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  /var/log/nginx/access.log  main;
    
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    
        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # See http://nginx.org/en/docs/ngx_core_module.html#include
        # for more information.
        include /etc/nginx/conf.d/*.conf;
    
        index   index.html index.htm;
    
        server {
            listen       80 ;
            listen       [::]:80 ;
            server_name  localhost;
            root         /usr/share/nginx/html;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
            location / {
            }
    
            # redirect server error pages to the static page /40x.html
            #
            error_page 404 /404.html;
                location = /40x.html {
            }
    
            # redirect server error pages to the static page /50x.html
            #
            error_page 500 502 503 504 /50x.html;
                location = /50x.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;
            #}
        }
    
    # Settings for a TLS enabled server.
    #
    #    server {
    #        listen       443 ssl;
    #        listen       [::]:443 ssl;
    #        server_name  localhost;
    #        root         /usr/share/nginx/html;
    #
    #        ssl_certificate "/etc/pki/nginx/server.crt";
    #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
    #        # It is *strongly* recommended to generate unique DH parameters
    #        # Generate them with: openssl dhparam -out /etc/pki/nginx/dhparams.pem 2048
    #        #ssl_dhparam "/etc/pki/nginx/dhparams.pem";
    #        ssl_session_cache shared:SSL:1m;
    #        ssl_session_timeout  10m;
    #        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #        ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
    #        ssl_prefer_server_ciphers on;
    #
    #        # Load configuration files for the default server block.
    #        include /etc/nginx/default.d/*.conf;
    #
    #        location / {
    #        }
    #
    #        error_page 404 /404.html;
    #            location = /40x.html {
    #        }
    #
    #        error_page 500 502 503 504 /50x.html;
    #            location = /50x.html {
    #        }
    
    
    }
    

    除此之外,在我的production.rb文件中:

    config.force_ssl = true
    

    编辑2:

    我在朋友的笔记本电脑上打开了网址。它打开了。似乎问题出在我的系统代理上。现在我可以删除我的系统代理,但我无法确定将访问我的网站的用户。有弹性beanstalk与系统代理一起使用的方法吗?与此同时,我也会因为互联网而烦恼。

    编辑3: 我尝试在我的笔记本电脑中从safari打开网站。有用。但它不适用于谷歌浏览器。

    编辑4: 我清除了浏览器中的缓存并且有效!吸取的教训。

1 个答案:

答案 0 :(得分:0)

将Rails应用程序部署到弹性beantalk后,我收到504网关超时错误。在带有内容的应用程序的根目录中添加名为“ Procfile”(无扩展名)的文件

web: bundle exec puma -C /opt/elasticbeanstalk/config/private/pumaconf.rb

解决了我的问题。