Nginx Proxy -> Varnish -> Nginx-> PHP -fpm 这个架构为什么无法访问?错误是 503,返回的是 Varnish 无法连接到 backend 之类的提示。

2017-09-19 21:49:40 +08:00
 Loyalsoldier

简单说: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;
  }
}
906 次点击
所在节点    NGINX
1 条回复
Loyalsoldier
2017-09-19 21:52:45 +08:00
去掉 Nginx Proxy,让 Varnish 直接监听 80 端口,是可以正常访问的,但是增加了 Nginx Proxy 之后就报错了……

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/392076

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX