【问题标题】:Unable to cache static contents with nginx for ruby on rails application无法使用 nginx 为 ruby​​ on rails 应用程序缓存静态内容
【发布时间】:2015-02-28 01:30:05
【问题描述】:

我在 digitalocean droplet 中使用带有 Rails 4.1 的 Ruby On Rails 应用程序。我已经安装了带有乘客的 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 缓存静态内容,请告诉我。

【问题讨论】:

    标签: ruby-on-rails caching nginx


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2011-02-03
      • 2011-04-26
      • 2020-03-06
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-24
      相关资源
      最近更新 更多