目录:
修改host文件
- windows,host路径:C:\Windows\System32\drivers\etc
192.168.197.10 www.test1.com
192.168.197.10 www.test2.com
- linux ,host路径:/etc/hosts
修改nginx.conf
原Nginx配置文件
[root@localhost ~]# grep -v -E '#|^$' /usr/local/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 localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
修改后的配置:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /usr/local/nginx/conf/vhosts/*;
}
虚拟主机配置目录
cd /usr/local/nginx/conf/
mkdir vhosts
vhosts/test1.conf
server {
listen 80;
server_name www.test1.com;
access_log logs/test1.access.log;
location / {
root html/test/test1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
vhosts/test2.conf
server {
listen 80;
server_name www.test2.com;
access_log logs/test2.access.log;
location / {
root html/test/test2;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
发布目录
创建对应域名的发布目录
mkdir -p /usr/local/nginx/html/test/{test1,test2}
[root@localhost test1]# cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to test1</h1>
...
验证
[root@localhost test1]# /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
[root@localhost test1]# /usr/local/nginx/sbin/nginx -s reload