Rails静态图像没有显示出来

时间:2016-10-14 23:07:18

标签: ruby-on-rails amazon-web-services nginx elastic-beanstalk production-environment

我为一次又一次被问到这样一个简单的问题而道歉。

我使用Rails 4.2.3构建了一个小应用程序。一切都在本地工作所以我试图使用Elastic Beanstalk和以下设置部署到AWS:运行Ruby 2.3(Puma)的64位Amazon Linux 2016.03 v2.1.6

在部署之前,我运行:

rake assets:precompile RAILS_ENV=production

然后我将这些文件提交给git并使用eb deploy将文件推送到EC2实例。

有些事情有效:

  • 当我进入该实例时,我会看到/ var / app / current / public / assets中的所有预编译资产
  • CSS看起来都很正确
  • Coffeescripts正常运行

但是,静态图像或我通过Paperclip上传的静态图像都不会像我期望的那样出现。

在production.rb中我有这一行:

config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

我可以通过进入控制台确认密钥不在我的ENV变量中:

irb(main):001:0> ENV['RAILS_SERVE_STATIC_FILES']
=> nil

这使我相信这些文件的服务应由nginx处理。我可以确认nginx正在运行,但坦率地说,我不知道它是如何配置的。

[ec2-user@ip-172-31-13-16 assets]$ ps waux | grep nginx
root      2800  0.0  0.4 109364  4192 ?        Ss   Oct08   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     2801  0.0  0.6 109820  6672 ?        S    Oct08   0:09 nginx: worker process
ec2-user 21321  0.0  0.2 110456  2092 pts/0    S+   23:02   0:00 grep --color=auto nginx

我"思考"我应该编辑我的.ebextensions文件,以便在部署时自动执行一些操作,但这是关于我遇到的问题。有什么建议吗?

/etc/nginx/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;

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 {
#        }
#    }

}

/etc/nginx/conf.d/virtual.conf

#
# A virtual host using mix of IP-, name-, and port-based configuration
#

#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

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

/etc/nginx/conf.d/webapp_healthd.conf

upstream my_app {
  server unix:///var/run/puma/my_app.sock;
}

log_format healthd '$msec"$uri"'
                '$status"$request_time"$upstream_response_time"'
                '$http_x_forwarded_for';

server {
  listen 80;
  server_name _ localhost; # need to listen to localhost for worker tier

  if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
    set $hour $4;
  }

  access_log  /var/log/nginx/access.log  main;
  access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;

  location / {
    proxy_pass http://my_app; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location /assets {
    alias /var/app/current/public/assets;
    gzip_static on;
    gzip on;
    expires max;
    add_header Cache-Control public;
  }

  location /public {
    alias /var/app/current/public;
    gzip_static on;
    gzip on;
    expires max;
    add_header Cache-Control public;
  }
}

1 个答案:

答案 0 :(得分:2)

修复webapp_healthd.conf以使nginx能够在public文件夹中提供文件,如果不能或它们不存在,则将proxy_pass发送到您的应用:

upstream my_app {
  server unix:///var/run/puma/my_app.sock;
}

server {
  listen 80;
  server_name _; # need to listen to localhost for worker tier

  if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
    set $hour $4;
  }

  access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;

    index index.html index.htm;

    location @app { 
        log_not_found off;
        access_log off;
        proxy_pass http://my_app; # proxy passing to upstream
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_cache_bypass $http_upgrade;
    }

    root /var/app/current/public;

    location / {
        try_files $uri $uri/ @app; # tries to serve static files if not will ask @app
    }
}
相关问题