我们遇到了用户login会话的问题,这是由于nginxconfiguration文件的caching。 我们的应用程序在cakephp中开发。 用nginx不可能停止caching特定的文件。 请find下面的nginx conf文件,并build议用nginx处理用户login会话的最佳方法
用户nginx; worker_processes auto;
error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; 事件{worker_connections 4024; }
http {include /etc/nginx/mime.types; default_type application / octet-stream;
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 0; keepalive_timeout 650; types_hash_max_size 2048;
# 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.php;
fastcgi_cache_path / home / main_domain / public_html / nginx / cache levels = 1:2 keys_zone = my_cache:10m max_size = 10g inactive = 600m; fastcgi_cache_key“$ scheme $ request_method $ host $ request_uri”;
服务器{听80; server_name main_domain.com; 重写^ http://www.main_domain.com $ request_uri; }
服务器{听80; server_name www.main_domain.com;
# root directive should be global root /home/main_domain/public_html/; index index.php index.html;
#set $ no_cache 0;
access_log /home/main_domain/public_html/access.log; error_log /home/main_domain/public_html/error.log; location / { try_files $uri $uri/ /index.php?$1; # proxy_cache my_cache; # proxy_pass http://main_domain2.com; # proxy_cache_valid 200 302 60m; # proxy_cache_valid 404 1m; }
位置〜* \ favicon.ico $ {expires 6m; }
位置〜*。(js | css)$ {try_files $ uri /index.php?$query_string; 到期1h; add_header Pragma“public”; add_header Cache-Control“public,must-revalidate,proxy-revalidate”; }
location ~ \.php$ { try_files $uri =404;
包括/ etc / nginx / fastcgi_params; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME / home / main_domain / public_html / $ fastcgi_script_name; fastcgi_read_timeout 300; }}
服务器{听80; server_name www.subdomain1.main_domain.com;
# root directive should be global root /home/main_domain/public_html/dev/subdomains/subdomain1/app/webroot; index index.php index.html; set $no_cache 0;
if ($request_uri ~* "/users.*") { set $no_cache 1; }
access_log /home/main_domain/public_html/dev/subdomains/subdomain1/access.log; error_log /home/main_domain/public_html/dev/subdomains/subdomain1/error.log; location /blog/ { try_files $uri $uri/ /blog/index.php?$args; } location / { try_files $uri $uri/ /index.php?$1; # proxy_cache my_cache; # proxy_pass http://main_domain2.com; # proxy_cache_valid 200 302 60m; # proxy_cache_valid 404 1m; }
位置〜* \ favicon.ico $ {expires 6m; }
位置〜*。(js | css)$ {try_files $ uri /index.php?$query_string; 到期1h; add_header Pragma“public”; add_header Cache-Control“public,must-revalidate,proxy-revalidate”; }
location ~ \.php$ { try_files $uri =404; fastcgi_cache my_cache; fastcgi_cache_valid 200 60m; # Only cache 200 responses, cache for 60 minutes fastcgi_cache_methods GET HEAD; # Only GET and HEAD methods apply add_header X-Fastcgi-Cache $upstream_cache_status; fastcgi_cache_bypass $no_cache; # Don't pull from cache based on $no_cache fastcgi_no_cache $no_cache; # Don't save to cache based on $no_cache fastcgi_buffer_size 128k; fastcgi_buffers 256 4k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k;
包括/ etc / nginx / fastcgi_params; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME / home / main_domain / public_html / dev / subdomains / subdomain1 / app / webroot $ fastcgi_script_name; }}
服务器{听80; server_name subdomain2.main_domain.com; 重写^ http://www.subdomain2.main_domain.com $ 1永久; }
服务器{听80; server_name www.subdomain2.main_domain.com; 设置$ mobile_rewrite do_not_perform;
if ($http_user_agent ~* "(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino") {
设置$ mobile_rewrite执行; }
if ($mobile_rewrite = perform) { rewrite ^ http://m.main_domain.com$request_uri? redirect; break; } # root directive should be global root /home/main_domain/public_html/dev/subdomains/subdomain2/app/webroot; index index.php index.html;
#set $ no_cache 0;
access_log /home/main_domain/public_html/dev/subdomains/subdomain2/access.log; error_log /home/main_domain/public_html/dev/subdomains/subdomain2/error.log; location / { try_files $uri $uri/ /index.php?$1; # proxy_cache my_cache; # proxy_pass http://main_domain2.com; # proxy_cache_valid 200 302 60m; # proxy_cache_valid 404 1m; }
位置〜* \ favicon.ico $ {expires 6m; }
位置〜*。(js | css)$ {try_files $ uri /index.php?$query_string; 到期1h; add_header Pragma“public”; add_header Cache-Control“public,must-revalidate,proxy-revalidate”; }
位置^〜/ blog {internal; 别名/ home / main_domain / public_html / dev / subdomains / subdomain2 / app / webroot / blog; access_logclosures; log_not_found off; 到期最大; }
location ~ \.php$ { try_files $uri =404;
包括/ etc / nginx / fastcgi_params; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME / home / main_domain / public_html / dev / subdomains / subdomain2 / app / webroot $ fastcgi_script_name; }}
其实你只提供了一小段你的Nginxconfiguration,但似乎你正在混合Nginx的caching选项。 所示的proxy_no_cache选项用于Nginx的Web代理function(将请求转发给另一个Web服务器)。
关于本地托pipe的静态文件(如图片,CSS,JS …),请允许Nginx通过使用expires选项发送适当的caching标题。
关于PHP应用程序,您可以使用Nginx的FastCGIcaching。 您也许知道,Nginx会将请求转发到PHP守护进程,如PHP-FPM。 整个PHP魔法发生在这个PHP-FPM守护进程中,Nginx只会得到最终的响应,将它发送回客户端。
PHP应用程序现在可以决定在这个响应中包含caching头文件。 如果允许caching(例如公共页面),PHP可能会返回一个Cache-Control标头以允许caching。
<?php ... header('Cache-Control: public, max-age=3600'); ... ?>
如果不允许cachingPHP可能会返回一个不同的Cache-Control头来拒绝caching:
<?php ... header('Cache-Control: private, no-store, max-age=0'); ... ?>
如果您的PHP应用程序能够发送适当的caching头,则可以使用以下configuration使用Nginx FastCGIcaching:
fastcgi_cache_path /tmp/fastcgi_cache levels=1:2 keys_zone=phpfpm:64m inactive=24h max_size=512M; server { listen 80; server_name www.mysite.com; root /var/www/xyz; index index.php index.html; # check for static files and deliver, for everything else use /index.php location / { try_files $uri $uri/ /index.php; } # set caching header for static files location ~* \.(jpg|jpeg|gif|png|css|js|ico|svg|ttf)$ { access_log off; log_not_found off; expires 30d; } # set caching header for asset files, don't exec index.php just show 404 location ^~ /assets/ { try_files $uri =404; access_log off; log_not_found off; expires 30d; } # PHP handler, allow FastCGI caching location ~ \.php$ { access_log /var/log/nginx/access.log combined; include fastcgi_params; fastcgi_keep_conn on; fastcgi_pass unix:/run/php/php5.6-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_cache phpfpm; fastcgi_cache_use_stale updating error timeout invalid_header http_500; } }
如果您的PHP应用程序无法发送正确的caching标题,则可以强制caching或覆盖错误的caching标题。 注意,这可能会导致向公众发布私人数据!
结论
它应该永远是Web应用程序(这里是一个PHP应用程序)定义哪个页面caching,哪些不是。 使用networking服务器(Nginx,Apache或其他)来做出这个决定就像使用防撞屏障从外面控制汽车。 它工作,但使用方向盘更聪明。