Nginx Varnish配置问题


0

我正在尝试使用此链接中的ssl安装varnish: https://www.linode.com/docs/websites/varnish/use-varnish-and-nginx-to-serve-wordpress-over-ssl-and-http-on-debian-8/

我做了所有的事情,如发布,但现在我得到“这个网站无法到达.example.com拒绝连接”错误。我只是缺少一点点我确定但我不知道在哪里。

这里/ etc / nginx / sites-available / default:

    server {
   listen  443 ssl;
   listen  [::]:443 ssl;
   server_name example.com  www.example.com;
   port_in_redirect off;

   ssl                  on;
   ssl_certificate      /etc/letsencrypt/live/example.com/fullchain.pem;
   ssl_certificate_key  /etc/letsencrypt/live/example.com/privkey.pem;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
   ssl_prefer_server_ciphers   on;

   ssl_session_cache   shared:SSL:20m;
   ssl_session_timeout 60m;

   add_header Strict-Transport-Security "max-age=31536000";
   add_header X-Content-Type-Options nosniff;

   location / {
     proxy_pass http://127.0.0.1:80;
     proxy_set_header Host $http_host;
     proxy_set_header X-Forwarded-Host $http_host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-Proto https;
     proxy_set_header HTTPS "on";

     access_log /var/www/html/logs/access.log;
     error_log  /var/www/html/logs/error.log notice;
     }
}

server {
   listen 8080;
   listen [::]:8080;
   server_name example.com  www.example.com;
   root /var/www/html;
   index index.php;
   port_in_redirect off;

   location / {
      try_files $uri $uri/ /index.php?$args;
   }

   location ~ \.php$ {
       try_files $uri =404;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       include fastcgi_params;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param HTTPS on;
       fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
       }
}

这是/ etc / default / varnish(我只是改变这一行):

DAEMON_OPTS="-a :80 \
            -T localhost:6082 \
            -f /etc/varnish/custom.vcl \
            -S /etc/varnish/secret \
            -s malloc,1G"

这里/etc/varnish/custom.vcl

vcl 4.0;

backend default {
    .host = "localhost";
    .port = "8080";
}

acl purger {
    "localhost";
    "142.93.244.9";
}

sub vcl_recv {
if (client.ip != "127.0.0.1" && req.http.host ~ "example.com") {
    set req.http.x-redir = "https://www.example.com" + req.url;
return(synth(850, ""));
}

if (req.method == "PURGE") {
    if (!client.ip ~ purger) {
        return(synth(405, "This IP is not allowed to send PURGE requests."));
  }
return (purge);
}

if (req.restarts == 0) {
    if (req.http.X-Forwarded-For) {
        set req.http.X-Forwarded-For = client.ip;
  }
}

if (req.http.Authorization || req.method == "POST") {
return (pass);
}

if (req.url ~ "/feed") {
return (pass);
}

if (req.url ~ "wp-admin|wp-login") {
return (pass);
}

set req.http.cookie = regsuball(req.http.cookie, "wp-settings-\d+=[^;]+(; )?", "");

set req.http.cookie = regsuball(req.http.cookie, "wp-settings-time-\d+=[^;]+(; )?", "");

if (req.http.cookie == "") {
    unset req.http.cookie;
  }
}
#end of vcl_recv

sub vcl_synth {
 if (resp.status == 850) {
     set resp.http.Location = req.http.x-redir;
     set resp.status = 302;
     return (deliver);
 }
}

sub vcl_purge {
 set req.method = "GET";
 set req.http.X-Purger = "Purged";
 return (restart);
}

sub vcl_backend_response {
set beresp.ttl = 24h;

set beresp.grace = 1h;

if (bereq.url !~ "wp-admin|wp-login|product|cart|checkout|my-account|/?remove_item=") {
    unset beresp.http.set-cookie;
    }
}

sub vcl_deliver {
    if (req.http.X-Purger) {
        set resp.http.X-Purger = req.http.X-Purger;
    }
}

/lib/systemd/system/varnish.service(我只是更改了这一行)

ExecStartPre=/usr/sbin/varnishd -C -f /etc/varnish/custom.vcl
ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/custom.vcl -S /etc/varnish/secret -s malloc,1G

谢谢你的帮助


1
你希望用“return(synth(850,”“))来实现你的目标;” ?
Gerard H. Pille

实际上我不确定,但我认为它是HTTP重定向的一部分
more

如果我没有弄错的话,850应该是一个有效的HTTP返回码,在你的情况下可能是301或302,这样浏览器就会遵循重定向。
Gerard H. Pille
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.