設定nginx繫結多個域名
nginx繫結多個域名,可把多個域名規則寫一個配置檔案裡,也可分別建立多個域名配置檔案,一般為了管理方便,建議每個域名建一個檔案,有些同類域名也可寫在一個總的配置檔案裡。
一、每個域名一個檔案的寫法
首先開啟nginx域名配置檔案存放目錄:/usr/local/nginx/conf/servers ,如要繫結域名www.itblood.com 則在此目錄建一個檔案:www.itblood.com.conf然後在此檔案中寫規則,如:
server{
listen 80;
server_name www.itblood.com; #繫結域名
index index.htm index.html index.php; #預設檔案
root /home/www/itblood.com; #網站根目錄
include location.conf; #呼叫其他規則,也可去除
}
然後重起nginx伺服器,域名就繫結成功了nginx伺服器重起命令:/etc/init.d/nginx restart
二、一個檔案多個域名的寫法
一個檔案新增多個域名的規則也是一樣,只要把上面單個域名重複寫下來就ok了,如:
server{
listen 80;
server_name www.itblood.com; #繫結域名
index index.htm index.html index.php; #預設檔案
root /home/www/itblood.com; #網站根目錄
include location.conf; #呼叫其他規則,也可去除
}
server{
listen 80;
server_name msn.itblood.com; #繫結域名
index index.htm index.html index.php; #預設檔案
root /home/www/msn.itblood.com; #網站根目錄
include location.conf; #呼叫其他規則,也可去除
}
三、不帶www的域名加301跳轉
如果不帶www的域名要加301跳轉,那也是和繫結域名一樣,先繫結不帶www的域名,只是不用寫網站目錄,而是進行301跳轉,如:
server
{
listen 80;
server_name itblood.com;
rewrite ^/(.*) http://www.itblood.com/$1 permanent;
}
四、新增404網頁
新增404網頁,都可又直接在裡面新增,如:
server{
listen 80;
server_name www.itblood.com; #繫結域名
index index.htm index.html index.php; #預設檔案
root /home/www/itblood.com; #網站根目錄
include location.conf; #呼叫其他規則,也可去除
error_page 404 /404.html;
}
四種規則方法,就可以自己獨立解決nginx 多域名配置問題了。
配置多域名
方法一:多個的.conf方法(優點是靈活,缺點就是站點比較多配置起來麻煩)
這裡以配置2個站點(2個域名)為例,n個站點可以相應增加調整,假設:
IP地址:192.168.1.100
域名1 example1.com放在/ www / example1
域名2 example2.com放在/ www / example2
配置nginx虛擬主機的基本思路和步驟如下:
把2個站點example1.com,example2.com放到nginx可以訪問的目錄/ www /
給每個站點分別建立一個nginx配置檔案example1.com.conf,example2.com.conf,並把配置檔案放到/ usr / local / nginx / vhosts /
然後在/usr/local/nginx/nginx.conf裡面加一句包括進步2建立的配置檔案全部包含進來(用*號)
重啟nginx
1,開啟/usr/local/nginx/nginix.conf檔案,在相應位置加入包括把以上2個檔案包含進來
使用者www www;
worker_processes 1;
#主伺服器錯誤日誌
error_log /usr/local/nginx/log/nginx/error.log;
pid /usr/local/nginx/nginx.pid;
事件{
worker_connections 51200;
}
#主伺服器配置
http {
包括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”’;
傳送檔案;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip上
伺服器{
聽80
伺服器名稱 _;
access_log /usr/local/nginx/log/nginx/access.log main;
server_name_in_redirect關閉
位置 / {
root / usr / share / nginx / html;
index index.html;
}
}
#包含所有的虛擬主機的配置檔案
包括/ usr / local / nginx / vhosts / *;
}
2,在/ usr / local / nginx下建立vhosts目錄
mkdir / usr / local / nginx / vhosts
3,在/ usr / local / nginx / vhosts /裡建立一個名字為example1.com.conf的檔案,把以下內容拷進去
伺服器{
聽80
server_name example1.com www。example1.com;
access_log / www / access_ example1.log main;
位置 / {
root /www/example1.com;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root / usr / share / nginx / html;
}
#將PHP指令碼傳遞給FastCGI伺服器,偵聽127.0.0.1:9000
位置〜.php $ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/example1.com/$fastcgi_script_name;
包括fastcgi_params;
}
位置〜/.ht {
拒絕所有
}
}
4,在/ usr / local / nginx / vhosts /裡建立一個名字為example2.com.conf的檔案,把以下內容拷進去
伺服器{
聽80
server_name example2.com www。example2.com;
access_log / www / access_ example1.log main;
位置 / {
root /www/example2.com;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root / usr / share / nginx / html;
}
#將PHP指令碼傳遞給FastCGI伺服器,偵聽127.0.0.1:9000
位置〜.php $ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/example2.com/$fastcgi_script_name;
包括fastcgi_params;
}
位置〜/.ht {
拒絕所有
}
}
5,重啟Nginx
/etc/init.d/nginx重啟
方法二:
動態目錄方法(優點是方便,每個域名對應一個資料夾,缺點是不靈活)
這個簡單的方法比起為每一個域名建立一個vhost.conf配置檔案來講,只需要在現有的配置檔案中增加如下內容:
#
根據需求改變這個埠
聽80; #could也可以是1.2.3.4:80也可以是1.2.3.4:80的形式
#多個主機名由空格分隔。更換這些。
#多個主機名可以用空格隔開,當然這個資訊也是需要按照你的需求而改變的
。server_name star.yourdomain.com * .yourdomain.com http://www.*.yourdomain.com/;
#Alternately:_ *
#或者可以使用:_ *(具體內容參見本維基其他頁面)
root / PATH / TO / WEBROOT / $ host;
error_page 404 http://yourdomain.com/errors/404.html;
access_log logs / star.yourdomain.com.access.log;
location / {
root / PATH / TO / WEBROOT / $ host /;
index index.php;
}
#直接提供靜態檔案
#直接支援靜態檔案(從配置上看不是直接支援啊)
位置〜* ^。+。(jpg | jpeg | gif | css | png | js | ico | html)$ {
access_log off;
到期30d;
}
位置〜.php $ {
#一定要使用不同的伺服器為fcgi程序,如果你需要
#如果需要,你可以為不同的FCGI程序設定不同的服務資訊
fastcgi_pass 127.0.0.1:YOURFCGIPORTHERE;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME / PATH / TO / WEBROOT / $ host / $ fastcgi_script_name;
fastcgi_param QUERY_STRING $ query_string;
fastcgi_param REQUEST_METHOD $ request_method;
fastcgi_param CONTENT_TYPE $ content_type;
fastcgi_param CONTENT_LENGTH $ content_length;
fastcgi_intercept_errors on;
}
位置〜/.ht {
deny all;
}
最後附另外一個二級域名匹配的方法
繫結域名
server_name * .abcd.com;
獲取
主機名if($ host〜*(。*)。(。*)。(。*))
{
set $ domain $ 1;
}
定義目錄
root html / abc / $ domain /;
location /
{
root html / abcd / $ domain;
index index.html index.php;
數安時代(GDCA)致力於網路資訊保安,已通過WebTrust 的國際認證,是全球可信任的證書籤發機構。GDCA專業技術團隊將根據使用者具體情況為其提供最優的產品選擇建議,並針對不同的應用或伺服器要求提供專業對應的HTTPS解決方案。GDCA一直以“構建網路信任體系,服務現代數字生活”的宗旨,致力於提供全球化的數字證書認證服務。其自主品牌——信鑑易 TrustAUTH SSL證書:包括 OVSSL、EVSSL、程式碼簽名證書等。為涉足網際網路的企業打造更安全的生態環境,建立更具公信力的企業網站形象。