nginx –通过不同规则缓存特定PHP页面的正确配置?

时间:2018-10-20 20:07:32

标签: nginx nginx-location nginx-config

我在服务器配置和相关方面还是一个新手,但是我有一项任务是针对大量并发用户优化我们的服务,现在尝试设置适当的页面缓存。

site.com/target目录中有两个PHP页面,每个页面均应通过不同的规则进行缓存:

    当存在两个定义的查询参数( foo bar )时,应缓存
  • index.php。例如,将保存site.com/target? foo = 1 &qux = 2& bar = 3 ,两个site.com/target?foo=1 bar = 3 和site.com/target? foo = 1 &baz = 100&qux = 5& bar = 3 将返回保存的副本,但具有至少一个具有不同值的目标参数的请求,例如site.com/target?foo=2 bar = 3 将使其成为自己的缓存副本;

  • style.php应该在考虑整体查询字符串的情况下进行缓存,site.com / target?foo = 1和site.com/target?foo=2都将自己的副本保存在缓存中。

所有其他PHP文件应保持不变。目前没有任何登录/身份验证,因此,它不应对Cookie做出反应。 site.com的public_html主目录已安装WordPress,但整个目标文件夹均未引用它。

到目前为止,这是我的配置,在服务器指令中的位置指令中定义了这两个规则:

# Server globals
user                    www-data;
worker_processes        2;
error_log               /var/log/nginx/error.log;
pid                     /var/run/nginx.pid;


# Worker config
events {
    worker_connections  1024;
    use                 epoll;
}


http {
# Main settings
sendfile                        on;
tcp_nopush                      on;
tcp_nodelay                     on;
client_header_timeout           1m;
client_body_timeout             1m;
client_header_buffer_size       2k;
client_body_buffer_size         256k;
client_max_body_size            256m;
large_client_header_buffers     4   8k;
send_timeout                    30;
keepalive_timeout               60 60;
reset_timedout_connection       on;
server_tokens                   off;
server_name_in_redirect         off;
server_names_hash_max_size      512;
server_names_hash_bucket_size   512;


# Log format
log_format  main    '$remote_addr - $remote_user [$time_local] $request '
                    '"$status" $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
log_format  bytes   '$body_bytes_sent';
access_log          /var/log/nginx/access.log  main;
access_log off;
log_format rt_cache '$remote_addr - $upstream_cache_status [$time_local]  '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';
access_log   /var/log/nginx/cache.log rt_cache;

# Mime settings
include             /etc/nginx/mime.types;
default_type        application/octet-stream;


# Compression
gzip                on;
gzip_comp_level     9;
gzip_min_length     512;
gzip_buffers        8 64k;
gzip_types          text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml application/x-font-ttf font/opentype;
gzip_proxied        any;


# Proxy settings
proxy_redirect      off;
proxy_set_header    Host            $host;
proxy_set_header    X-Real-IP       $remote_addr;
proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_header   Set-Cookie;
proxy_connect_timeout   90;
proxy_send_timeout  90;
proxy_read_timeout  90;
proxy_buffers       32 4k;

# SSL PCI Compliance
ssl_session_cache   shared:SSL:10m;
ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers        "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
ssl_dhparam         /etc/nginx/dhparams.pem;


# Error pages
error_page          403          /error/403.html;
error_page          404          /error/404.html;
error_page          502 503 504  /error/50x.html;


# Cache
proxy_cache_path /var/cache/nginx levels=2 keys_zone=cache:5m inactive=1m max_size=512m;
proxy_cache_key "$host$request_uri $cookie_user";
proxy_temp_path  /var/cache/nginx/temp;
proxy_ignore_headers Expires Cache-Control;
proxy_cache_use_stale error timeout invalid_header http_502;
proxy_cache_valid any 3d;
proxy_cache_bypass $http_cache_bypass;

map $http_cookie $no_cache {
    default 0;
    ~SESS 1;
    ~wordpress_logged_in 1;
}


# Wildcard include
include             /etc/nginx/conf.d/*.conf;

  server {
    location ~*site.com/public_html/target/index.php?.*foo=[0-9]*.*bar=[0-9]*.* {
      proxy_cache_key "$host$request_uri$args";
      proxy_cache cache;
      proxy_cache_valid 5m;
      proxy_cache_valid 404 1m;
      add_header Proxy-Cache $upstream_cache_status;
    }
    location ~*site.com/public_html/target/style.php?.* {
      proxy_cache_key "$host$request_uri$args";
      proxy_cache cache;
      proxy_cache_valid 5m;
      proxy_cache_valid 404 1m;
      add_header Proxy-Cache $upstream_cache_status;
    }
  }
}

当然,这似乎不起作用–指示页面的负载测试仍然显示出请求数量和响应时间之间的直接依赖关系,就像根本没有缓存一样。

我想知道我的配置出了什么问题,如果可能的话,我想知道我的配置可以正常工作。请让我知道是否可以详细介绍我们的服务器配置。预先感谢!

0 个答案:

没有答案