tomcat+apache+nginx虚拟主机

本文详细介绍如何使用Nginx配置基于域名、IP和端口的虚拟主机,包括配置文件修改、服务重启和客户端测试步骤。同时,文章还提供了Apache虚拟主机配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  1. 修改配置文件:
    # 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 &quot;%r&quot; %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>
    
  2. 创建网页发布目录及测试网页

# 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/  &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值