我的网站有 nginx php-fpm 服务器。我想为 nginx 使用 microcache。一开始一切都很好。我用 curl 命令时遇到问题。当我尝试登录时,问题就开始了。我试了所有办法,但还是无法解决登录问题。
我将“logged_in”cookie 设置为 10 秒,并在“缓存配置”中为该 cookie 设置了“no-cache”。它应该在有该 cookie 时绕过缓存。我已“设置”no-cache 设置,这是我的登录名。此外,我的网站有 exmple.org/?i=login,所以我不知道当我单击登录时会发生什么 :D。
主页面是可缓存的,但登录时返回未登录主页,刷新后我成为登录用户。对于注销,它会将我注销,但刷新后我仍然是登录用户。所以我不知道如何修复/绕过登录过程。
请帮帮我。
服务器配置:
fastcgi_cache_path /usr/share/nginx/cache/fcgi levels=1:2 keys_zone=microcache:32m max_size=1024m inactive=3h;
fastcgi_cache_key $scheme$host$request_uri$request_method;
fastcgi_cache_use_stale updating error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
add_header X-Cache $upstream_cache_status;
server {
listen ip:80;
server_name example.org;
return 301 $scheme://www.example.org$request_uri;
}
server {
server_name www.example.org;
listen ip:80;
root /home/example/public_html;
index index.html index.htm index.php;
access_log /var/log/virtualmin/example.org_access_log;
error_log /var/log/virtualmin/example.org_error_log;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include /etc/nginx/example.d/cache.conf;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_cache microcache;
fastcgi_cache_key $scheme$host$request_uri$request_method;
fastcgi_cache_valid 200 301 302 30s;
#fastcgi_pass_header Set-Cookie;
#fastcgi_pass_header Cookie;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
fastcgi_pass unix:/run/php/php5.6-fpm_example.sock;
fastcgi_index index.php;
include /etc/nginx/example.d/fastcgi.conf;
}
location ~* \.(jpg|jpeg|gif|css|png|js|woff|ttf|svg|ico|eot)$ {
access_log off;
log_not_found off;
expires max;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
include /etc/nginx/example.d/redirect.conf;
include /etc/nginx/example.d/rewrite.conf;
}
缓存配置(包含在服务器配置中):
#Cache everything by default
set $no_cache 0;
#Don't cache POST requests
if ($request_method = POST)
{
set $no_cache 1;
}
#Don't cache if the URL contains a query string
if ($query_string != "")
{
set $no_cache 1;
}
#Don't cache the following URLs
if ($request_uri ~* "/*login*|/*ajax*|/sistem/modul/login.php")
{
set $no_cache 1;
}
#Don't cache if there is a cookie called PHPSESSID
if ($http_cookie = "Logged_in")
{
set $no_cache 1;
}
编辑:实际上,经过一些检查后,我很确定我的问题只是出在 phpsessid 上。每个连接都有 phpsessid,nginx 也会缓存它们,或者通过指令完全忽略它们。如果我缓存 phpsesid,并且如果首先使用管理员帐户使用浏览器登录,那么每个人都会获得管理员登录缓存 :D 我需要一个应该保护的连接 phpsessid。
就像 nginx 应该首先清除 phpsessid cookie 并将 php 发送到 fastcgi。并且从 fastcgi 服务器返回该 php 时,nginx 应该附加最初清除的相同 phpsessid。或者就像缓存 php 或没有 phpsessid cookie 的所有内容,当从缓存中提供内容时,nginx 应该将未更改的官方 phpsessid 附加到它们。
这样,每个访问者都会拥有唯一的 phpsessid 并具有缓存内容,这将解决我的登录问题。
我可能可以清除并设置 phpsessid。但我不知道如何保存该唯一/特定的 phpsessid 并重新设置它。或者可能根本不可能。我想到了理论,但不知道该怎么做 :D
答案1
也许你应该采取简单的方法,只需为你的登录 URL 定义一个块并手动关闭缓存。这就是我在 Wordpress 上所做的。
我在服务器定义之外有这个速率限制块(我有一个日志流量网站,所以每秒 1 次登录就足够了)。
limit_req_zone $binary_remote_addr zone=login:1m rate=1r/s;
这是我的服务器内部
# Rate limit wp-login.php to prevent brute force attacks
location = /wp-login.php {
# Next line applies the rate limit defined above
limit_req zone=login burst=3;
fastcgi_keep_conn on;
fastcgi_intercept_errors on;
fastcgi_pass php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# No caching
more_clear_headers "Cache-Control";
add_header Cache-Control "private, max-age=0, no-cache, no-store";
more_clear_headers "Expires";
}
更多内容请参阅我的 Wordpress/Nginx 教程,这里。
更新
这是我在 Wordpress 的 Nginx 配置中的内容
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wordpress_logged_in") {
set $skip_cache 1;
}
请阅读我上面链接的教程,我有一个相当有效的 Wordpress Nginx 配置,您可以按原样使用或从中复制粘贴部分内容。