Linux

清漆,隱藏埠號

  • June 27, 2012

我的設置如下:

**作業系統:**在 OpenVZ 虛擬機上執行的 CentOS 6.2。

Web 伺服器: Nginx 監聽 8080 埠

反向代理: varnish 監聽 80 埠

問題是 Varnish 將我的請求重定向到埠 8080,這會出現在地址欄中,像這樣http://mysite.com:8080/directory/,導致站點上的相關連結在請求中包含埠號 (8080),從而繞過 Varnish。

該網站由 WordPress 提供支持。

如何允許 Varnish 使用 Nginx 作為埠 8080 上的後端而不將埠號附加到地址?

**編輯:**清漆設置如下:

我已經告訴 Varnish 守護程序預設監聽 80 埠。

VARNISH_VCL_CONF=/etc/varnish/default.vcl
#
# # Default address and port to bind to
# # Blank address means all IPv4 and IPv6 interfaces, otherwise specify
# # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=
VARNISH_LISTEN_PORT=80
#
# # Telnet admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
#
# # Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret
#
# # The minimum number of worker threads to start
VARNISH_MIN_THREADS=1
#
# # The Maximum number of worker threads to start
VARNISH_MAX_THREADS=1000
#
# # Idle timeout for worker threads
VARNISH_THREAD_TIMEOUT=120
#
# # Cache file location
VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
#
# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
VARNISH_STORAGE_SIZE=1G
#
# # Backend storage specification
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
#
# # Default TTL used when the backend does not specify one
VARNISH_TTL=120

Varnish 呼叫的 VCL 文件(通過 include in default.vcl)包括:

backend playwithbits {
    .host = "127.0.0.1";
    .port = "8080";
}
acl purge {
    "127.0.0.1";
}
sub vcl_recv {
    if (req.http.Host ~ "^(.*\.)?playwithbits\.com$") {
         set req.backend = playwithbits;
         set req.http.Host = regsub(req.http.Host, ":[0-9]+", ""); 
         if (req.request == "PURGE") {
              if (!client.ip ~ purge) {
                   error 405 "Not allowed.";
              }
              return(lookup);
         }
         if (req.url ~ "^/$") {
              unset req.http.cookie;
         }
    }
}
sub vcl_hit {
    if (req.http.Host ~ "^(.*\.)?playwithbits\.com$") {
         if (req.request == "PURGE") {
              set obj.ttl = 0s;
                   error 200 "Purged.";
         }
    }
}
sub vcl_miss {
    if (req.http.Host ~ "^(.*\.)?playwithbits\.com$") {
         if (req.request == "PURGE") {
              error 404 "Not in cache.";
         } 
         if (!(req.url ~ "wp-(login|admin)")) {
              unset req.http.cookie;
         }
         if (req.url ~ "^/[^?]+.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.|)$") {
              unset req.http.cookie;
              set req.url = regsub(req.url, "\?.$", "");
         }
         if (req.url ~ "^/$") {
              unset req.http.cookie;
         }
    }
}
sub vcl_fetch {
    if (req.http.Host ~ "^(.*\.)?playwithbits\.com$") {
         if (req.url ~ "^/$") {
              unset beresp.http.set-cookie;
         }
         if (!(req.url ~ "wp-(login|admin)")) {
              unset beresp.http.set-cookie;
         }
    }
}

所以我設法解決了這個問題,向任何試圖找到答案的人道歉,因為這不是我診斷的問題。

在我應用清漆之前,我告訴 wordpress 我的網站 url 是playwithbits.com:8080為了正確地提供內容,所以它將埠號附加到我的所有 URL 上。

我完全忘記了這個設置,一旦我刪除了埠號,我的網站就開始正常工作了。

Varnish 本身從不發送 301 答案(除非你做了一些主要的 VCL 魔法),所以如果你得到一個重定向,它必須來自你的後端。

引用自:https://serverfault.com/questions/398936