nginx綁定多個域名涉及到的技術為url rewrite,可以先了解下知識背景再過來學習。
這里以域名:www.sample.com為例
1.在/usr/local/nginx/conf文件夾中創建sample.conf文件
2.在其中寫入如下內容並保存:
server{
listen 80;
server_name sample.cn www.sample.cn;
root /home/www/sample;
index index.html index.htm;
charset utf-8;
location / {
root /home/www/sample;
index index.html index.htm;
}
location ~* \.(jpg|gif|png)$ {
if (-f $request_filename) {
expires max;
break;
}
}
location ~ /\.ht {
deny all;
}
}
3.然后打開nginx.conf文件,在最后一行追加如下內容:
include linkcp.conf;
4.創建/home/www/sample文件夾,並在其中創建文件index.html內容如下
<html>
<head>
</head>
<body>
<p>Test Page!</p>
</body>
</html>
5.在瀏覽器中輸入域名www.sample.com顯示如下:
Test Page!
配置完成