站點的根目錄和域名相同html
mkdir -p /data/site/a.chen.com複製代碼
cd /data/site/a.chen.com
touch inde.html
echo 'this is a from chen com...' > /data/site/a.chen.com/inde.html複製代碼
mkdir -p /data/site/b.chen.com複製代碼
cd /data/site/b.chen.com
touch inde.html
echo 'this is b from chen com...' > /data/site/b.chen.com/inde.html複製代碼
mkdir -p /data/logs/nginx複製代碼
(注:若還沒安裝nginx的話,詳情請看這一篇文章nginx
2.1.進入nginx配置目錄
cd /usr/local/nginx/conf/複製代碼
2.2.編輯nginx.conf
vim nginx.conf複製代碼
2.2.1.配置nginx日誌格式
在nginx.conf中找到以下內容,而且將#註釋標誌去掉瀏覽器
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';複製代碼
2.2.2.配置nginx主配置內容
a域名服務器bash
server{ server_name a.chen.com; listen 80; root /data/site/a.chen.com; access_log /data/logs/nginx/a.chen.com-access.log main; location / { } }複製代碼
b域名服務器服務器
server{ server_name b.chen.com; listen 80; root /data/site/b.chen.com; access_log /data/logs/nginx/b.chen.com-access.log main; location / { } }複製代碼
配置解析:curl
- server{}:配置虛擬主機必須有的的段
- server_name:虛擬主機的域名,能夠寫多個域名,相似於別名,好比說你能夠配置成:(這樣的話,任何一個域名,內容都是同樣的)
server_name a.chen.com b.chen.com c.chen.com複製代碼
- listen:127.0.0.1:80 監聽端口
- root :站點根目錄,網站文件存放的地方。(注:站點目錄和域名儘可能同樣)
- access_log:訪問日誌
- location /{}:默認uri
/usr/local/nginx/sbin/nginx -t複製代碼
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful複製代碼
/usr/local/nginx/sbin/nginx -s reload複製代碼
192.168.1.111 a.chen.com
192.168.1.111 b.chen.com複製代碼
echo '192.168.1.111 a.chen.com 192.168.1.111 b.chen.com' >> /etc/hosts複製代碼
curl http://a.chen.com複製代碼