简单说:Nginx Proxy 监听 80 端口,将请求全部转发给 Varnish(8080 端口),Varnish 进行处理后,再将请求转发给 Nginx(8081 端口),接着遇到 php 文件就交给 PHP-fpm ( 9000 端口)。
问题应该是出在 Nginx Proxy 和 Varnish 以及 Nginx 之间。
注释:在 Varnish 前面再加一层 Nginx Proxy,是因为每天晚上都要进行网页 302 跳转,Nginx 控制跳转只需要 return 302 状态码和一个 URL 就好了,而 Varnish 不好控制页面跳转(因为把 PHP 请求都缓存了 10 分钟,PHP 请求在刚设置跳转时的 10 分钟内无法到达 PHP-fpm ),而又不想因为跳转而关掉 Varnish 导致后端压力过大。
Nginx Proxy 的配置:
upstream varnish { server 127.0.0.1:8080 weight=5; } server { listen 80; server_name example.com server_tokens off; root /www/path/to/the/root/folder; location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://varnish; } } Varnish 配置文件 default.vcl:
vcl 4.0; backend default { .host = "127.0.0.1"; .port = "8081"; } Varnish 配置文件 varnish.params:
VARNISH_LISTEN_PORT=8080 Nginx 配置:
server { listen 8081; server_name example.com server_tokens off; root /www/path/to/the/root/folder; location / { index index.php index.html; try_files $uri $uri/ =404; } location ~* \.php$ { try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 