Nginx演练(1)配置虚拟主机

简介:

本文演练的主要内容是:

  1. 使用Nginx,实现基于IP的虚拟主机

  2. 使用Nginx,实现基于域名的虚拟主机

  3. tomcat配置虚拟主机

1.前提

什么是虚拟主机?

虚拟主机使用是特殊的软硬件技术,把一台运行在Internet上的服务器主机分成一台台“虚拟"的主机,每台虚拟主机都可以是一台独立的网站,可以具有独立的域名,具有完整的Internet服务器功能(WWW,FTP,Email),同一台主机上的虚拟主机之间完全独立的。从网站访问者角度来看,每一台虚拟主机和一台独立主机完全一样。这些虚拟主机,可以共享CPU和内存。

wiki概念

2.使用Nginx,实现基于IP的虚拟主机

测试环境:

1
2
3
4
5
6
7
8
9
10
11
12
[root@hadoop1 ~] # uname -a
Linux hadoop1 2.6.32-358.el6.i686  #1 SMP Thu Feb 21 21:50:49 UTC 2013 i686 i686 i386 GNU/Linux
[root@hadoop1 ~] # ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:56:20:2B:84  
           inet addr:192.168.163.146  Bcast:192.168.163.255  Mask:255.255.255.0
           inet6 addr: fe80::250:56ff:fe20:2b84 /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:10487 errors:0 dropped:0 overruns:0 frame:0
           TX packets:1307 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:721155 (704.2 KiB)  TX bytes:215028 (209.9 KiB)
...



2.1 配置IP别名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
配置别名
 
ifconfig  eth0:1 192.168.163.147 broadcast 192.168.163.255 netmask 255.255.255.0 up
route add -host 192.168.163.147 dev eth0:1
 
ifconfig  eth0:2 192.168.163.148 broadcast 192.168.163.255 netmask 255.255.255.0 up
route add -host 192.168.163.148 dev eth0:2
 
该项配置,机器重启后失效,如果长期有效,最好将以上片段写入到 /etc/rc . local
 
结果
[root@hadoop1 ~] # ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:56:20:2B:84  
           inet addr:192.168.163.146  Bcast:192.168.163.255  Mask:255.255.255.0
           inet6 addr: fe80::250:56ff:fe20:2b84 /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:10487 errors:0 dropped:0 overruns:0 frame:0
           TX packets:1307 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:721155 (704.2 KiB)  TX bytes:215028 (209.9 KiB)
 
eth0:1    Link encap:Ethernet  HWaddr 00:50:56:20:2B:84  
           inet addr:192.168.163.147  Bcast:192.168.163.255  Mask:255.255.255.0
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 
eth0:2    Link encap:Ethernet  HWaddr 00:50:56:20:2B:84  
           inet addr:192.168.163.148  Bcast:192.168.163.255  Mask:255.255.255.0
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 
lo        Link encap:Local Loopback  
           inet addr:127.0.0.1  Mask:255.0.0.0
           inet6 addr: ::1 /128  Scope:Host
           UP LOOPBACK RUNNING  MTU:16436  Metric:1
           RX packets:30 errors:0 dropped:0 overruns:0 frame:0
           TX packets:30 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0 
           RX bytes:2424 (2.3 KiB)  TX bytes:2424 (2.3 KiB)


2.2 nginx.conf配置3个server节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
     server {
         # 监听的IP和端口
         listen 192.168.163.146:80;
         server_name 192.168.163.146;
         access_log logs /server1 .access.log combined;
 
         location /
         {
             index index.html index.htm;
             #存放目录
             root  /u01/up1 ;
         }
     }
 
     server {
         # 监听的IP和端口
         listen 192.168.163.147:80;
         server_name 192.168.163.147;
         access_log logs /server2 .access.log combined;
 
         location /
         {
             index index.html index.htm;
             #存放目录
             root  /u01/up2 ;
         }
     }
 
     server {
         # 监听的IP和端口
         listen 192.168.163.148:80;
         server_name 192.168.163.148;
         access_log logs /server3 .access.log combined;
 
         location /
         {
             index index.html index.htm;
             #存放目录
             root  /u01/up3 ;
         }
     }

2.3 准备3个站点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@hadoop1 u01] # cat /u01/up1/index.html 
<!doctype html>
<html lang= "en" >
< head >
         <meta charset= "UTF-8" >
         <title>index< /title >
< /head >
<body>
         this is server1
< /body >
< /html >
 
 
[root@hadoop1 u01] # cat /u01/up2/index.html 
<!doctype html>
<html lang= "en" >
< head >
         <meta charset= "UTF-8" >
         <title>index< /title >
< /head >
<body>
         this is server2
< /body >
< /html >
 
[root@hadoop1 u01] # cat /u01/up3/index.html 
<!doctype html>
<html lang= "en" >
< head >
         <meta charset= "UTF-8" >
         <title>index< /title >
< /head >
<body>
         this is server3
< /body >
< /html >
 
 
2.4 重启nginx,测试

wKiom1e9R7CjlB-AAABoXOalCUc624.png


3.使用Nginx,实现基于域名的虚拟主机

基于IP的虚拟主机技术,有一步骤要专门设置IP,一个IP对应一个主机,另外对企业来说,IP也是一种资源,这势必会引起IP资源短缺。 基于域名的虚拟主机,能很好的解决这个问题,也是比较流行和常用的虚拟主机技术。


3.1 修改nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  ...
     server {
         # 监听的IP和端口
         listen 192.168.163.146:80;
         server_name server1.domain.com;
         access_log logs /server1 .access.log combined;
 
         location /
         {
             index index.html index.htm;
             #存放目录
             root  /u01/up1 ;
         }
     }
 
     server {
         # 监听的IP和端口
         listen 192.168.163.146:80;
         server_name server2.domain.com;
         access_log logs /server2 .access.log combined;
 
         location /
         {
             index index.html index.htm;
             #存放目录
             root  /u01/up2 ;
         }
     }
 
     server {
         # 监听的IP和端口
         listen 192.168.163.146:80;
         server_name www.domain.com *.domain.com;
         access_log logs /server3 .access.log combined;
 
         location /
         {
             index index.html index.htm;
             #存放目录
             root  /u01/up3 ;
         }
     }
     ...

3.2重新加载

1
  /usr/local/nginx-1 .7.9 /sbin/nginx   -s reload

3.3 在测试主机添加域名解析(仿DNS)

我的测试主机是windows,所以在C:\Windows\System32\drivers\etc\HOSTS添加如下片段:

(如果是linux,需要修改/etc/hosts)

1
2
3
4
192.168.163.146 server1.domain.com
192.168.163.146 server2.domain.com
192.168.163.146 www.domain.com
192.168.163.146 www2.domain.com

3.4测试

wKiom1e9THLAAwjdAACL_2qF89Q170.png


4.tomcat配置虚拟主机

作为java程序员,tomcat服务器意义就不说了,tomcat也支持虚拟主机配置。

环境:windows7

4.1 编辑server.xml

可以不注释原有的localhost域名,增加两个Host,分别制定两个不同appBase目录。

1
2
3
4
5
6
7
     < Engine  name = "Catalina"  defaultHost = "localhost" >
       <!--
      <Host name="localhost" appBase="webapps"></Host>      
         -->
      < Host  name = "app1.domain.com"  appBase = "webapps01" ></ Host >      
      < Host  name = "app2.domain.com"  appBase = "webapps02" ></ Host >      
     </ Engine >

4.2 准备测试的app

4.2.1 在${tomcat_home}目录下,创建两个目录,webapps01,webapps02

4.2.2 创建app测试项目,并放入webapps01目录下

index.jsp

1
2
3
4
5
6
7
8
9
10
11
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!doctype html>
< html  lang = "en" >
< head >
     < meta  charset = "UTF-8" >
     < title >jsp</ title >
</ head >
< body >
     this is app01's jsp
</ body >
</ html >

web.xml

1
2
3
4
5
6
7
8
9
10
11
<? xml  version = "1.0"  encoding = "ISO-8859-1" ?>
< web-app  xmlns = "http://java.sun.com/xml/ns/javaee"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version = "2.5"
 
   < display-name >app</ display-name >
   < description >
      app test
   </ description >
</ web-app >
1
2
3
4.2.3 复制app项目到webapps02目录下,为了区分,调整下index.jsp内容即可。
4.2.4 重启tomcat
4.2.5 在C:\Windows\System32\drivers\etc\HOSTS添加如下片段
1
2
127.0.0.1 app1.domain.com
127.0.0.1 app2.domain.com


 4.2.6 测试

wKioL1e9U-Lwe2n3AABBClf0X3U017.png

4.2.7 这种配置方式,比较笨重,不太灵活。还有另外一种方式。


4.2.8.tomcat配置虚拟主机方式二

编辑server.xml

1
2
3
4
5
6
7
8
9
10
11
12
     ...
     < Engine  name = "Catalina"  defaultHost = "localhost" >
 
      < Host  name = "localhost"  appBase = "webapps" ></ Host >      
      < Host  name = "app1.domain.com"  appBase = "webapps" >
         < Context  path = "/app"  docBase = "D:\apache-tomcat-6.0.43\webapps01\app" ></ Context >
      </ Host >      
      < Host  name = "app2.domain.com"  appBase = "webapps" >
         < Context  path = "/app"  docBase = "D:\apache-tomcat-6.0.43\webapps02\app" ></ Context >
      </ Host >  
     </ Engine >
     ...

和上面配置效果一样的。个人比较倾向于第二种方式,更方便。


4.2.9

测试结果就不贴了。


总之: nginx配置好了虚拟主机,接下来就可以细化nginx配置,进一步支持缓存,安全,日志等等了。




本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1842019,如需转载请自行联系原作者
相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
23天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
24 0
|
1月前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
40 0
|
22天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
90 0
|
2天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
9 0
|
4天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
5天前
|
前端开发 JavaScript 应用服务中间件
修改Jeecg-boot context-path(附加图片+Nginx配置)
修改Jeecg-boot context-path(附加图片+Nginx配置)
12 0
|
15天前
|
域名解析 Ubuntu 应用服务中间件
Nginx实现虚拟主机
Nginx实现虚拟主机
|
15天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
22 0
|
16天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
26 0
|
25天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0