在多个站点上使用Nginx设置清漆

在多个站点上使用Nginx设置清漆,nginx,web,server,ubuntu-16.04,varnish,Nginx,Web,Server,Ubuntu 16.04,Varnish,在过去的几天里,我试图在Ubuntu 16.04上用Nginx 1.12.2设置Varnish 4.1。我阅读了文档和许多不同的来源,但我似乎不能很好地处理事情。该网站处于重定向循环中,当我使用命令varnishd-f/etc/varnish/default.vcl-d时,我收到以下错误:无法打开套接字::80:地址已在使用中 为了澄清这一点,我正在尝试设置nginx以接收HTTPS(也可以是redirec HTTP到HTTPS)并将其发送到Varnish,然后在缓存未命中时返回nginx。提前

在过去的几天里,我试图在Ubuntu 16.04上用Nginx 1.12.2设置Varnish 4.1。我阅读了文档和许多不同的来源,但我似乎不能很好地处理事情。该网站处于重定向循环中,当我使用命令varnishd-f/etc/varnish/default.vcl-d时,我收到以下错误:无法打开套接字::80:地址已在使用中

为了澄清这一点,我正在尝试设置nginx以接收HTTPS(也可以是redirec HTTP到HTTPS)并将其发送到Varnish,然后在缓存未命中时返回nginx。提前向任何能为我指明正确方向的人表示感谢

我已将我的nginx设置为如下所示(/etc/nginx/sites available/fujiorganics.com):

我的varnish配置文件如下所示(/etc/varnish/default.vcl):

这个(/etc/systemd/system/varnish.service.d/customexec.conf):

最后,此服务器块包含在与上述第一个LSID相同的文件中

server {
    listen 8080;
    listen [::]:8080;

    root /var/www/fujiorganics.com/html;
    index index.php index.html index.htm;

    server_name 127.0.0.2;
    location / {
        try_files $uri $uri/ =404;
    }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                # With php7.0-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
        #        With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }

}

我可以确认,没有varnish重定向,网站运行得非常好。

varnish正在尝试在端口8080上与Nginx通话,但Nginx在端口80上侦听,这也是varnish想要侦听的端口。
将Varnish配置为在端口80上侦听,Nginx配置为在8080上侦听,它应该可以工作。

非常感谢您的回复。是否可以将varnish改为只侦听端口8080?是的,但Nginx必须侦听另一个端口。@AriannaAondio您的配置是否可以使用https?
    vcl 4.0;

# List of upstream proxies we trust to set X-Forwarded-For correctly.
backend default {
  .host = "127.0.0.1";
  .port = "8080";
}


backend fujiorganics {
  .host = "127.0.0.2";
  .port = "8080";
}


sub vcl_recv {

 # Remove any Google Analytics based cookies
  set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
  set req.http.Cookie = regsuball(req.http.Cookie, "_ga=[^;]+(; )?", "");
  set req.http.Cookie = regsuball(req.http.Cookie, "_gat=[^;]+(; )?", "");
  set req.http.Cookie = regsuball(req.http.Cookie, "utmctr=[^;]+(; )?", "");
  set req.http.Cookie = regsuball(req.http.Cookie, "utmcmd.=[^;]+(; )?", "");
  set req.http.Cookie = regsuball(req.http.Cookie, "utmccn.=[^;]+(; )?", "");

  # Remove Optimizely Cookies
  set req.http.Cookie = regsuball(req.http.Cookie, "optim.=[^;]+(; )?", "");
  # Remove Gauges Cookies
  set req.http.Cookie = regsuball(req.http.Cookie, "_gau.=[^;]+(; )?", "");


# Remove a ";" prefix in the cookie if present
  set req.http.Cookie = regsuball(req.http.Cookie, "^;\s*", "");

  # Are there cookies left with only spaces or that are empty?
  if (req.http.cookie ~ "^\s*$") {
    unset req.http.cookie;
  }

   if (req.restarts == 0) {
    if (req.http.x-forwarded-for) {
      set req.http.X-Forwarded-For =
        req.http.X-Forwarded-For + ", " + client.ip;
      } else {
        set req.http.X-Forwarded-For = client.ip;
      }
  }

  if (req.method != "GET" &&
      req.method != "HEAD" &&
      req.method != "PUT" &&
      req.method != "POST" &&
      req.method != "TRACE" &&
      req.method != "OPTIONS" &&
      req.method != "DELETE") {
        /* Non-RFC2616 or CONNECT which is weird. */
        return (pipe);
   }
   if (req.method != "GET" && req.method != "HEAD") {
        /* We only deal with GET and HEAD by default */
      return (pass);
  }

  if ( (req.http.host ~ "^(?i)fujiorganics.com") && req.http.X-Forwarded-Proto !~ "(?i)https") {

        set req.backend_hint = fujiorganics;
        set req.http.x-redir = "https://" + req.http.host + req.url;
        return (synth(750, ""));
  }
 return (hash);
}

# handles redirecting from http to https
sub vcl_synth {
  if (resp.status == 750) {
    set resp.status = 301;
    set resp.http.Location = req.http.x-redir;
    return(deliver);
  }
}

sub vcl_backend_response {
  set beresp.ttl = 10s;
  set beresp.grace = 1h;
}

sub vcl_deliver {
  if (obj.hits > 0) { # Add debug header to see if it's a HIT/MISS and the number of hits, disable when not needed
    set resp.http.X-Cache = "HIT";
  } else {
    set resp.http.X-Cache = "MISS";
  }
}
[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -a :8080 -T localhost:6082 -f 
/etc/varnish/default.vcl -S /etc/varnish/secret -s default,1G
server {
    listen 8080;
    listen [::]:8080;

    root /var/www/fujiorganics.com/html;
    index index.php index.html index.htm;

    server_name 127.0.0.2;
    location / {
        try_files $uri $uri/ =404;
    }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                # With php7.0-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
        #        With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }

}