
我可以使用什麼樣的設定在同一台伺服器上託管 wordpress/php 和 node.js?我有一個來自 Digitalocean 的 VPS,一個 Ubuntu 12.04 伺服器。
我讀過一些使用:Apache -> Nginx -> Varnish,但我又讀到一些地方,如果你有 Nginx,你就不需要 Varnish。
我現在的解決方案是連接埠 80 上的 Varnish,有 2 個後端,一個用於 Apache,一個用於 Node.js。
這適用於 nginx 而不是 Varnish 後面的 Apache 嗎?
答案1
是的,如果您將Apache 切換為Nginx,您現有的設定(Varnish 前端,帶有Apache 和Node.js 後端)將工作相同(即Varnish 並不特別關心後端- Nginx 和Apache 將被同等對待) 。
Node.js 通常與 Nginx 一起運行,因為直接透過 Nginx 提供靜態資源比透過 Node.js 更有效率。對於簡單的設置,您可以完全消除 Varnish - Node.js 將在連接埠 80 上運行,並將選擇請求代理到 Node.js 上游伺服器:
upstream nodejs {
server 127.0.0.1:3000;
server 127.0.0.1:3000;
}
server {
listen 80 default_server;
server_name www.example.com;
error_log /var/log/nginx/www.example.com/error.log;
access_log /var/log/nginx/www.example.com/access.log;
root /var/www/www.example.com/public;
location / {
try_files $uri @nodejs;
}
location @nodejs {
proxy_pass http://nodejs;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
在此設定之前使用 Varnish 的(先前)原因之一是因為 Nginx 本身不支援代理 HTTP 1.1 連接(與 Websockets 一起使用的升級標頭需要它) - 這不再是問題,並且當前版本的Nginx 也能夠代理websocket 連接。除此之外,如果您實際上使用Varnish 來快取文件,這是將其保留為前端伺服器的一個令人信服的理由(不過,值得注意的是,如果您使用Varnish 來提供動態生成的快取副本,則唯一真正的收益會發生)頁)。例如,如果您的Nginx 監聽連接埠81,Node.js 監聽連接埠3000,那麼您的Varnish VCL 中可能有以下內容(它將使用Node.js 提供來自example.com/nodejs 的路徑,並使用Nginx 提供其他所有內容) :
backend default {
.host = "127.0.0.1";
.port = "81";
}
backend nodejs{
.host = "127.0.0.1";
.port = "3000";
}
...
sub vcl_recv {
set req.grace = 120s;
...
if (req.http.Host ~ "^(www\.)?example.com") {
if (req.url ~ "^/nodejs/") {
set req.backend = nodejs;
}
}
return (lookup);
}