對於每一個網站,我nginx的主持人,我有後者持有/etc/nginx/conf.d/
nginx的 - 如何使服務器範圍內的配置,每託管網站
例如服務器{}塊不同的文件...
/etc/nginx/conf.d/website1.co.uk
/etc/nginx/conf.d/website2.org
/etc/nginx/conf.d/website3.com
我發現自己重複同樣的代碼中的每臺服務器{}塊,並想知道是否有可能做一個「一網打盡」服務器{}塊來容納可重用的代碼。
這種新的「一網打盡」的文件將包括的東西,如...前
# Redirect all www. attempts to non-www.
server {
server_name www.$anything; hmm?
return 301 $scheme://$hostname$request_uri;
}
server {
server_name _; hmm?
# Add expires to static files
location ~* \.(?:ico|css|js|gif|jpe?g|png|bmp)$ {
expires max;
access_log off;
}
# Pass PHP files to PHP
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
有沒有人做到這一點?
我們是否必須爲我們託管的每個網站重複這些類型的通用代碼?
我沒有真正測試過這個,但我知道nginx server_name接受正則表達式,如果值以〜開頭。對於你的非www的例子,說你應該可以使用'server_name'〜^ [^ w] {3}。* $「'。更多文檔可以在這裏找到:http://nginx.org/en/docs/http/server_names.html#regex_names – 2013-04-11 19:17:51
我很遺憾地告訴你兩件事情:正則表達式在nginx上非常沉重,通常值得擁有多個PHP -FPM池 - 以及多個PHP「位置」塊。使用腳本生成它們 - 功能非常豐富! – 2013-04-11 20:15:48