如何使用 Nginx 和 Passenger 设置代理缓存

如何使用 Nginx 和 Passenger 设置代理缓存

我使用 Nginx 和 Passenger 作为我的 rails 应用程序。我想使用代理缓存来缓存我的页面。但是,每个请求都直接发送到我的 rails 应用程序。我不知道我的配置有什么问题。以下是我的配置:

user www-data;
worker_processes  1;


events {
    worker_connections  1024;
}

http {
    passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-2.2.15;
    passenger_ruby /usr/bin/ruby1.8;
    passenger_max_pool_size 6;
    passenger_max_instances_per_app 1;
    passenger_pool_idle_time 0;
    rails_spawn_method conservative;
    include       mime.types;
    default_type  application/octet-stream;

    server_names_hash_bucket_size 512;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_http_version 1.0;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types text/plain text/css text/javascript application/javascript    application/json application/x-javascript text/xml application/xml application/xml+rss;

    proxy_cache_path  /var/www/cache/webapp levels=1:2 keys_zone=webapp:8m max_size=1000m inactive=600m;
    include vhosts/*.conf;

    include /opt/nginx/conf/sites-enabled/*;

    root    /var/www;
}


server {
    listen 127.0.0.1:3008;
    server_name localhost;
    root /var/www/yoolk_web_app/public;   # <--- be sure to point to 'public'!
    passenger_enabled on;
    rails_env development;
    passenger_use_global_queue on;

}

server {
    listen 80;
    server_name webpage.dev;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host       $host;

    error_page 503 http://$host/maintenance.html;

    location ~* (css|js|png|jpe?g|gif|ico)$ {
    root /var/www/web_app/public;
    expires       max;
}

location / {
    proxy_pass http://127.0.0.1:3008/;
    proxy_cache webapp;
    proxy_cache_valid  200 10m;
}

#More Location

}

答案1

尝试确定正确乘客生成方法nginx 配置文件中的选项可以缓存任何应用程序代码。

答案2

将以下指令添加到您的location /部分:

proxy_cache_lock on;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_ignore_headers Expires Cache-Control;
proxy_cache_revalidate off;

无论用户请求如何,他们都应该强制使用缓存

相关内容