一. 配置基于域名的虚拟主机
[[email protected] conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf [[email protected] conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 注:初学省略一些无关的配置参数,便于理解
1.1 创建域名对应的站点目录及文件
[[email protected] conf]# mkdir ../html/www [[email protected] conf]# echo "www.test.com" >> ../html/www/index.html [[email protected] conf]# cat ../html/www/index.html www.test.com [[email protected] conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.test.com; //域名 location / { root html/www; //站点目录 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
1.2 检查语法并重新加载配置文件
[[email protected] conf]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful [[email protected] conf]# /application/nginx/sbin/nginx -s reload [[email protected] conf]# netstat -lntup|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5267/nginx
1.3 测试www.test.com
注:这里测试主要分为Windows客户端和Linux客户端,测试的时候需要在客户端hosts文件里添加对应的解析记录,
Windows的hosts文件在C:\Windows\System32\drivers\etc,Linux的hosts文件在/etc/hosts
1.4 配置多个域名虚拟主机(www、bbs、blog)
[[email protected] conf]# mkdir ../html/bbs [[email protected] conf]# echo "bbs.test.com" >> ../html/bbs/index.html [[email protected] conf]# mkdir ../html/blog [[email protected] conf]# echo "blog.test.com" >> ../html/blog/index.html [[email protected] conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.test.com; //域名www.test.com location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name bbs.test.com; //域名bbs.test.com location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name blog.test.com; //域名 blog.test.com location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [[email protected] conf]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful [[email protected] conf]# /application/nginx/sbin/nginx -s reload
1.5 测试多个域名访问
二、配置基于端口的虚拟主机
2.1 修改配置文件nginx.conf
[[email protected] conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; //端口 server_name www.test.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 81; //端口 server_name bbs.test.com; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 82; //端口 server_name blog.test.com; location / { root html/blog; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
2.2 检查语法并重新加载配置文件
[[email protected] ~]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful [[email protected] ~]# /application/nginx/sbin/nginx -s reload [[email protected] ~]# netstat -lntup|grep nginx //查看端口是否监听 tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 5267/nginx tcp 0 0 0.0.0.0:82 0.0.0.0:* LISTEN 5267/nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5267/nginx
2.3 测试基于端口访问
三、基于IP的虚拟主机
3.1 在服务器网卡上增加多个IP
[[email protected] ~]# ip addr add 192.168.101.131/24 dev eth0 [[email protected] ~]# ip addr add 192.168.101.132/24 dev eth0 [[email protected] ~]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:d5:93:21 brd ff:ff:ff:ff:ff:ff inet 192.168.101.130/24 brd 192.168.101.255 scope global eth0 inet 192.168.101.131/24 scope global secondary eth0 inet 192.168.101.132/24 scope global secondary eth0 inet6 fe80::20c:29ff:fed5:9321/64 scope link valid_lft forever preferred_lft forever
3.2 修改配置文件nginx.conf
[[email protected] ~]# vim /application/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name 192.168.101.130; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 81; server_name 192.168.101.131; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 82; server_name 192.168.101.132; location / { root html/blog; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
3.2 检查语法并重新加载配置文件
[[email protected] ~]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful [[email protected] ~]# /application/nginx/sbin/nginx -s reload
3.3 测试基于IP访问
时间: 2024-11-03 12:44:16