如何在nginx + varnish服务器上修复HTTP ERROR 500

时间:2017-02-20 10:42:49

标签: nginx varnish varnish-vcl

我在digitalocean Droplet上运行了2个wordpress网站。一个网站工作得非常好。

但是在另一个网站上当我登录到wordpress admin时,它工作正常。但是当我没有登录时,会发生HTTP ERROR 500

两个网站的nginx和varnish配置都是一样的。

这是NGINX虚拟主机配置:

server {

    listen 8080;

    listen 443 ssl;

    root /home/user/example.com/public_html;
    index index.html index.htm index.php;

    server_name example.com www.example.com;
    include hhvm.conf;


    ssl_certificate /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key /etc/nginx/ssl/nginx.key;

    access_log        off;  

    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?q=$uri&$args;


    }

    location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|woff|eot|ttf|svg|mp4|webm)$ {
            access_log        off;
            log_not_found     off;
        expires 1M;
        add_header Pragma public;
        add_header Cache-Control public;
        add_header Vary Accept-Encoding;
        }

}

以下是/etc/varnish/default.vcl

# Redirect to Nginx Backend if not in cache
backend default {    
    .host = "127.0.0.1";    
    .port = "8080";
}

import std;

include "lib/xforward.vcl";
include "lib/cloudflare.vcl";
include "lib/purge.vcl";
include "lib/bigfiles.vcl";        # Varnish 3.0.3+
#include "lib/bigfiles_pipe.vcl";  # Varnish 3.0.2
include "lib/static.vcl";

acl cloudflare {
    # set this ip to your Railgun IP (if applicable)
    # "1.2.3.4";
}
acl purge {
    "127.0.0.1";
}

# vcl_recv is called whenever a request is received 
sub vcl_recv {
    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.http.X-Real-IP) {         
        set req.http.X-Forwarded-For = req.http.X-Real-IP;    
    } else {        
        set req.http.X-Forwarded-For = client.ip;    
    } 
    # Serve objects up to 2 minutes past their expiry if the backend
    # is slow to respond.
        set req.grace = 120s;

        set req.backend = default;

        if (!req.http.X-Forwarded-Proto) {        
            set req.http.X-Forwarded-Proto = "http";        
            set req.http.X-Forwarded-Port = "80";        
            set req.http.X-Forwarded-Host = req.http.host;    
        } 

    # This uses the ACL action called "purge". Basically if a request to
    # PURGE the cache comes from anywhere other than localhost, ignore it.
        if (req.request == "PURGE") 
        {if (!client.ip ~ purge)
            {error 405 "Not allowed.";}
            return(lookup);}

    # Pass any requests that Varnish does not understand straight to the backend.
        if (req.request != "GET" && req.request != "HEAD" &&
                req.request != "PUT" && req.request != "POST" &&
                req.request != "TRACE" && req.request != "OPTIONS" &&
                req.request != "DELETE") 
        {return(pipe);}     /* Non-RFC2616 or CONNECT which is weird. */

    # Pass anything other than GET and HEAD directly.
        if (req.request != "GET" && req.request != "HEAD") {
            return(pass);
        }      /* We only deal with GET and HEAD by default */

    # Pass requests from logged-in users directly.
        if (req.http.Authorization || req.http.Cookie) {
            return(pass);
        }      /* Not cacheable by default */

    # Pass any requests with the "If-None-Match" header directly.
        if (req.http.If-None-Match) {
            return(pass);
        }

    # Force lookup if the request is a no-cache request from the client.
        if (req.http.Cache-Control ~ "no-cache") {
            ban_url(req.url);
        }
        return(lookup);
}

sub vcl_pipe {
# This is otherwise not necessary if you do not do any request rewriting.
    set req.http.connection = "close";
}

# Called if the cache has a copy of the page.
sub vcl_hit {
    if (req.request == "PURGE") {
        ban_url(req.url);
        error 200 "Purged";
    }

    if (!obj.ttl > 0s) {
        return(pass);
    }
}

# Called if the cache does not have a copy of the page.
sub vcl_miss {
    if (req.request == "PURGE") {
        error 200 "Not in cache";
    }
}

# Called after a document has been successfully retrieved from the backend.
sub vcl_fetch {
    # make sure grace is at least 2 minutes
    if (beresp.grace < 2m) {
        set beresp.grace = 2m;
    }

    # catch obvious reasons we can't cache
    if (beresp.http.Set-Cookie) {
        set beresp.ttl = 0s;
    }

    # Varnish determined the object was not cacheable
    if (beresp.ttl <= 0s) {
        set beresp.http.X-Cacheable = "NO:Not Cacheable";
        return(hit_for_pass);

    # You don't wish to cache content for logged in users
    } else if (req.http.Cookie ~ "wp-postpass_|wordpress_logged_in_|comment_author|PHPSESSID") {
        set beresp.http.X-Cacheable = "NO:Got Session";
        return(hit_for_pass);

    # You are respecting the Cache-Control=private header from the backend
    } else if (beresp.http.Cache-Control ~ "private") {
        set beresp.http.X-Cacheable = "NO:Cache-Control=private";
        return(hit_for_pass);

    # You are extending the lifetime of the object artificially
    } else if (beresp.ttl < 300s) {
        set beresp.ttl   = 300s;
        set beresp.grace = 300s;
        set beresp.http.X-Cacheable = "YES:Forced";

    # Varnish determined the object was cacheable
    } else {
        set beresp.http.X-Cacheable = "YES";
    }

    # Avoid caching error responses
    if (beresp.status == 404 || beresp.status >= 500) {
        set beresp.ttl   = 0s;
        set beresp.grace = 15s;
    }




    # Deliver the content
    return(deliver);
}

sub vcl_pass {
    return (pass);
}

sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    return (hash);
}

sub vcl_deliver {
    # Debug    
    remove resp.http.Via;    
    remove resp.http.X-Varnish;    

    # Add a header to indicate a cache HIT/MISS    
    #if (obj.hits > 0) {        
    #   set resp.http.X-Cache = "HIT";        
    #   set resp.http.X-Cache-Hits = obj.hits;        
    #   set resp.http.X-Age = resp.http.Age;        
    #   remove resp.http.Age;    
    #} else {        
    #   set resp.http.X-Cache = "MISS";    
    #}    

    # Remove some headers: PHP version
    unset resp.http.X-Powered-By;

    # Remove some headers: Apache version & OS
    unset resp.http.Server;

    # Remove some heanders: Varnish
    unset resp.http.Via;
    unset resp.http.X-Varnish;


    return (deliver);
}

sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    set obj.http.Retry-After = "5";
    synthetic {"
        <?xml version="1.0" encoding="utf-8"?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html>
            <head>
            <title>"} + obj.status + " " + obj.response + {"</title>
            </head>
            <body>
            <h1>Error "} + obj.status + " " + obj.response + {"</h1>
            <p>"} + obj.response + {"</p>
            <h3>Guru Meditation:</h3>
            <p>XID: "} + req.xid + {"</p>
            <hr>
            <p>Varnish cache server</p>
            </body>
            </html>
            "};
    return (deliver);
}

sub vcl_init {
    return (ok);
}

sub vcl_fini {
    return (ok);
}

我不明白是什么导致500错误。请指导我如何解决此问题。 感谢

0 个答案:

没有答案