Nginx配置虚拟主机
我们在一台服务器上启动多个网站
如何区分不同的网站:
1、域名不同
2、端口不同
1.通过端口区分不同虚拟主机
首先编辑Nginx的配置文件 vim /usr/local/nginx/conf/nginx.conf

接着往下看配置文件,如下图所示。

添加主机
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;(域名)
#charset koi8-r;
#access_log logs/host.access.log main;
Location / {
root html;
index index.html index.htm;
}
}
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-81;
index index.html index.htm;
}
}
}
修改后html81在nginx目录下是没有的,我们复制一份html目录并命名为html81,如下所示。


为了区分出我们访问的nginx首页,我们进入html81目录,修改index.html,将<h1>Welcome to nginx!</h1>修改为<h1>Welcome to nginx81!</h1>,如下图所示。

重新启动nginx
[root@localhost nginx]# sbin/nginx -s reload

2.通过域名区分虚拟主机
什么是域名
域名就是网站。
www.baidu.com
www.taobao.com
www.jd.com
Tcp/ip
Dns服务器:把域名解析为ip地址。保存的就是域名和ip的映射关系。
一级域名:
Baidu.com
Taobao.com
Jd.com
二级域名:
www.baidu.com
Image.baidu.com
Item.baidu.com
三级域名:
1.Image.baidu.com
Aaa.image.baidu.com
一个域名对应一个ip地址,一个ip地址可以被多个域名绑定。
本地测试可以修改hosts文件。
修改window的hosts文件:(C:\Windows\System32\drivers\etc)
可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器。
先改一下本地的域名的映射关系

在Nginx中配置域名
下面我们到nginx的配置文件中再添加两个虚拟主机的配置,如下图所示,可以看到端口号都是默认的80,只是域名不一样而已,为了区分不同的网站,我们分别建一个html目录命名为html-163和html-sougou

html-163和html-sougou在nginx目录下默认是没有的,我们分别复制html目录并命名为html-baidu和html-sougou,

然后为了区别,我们像上面的操作一样修改index.html的显示内容
访问:


总结:
1.要配置本地的映射关系
2.配置nginx.conf文件
3.修改index.html的内容