簡體   English   中英

強制使用NGinx,Varnish和專用IP的多個網站,從非www到www

[英]Force multiple websites with NGinx, Varnish and dedicated IP's from non-www to www

我正在處理這幾天,需要幫助的人! 我希望有人在這里解決這個問題:

3個Wordpress網站 (3個專用IP)

我已經使用這個Ansible手冊來部署: https//github.com/zach-adams/hgv-deploy-full

這是fullstack:

Ubuntu 14.04(專用服務器,無防火牆)

  • Percona DB(MySQL)
  • HHVM(默認PHP解析器)
  • PHP-FPM(備份PHP解析器)
  • Nginx Varnish(默認運行)
  • Memcached和APC
  • 清潔WordPress安裝(最新版本)
  • WP-CLI

一切正常。 我檢查了標題,Varnish正在工作。 當我嘗試在每個站點中使用Varnish強制301從非www到www時,它會進入url重定向循環。 我在一些文檔中讀到了這條線的Nginx配置:

server {
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

它不起作用,我認為它適用於Varnish配置。 我按照了一些文檔但沒有對我有用:

我希望有人可以照亮我:)

為了使用Varnish從非www重定向到www,您需要執行以下操作:

1-禁用使用nginx配置編寫的任何重定向規則。

2-使用以下代碼段將非www重定向到www通過varnish:

sub vcl_recv {
    if (req.http.host == "example.com") {
        set req.http.host = "www.example.com";
        error 750 "http://" + req.http.host + req.url;
    }
}

sub vcl_error {
    if (obj.status == 750) {
        set obj.http.Location = obj.response;
        set obj.status = 301;
        return(deliver);
    }
}

注意:我今天使用了上面的代碼片段並且它對我來說正常工作(它適用於在你的問題中提到的劇本中使用的Varnish 3)

如果您想使用nginx重定向,可以試試這個:

server {
    listen 8080; # modify this with your current nginx port
    server_name  example.com;
    return 301 $scheme://www.example.com$request_uri;
}
server {
    listen 8080; # modify this with your current nginx port
    server_name www.example.com;
    root /path/to/example/files/;
    # ...the rest of your config file goes here...
}

僅使用一種方法清漆或nginx。 如果你有nginx作為前端我建議你采用nginx方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM