概念
虛擬主機(Virtual Host)可以在一臺服務器上綁定多個域名,架設多個不同的網站,一般在開發機或者要部署多個小網站的服務器上需要配置虛擬主機。nginx的虛擬主機配置其實也挺簡單,為了使得配置文件清晰,可以改每一個虛擬主機建立一個配置文件,然后在主配置文件(nginx.conf)里使用include語句包含所有的虛擬主機配置文件。
操作
- 建立存放虛擬主機配置文件的文件夾
sudo mkdir /usr/etc/nginx/vhosts
- 建立虛擬主機配置文件
sudo vim /usr/etc/nginx/vhosts/domain1.com.conf
- 在配置文件中,填寫如下內容(其中domain1.com需要替換成你自己的域名):
server {
listen 80;
server_name domain1.com www.domain1.com;
access_log /var/log/access_domain1.log main;
location / {
root /var/www/domain1.com;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/domain1.com/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
- 在主配置文件中,include所有的虛擬主機配置:
sudo vim /usr/local/etc/nginx/nginx.conf
#在 http 配置節的結束花括號 } 前一行加入如下語句
include /usr/local/etc/nginx/vhosts/*;
- 重新加載Nginx配置文件,完成配置
nginx -s reload