配置静态的web,须要实现一个虚拟主机。
step1: 准备工作
1 查看你的网卡地址(我的 192.168.223.135)
#ifconfig
我们这里建立两个web服务器,所以添加一个网卡地址。
#ifconfig eth0:0 192.168.223.145
2 建立两个网站文件夹
#mkdir /var/tmp/website1
#mkdir /var/tmp/website2
3 建立两个日志文件夹
#mkdir /var/tmp/log/website1
#mkdir /var/tmp/log/website2
4 创建两个測试页
#echo "this is website1" > /var/tmp/website1/index.html
#echo "this is website2" > /var/tmp/website2/index.html
step2: 改动配置文件,并添加一个server节点
1 找到你的nginx的安装文件夹,改动配置文件
#vim /usr/local/nginx/conf/nginx.conf
改动配置文件里的server节点
server {
listen 192.168.223.135:80;
server_name localhost;
#charset koi8-r;
access_log /var/tmp/log/website1/access.log;
error_log /var/tmp/log/website1/error.log;
location / {
root /var/tmp/website1;
index index.html index.htm;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
加入一个server节点
server {
listen 192.168.223.145:80;
server_name localhost;
#charset koi8-r;
access_log /var/tmp/log/website2/access.log;
error_log /var/tmp/log/website2/error.log;
location / {
root /var/tmp/website2;
index index.html index.htm;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2 启动nginx
#/usr/local/nginx/sbin/nginx
client 打开网页。訪问网站 http://192.168.223.135 http://192.168.223.145 试试
假设你认为这样输入ip地址来訪问过于丑陋。还能够改动成基于主机头的,比如:www.xxxxxxx.com形式
step 1: 改动server_name
我们仅仅须要改动上述两个server节点中的server_name就可以。
第一个改动为:server_name www.website1.com
第二个改动为:server_name www.website2.com
step2 :改动本机hosts文件
在hosts文件里加入
192.168.223.135 www.website1.com
192.168.223.145 www.website2.com
#echo "192.168.223.135 www.website1.com" >> /etc/hosts
#echo "192.168.223.145 www.website2.com" >> /etc/hosts
有的机器的hosts文件可能为仅仅读文件,改动须要root权限,你能够这样子干
为其加入一个写权限:
#chmod a+w /etc/hosts
然后再运行上面的加入指令。
step3: 重新启动nginx
#pkill nginx
#/usr/local/nginx/sbin/nginx
然后再客户机打开浏览器。输入www.website1.com www.website2.com 试试。