安装nginx请参考,nginx编译安装的博文
1:配置nginx虚拟主机,同一个端口80,多个不同的域名。nginx默认主配置文件内容如下
[[email protected] conf]# cat nginx.conf user nginx; worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } include ./conf.d/*.conf; //此行添加是方面配置引用多个虚拟主机的配置文件。 }
2:需求使用同一个端口80,不同域名访问、
访问www.zxl.com 内容显示为www.zxl.com 访问www.bbs.com内容显示为www.bbs.com
配置www.zxl.com虚拟主机文件
在nginx主配置文件目录下创建子目录方便引用
[[email protected] conf]# mkdir conf.d
zxl.com.conf配置文件内容如下:
[[email protected] conf.d]# cat zxl.com.conf server { listen 80; server_name www.zxl.com zxl.com; location / { root /data/zxl; index index.html index.htm; access_log logs/zxl.access.log; error_log logs/zxl.error.log; } }
bbs.com.conf配置文件内容如下:
[[email protected] conf.d]# cat bbs.com.conf server { listen 80; server_name www.bbs.com bbs.com; location / { root /data/bbs; index index.html index.htm; access_log logs/bbs.access.log; error_log logs/bbs.error.log; } }
创建www.zxl.com和www.bbs.com访问目录文件
[[email protected] ~]# ls -ld /data/{zxl,bbs} drwxr-xr-x 2 root root 4096 Dec 18 10:08 /data/bbs drwxr-xr-x 2 root root 4096 Dec 18 12:36 /data/zxl
bbs和zxl目录下文件相应的内容如下:
[[email protected] ~]# cat /data/zxl/index.html <h1> This is a site www.zxl.com test! </h1> [[email protected] ~]# cat /data/bbs/index.html <h1> This is a site www.bbs.com test! </h1>
检查nginx语法查看是nginx相关配置文件是否有问题,nginx检查配置文件语法大法好!
出现ok以及is successful是正确的
[[email protected] ~]# /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
启动nginx
[[email protected] ~]# /usr/local/nginx/sbin/nginx [[email protected] ~]# lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 3476 root 10u IPv4 15998 0t0 TCP *:http (LISTEN) nginx 3477 nginx 10u IPv4 15998 0t0 TCP *:http (LISTEN)
配置hosts文件
[[email protected] ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 127.0.0.1 www.zxl.com zxl.com 127.0.0.1 www.bbs.com bbs.com
测试访问www.zxl.com和www.bbs.com结果如下:
[[email protected] ~]# elinks http://www.zxl.com --dump This is a site www.zxl.com test! [[email protected] ~]# elinks http://www.bbs.com --dump This is a site www.bbs.com test!
时间: 2024-11-01 09:17:52