无法使用nginx为rails应用程序上的ruby缓存静态内容

时间:2015-01-01 10:23:02

标签: ruby-on-rails caching nginx

我正在使用Ruby On Rails应用程序和Rails 4.1在digitalocean droplet中。我已经安装了nginx和乘客,我的应用程序在这里完美运行。

现在我想用nginx缓存静态内容,我已经为nginx.conf文件配置了以下内容。我不知道这里有什么问题。请给我建议同样的工作。

我的nginx.conf文件:

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

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    log_format cache '***$time_local '
                     '$upstream_cache_status '
                     'Cache-Control: $upstream_http_cache_control '
                     'Expires: $upstream_http_expires '
                     '"$request" ($status) '
                     '"$http_user_agent" ';

    access_log  /var/log/nginx/cache.log cache;

    proxy_cache_path /data/nginx/cache keys_zone=one:10m levels=1:2 loader_threshold=300 loader_files=200 max_size=200m;
    proxy_temp_path /data/nginx/cache/tmp;

    server {
        listen 80;
        proxy_cache one;

        location / {
            proxy_set_header    X-Real-IP         $remote_addr;
            proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto http;
            proxy_set_header    Host              $http_host;
            proxy_redirect      off;
            proxy_pass http://api.xyz.com;
        }

        location ~ ^/(stylesheets|javascripts|images|system/avatars) {
            expires 720h;
        }
    }

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

    # set client body size to 20M #
    client_max_body_size 20M;

    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;

    ##
    # Phusion Passenger config
    ##
    # Uncomment it if you installed passenger or passenger-enterprise
    ##

    passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
    passenger_ruby /usr/local/bin/ruby;
    ##
    # Virtual Host Configs
    ##

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

如果您需要使用nginx缓存静态内容,请告诉我。

1 个答案:

答案 0 :(得分:1)

自Rails 3.1以来,它在生产环境中使用资产预编译。我在nginx中使用它作为我的rails应用程序:

location ~ ^/assets/ {
        expires 1y;
        add_header Cache-Control public;
        add_header ETag "";
        break;
}

在此处阅读:http://guides.rubyonrails.org/v4.1.8/asset_pipeline.html