nginx虚拟主机
基于域名
www.baidu.com /baidu
www.163.com html/163 /usr/local/nginx/html/163
www.qq.com /opt/qq
1.解析
2.修改配置文件
server {
listen 80;
server_name www.google.com;
location / {
root html/google;
index index.html index.htm;
}
}
server {
listen 80 default; \\default 在浏览器中直接键入IP地址会进入这个
server_name www.163.com;
location / {
root html/163; \\发布目录在主发布目录下的163目录
index index.html index.htm;
}
}
server {
listen 80;
server_name www.baidu.com;
location / {
root /baidu; \\发布目录在/baidu目录
index index.html index.htm;
}
}
基于IP:
server {
listen 192.168.10.11:80;
server_name web.11.com;
location / {
root /11/html;
index index.html index.htm;
}
}
server {
listen 192.168.10.12:80;
server_name web.12.com;
location /{
root /12/html;
index index.html index.htm;
}
}
基于端口 :
server {
listen 1111;
server_name web.11.com;
location /{
root /11/html;
index index.html index.htm;
}
}
server {
listen 1212;
server_name web.11.com;
location /{
root /12/html;
index index.html index.htm;
}
}
www.haha.com 192.168.1.100:8080 www
bbs.haha.com 192.168.1.100:8081 bbs
tomcat
-
修改配置文件:
# vim /usr/local/tomcat/conf/server.xml
<Engine name="Catalina" defaultHost="localhost"> #这里可以定义输入IP地址默认进入哪个网页 <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" #虚拟主机1 unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="www.test1.com" appBase="/myweb/test1" > #虚拟主机2 <Context path="" docBase="ROOT" /> </Host> <Host name="www.test2.com" appBase="/myweb/test2" > #虚拟主机3 <Context path="" docBase="ROOT" /> </Host> </Engine>
-
创建网页发布目录及测试网页
# mkdir -p /myweb/test{1,2}/ROOT
# vim /myweb/test1/ROOT/index.jsp
<html>
<head>
<title>JSP test page</title>
</head>
<body>
<% out.println("test1"); %>
</body>
</html>
3. 重启服务
4. 客户端解析,测试
apache
# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d/
# vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/var/www/www"
ServerName www.uplooking.com
ServerAlias uplooking.com
ErrorLog "/var/log/httpd/www-error_log"
CustomLog "/var/log/httpd/www-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/bbs"
ServerName bbs.uplooking.com
ErrorLog "/var/log/httpd/bbs-error_log"
CustomLog "/var/log/httpd/bbs-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/blogs"
ServerName blogs.uplooking.com
ErrorLog "/var/log/httpd/blogs-error_log"
CustomLog "/var/log/httpd/blogs-access_log" common
</VirtualHost>
# mkdir /var/www/www /var/www/blogs /var/www/bbs
# echo "www.uplooking.com" > /var/www/www/index.html
# echo "bbs.uplooking.com" > /var/www/bbs/index.html
# echo "blogs.uplooking.com" > /var/www/blogs/index.html
# systemctl restart httpd
客户端
# vim /etc/hosts
192.168.100.11 www.uplooking.com
192.168.100.11 bbs.uplooking.com
192.168.100.11 blogs.uplooking.com
# firefox http://blogs.uplooking.com/ &
# firefox http://bbs.uplooking.com/ &
# firefox http://www.uplooking.com/ &