2013-11-26 60 views
3

我有3臺計算機在同一網絡(LAN)。我想將一臺計算機配置爲Nginx Web-Server,另一臺計算機配置爲Varnish Cache服務器和一個客戶端。我成功安裝了一個(比方說A)Nginx(192.168.0.15)和B Varnish(192.168.0.20)。我將A配置爲網絡服務器,並且可以從其他計算機瀏覽index.html。但我不能連接它與B. 我搞砸了「nginx.conf」和「/sites-available/server.com」和Varnish的「default.vcl」簡單配置Nginx和Varnish

你能給我的基本配置適合我的環境?

如果你想看看我的 nginx.conf:

http { 
    include  /etc/nginx/mime.types; 
    default_type application/octet-stream; 

    log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
          '$status $body_bytes_sent "$http_referer" ' 
          '"$http_user_agent" "$http_x_forwarded_for"'; 

    access_log /var/log/nginx/access.log main; 

    sendfile  on; 

    keepalive_timeout 65; 

    #gzip on; 
    include /etc/nginx/conf.d/*.conf; 

    upstream dynamic_node { 
      server 1.1.1.1:80; # 1.1.1.1 is the IP of the Dynamic Node 
     } 
    server { 
     listen  81; 
     server_name myserver.myhome.com; 
     location/{ 
     #root /var/www/server.com/public_html; 
     #index index.html index.htm; 

     # pass the request on to Varnish 
     proxy_pass http://192.168.0.20; 

     # Pass a bunch of headers to the downstream server. 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

     proxy_redirect  off; 
     } 
    } 
} 

/sites-available/server.com:

服務器{

listen 80; 
server_name myserver.myhome.com; 

access_log /var/www/server.com/access.log; 
error_log /var/www/server.com/error.log; 

}

和default.vcl像這樣:

backend web1 { 
    .host = "192.168.0.15"; 
    .port = "8080"; 
} 
sub vcl_recv { 
    if (req.http.host == "192.168.0.15") { 

     #set req.http.host = "myserver.myhome.com"; 
     set req.backend = web1; 
    } 
} 

最後的/ etc /默認/清漆:

DAEMON_OPTS="-a :6081 \ 
      -T localhost:6082 \ 
      -f /etc/varnish/default.vcl \ 
      -S /etc/varnish/secret \ 
      -s malloc,256m" 

感謝提前:)

+0

你可以在後端使用nginx upstream - 這是清漆。確保IP地址和端口匹配 –

+0

proxy_pass應該工作提供清漆指向該IP地址/端口 –

+0

設置所有端口= 81(DAEMON_OPTS除外),我得到502在本地主機壞網關:81 – TheSoulkiller

回答

3

現在,您的清漆實例偵聽端口6081.這需要在proxy_pass被指定對於nginx例如

proxy_pass http://192.168.0.20:6081 

我假設你提到的IP地址是正確的,並且計算機之間的網絡連接不受限制。

更新

請記住,你可以在清漆或周圍的其他方式的前使用nginx的。 nginx和varnish都可以作爲後臺服務的代理。
您當前的實現是使用nginx作爲代理。這意味着你可以依賴proxy_pass或者在nginx中使用上游模塊(如果你希望在前面加入一個nginx來實現多個varnish實例的負載均衡)。本質上,無論哪個代理,代理中指定的後端(您的情況下爲nginx)的IP地址和端口號必須與後端服務的IP地址和端口號(您的案例中的varnish)匹配。 varnish中的後端將需要匹配您正在使用的任何應用程序服務器/服務(tomcat/netty/django/ror等)的IP地址和端口號。

+0

工作表示感謝。 – TheSoulkiller

+0

但是* backend web1 { .host =「192.168.0.15」; .port =「8080」; } *配置是否正確? – TheSoulkiller

+0

我正在更新答案以回答您的問題 –