使用弹性 beantalk 部署的 Rails 应用程序响应“无法访问站点”

Posted

技术标签:

【中文标题】使用弹性 beantalk 部署的 Rails 应用程序响应“无法访问站点”【英文标题】:Rails app deployed using elastic beanstalk responds with "site can't be reached" 【发布时间】:2017-06-01 23:03:30 【问题描述】:

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

    Puma(应用服务器)3.0 Ruby on Rails 5.0 Postgresql 9.5.3

我尝试将此应用程序部署到 aws elastic beanstalk 并成功完成。但是,当我尝试访问 beanstalk 生成的网址时,应用程序服务器上没有任何命中。几分钟后浏览器抛出“无法访问站点”。

这里是必要的日志:

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 仍保持原样。我无法在任何日志中发现任何错误。

进一步参考:

这是我的其他几个文件:

config/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

宝石文件

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和***的研究之后,我尝试了一些东西:

我将以下内容附加到我的 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:

我在朋友的笔记本电脑上打开了网址。它打开了。看来问题出在我的系统代理上。现在我可以删除我的系统代理,但我不能确定我的用户会访问我的网站。有没有办法将弹性豆茎与系统代理一起使用?同时,我也会在互联网上爱抚。

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

编辑 4: 我从浏览器中清除了缓存,它起作用了!吸取的教训。

【问题讨论】:

您是否正确设置了您的 ngnix.conf?如果你也可以发布你的 nginx.conf 文件? @sahil 请看一下..我已经添加了 nginx 配置文件。 root 应该指向 /path/to/code_folder/public。这是一个template,我将它用于在 Pushion 乘客、Nginx 和 Rails 上运行的多个应用程序。 将此config.force_ssl = true 更改为false 解决了这个问题。我花了几天时间试图弄清楚这一点 【参考方案1】:

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

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

解决了我的问题。

【讨论】:

以上是关于使用弹性 beantalk 部署的 Rails 应用程序响应“无法访问站点”的主要内容,如果未能解决你的问题,请参考以下文章

在弹性 beantalk 上部署 rails 应用程序时出错(生产模式)

升级弹性 beantalk 堆栈时,Rails 应用程序无法部署

我的 ec2 实例中乘客日志文件的位置(弹性 beantalk + rails)

尝试通过弹性 beantalk 运行救援时忽略 .ebextensions 文件

在 AWS 弹性 beanstalk 上部署 Rails - 静态资产路由不起作用

应该如何使用 Amazon Web 服务及其弹性 beantalk 将机密文件推送到 EC2 Ruby on Rails 应用程序?