V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Loyalsoldier
V2EX  ›  NGINX

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

  •  
  •   Loyalsoldier · 2017-09-19 21:49:40 +08:00 · 902 次点击
    这是一个创建于 2403 天前的主题,其中的信息可能已经有所发展或是发生改变。

    简单说: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;
      }
    }
    
    1 条回复    2017-09-19 21:52:45 +08:00
    Loyalsoldier
        1
    Loyalsoldier  
    OP
       2017-09-19 21:52:45 +08:00
    去掉 Nginx Proxy,让 Varnish 直接监听 80 端口,是可以正常访问的,但是增加了 Nginx Proxy 之后就报错了……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5015 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:40 · PVG 13:40 · LAX 22:40 · JFK 01:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.