1、什么是Nginx的虚拟主机?

  答:虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响的。通过nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置,a、基于ip的虚拟主机, b、基于域名的虚拟主机 c、基于端口的虚拟主机。

2、Nginx的nginx.conf配置都代表什么意思?

 # 从第一个虚拟主机的例子可以看出nginx的配置文件结构如下。
# user 代表访问权限是什么,就是通过nginx访问linux 服务器中文件时,使用的用户权限。
user ftpuser; # 工作流程 ID。
worker_processes ; # events模块中包含nginx中所有处理连接的设置。
events {
# 工作进程的最大连接数量,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections,worker_processes为我们再main中开启的进程数。
worker_connections ;
} # 定义http服务器内容。里面的每个server就是一个虚拟主机。
http {
include mime.types; # 加载响应类型。
default_type application/octet-stream; # 默认使用 IO 流实现请求/应答。
sendfile on; # 是否支持文件传输。
keepalive_ timeout ; # 心跳监测。 # 配置一个服务节点。每个server就是一个虚拟主机。
server {
listen ; # 端口。监听的端口就是80
server_name localhost; # 监听的地址/IP/域名/主机名。 # 当前网络服务节点对应本地什么目录。
# 相对地址,从 nginx 安装目录开始寻址. 绝对地址从根开始寻址 。
# 请求过来后去那里找对应的资源,比如欢迎页。
location / {
# 修改web服务节点的根目录为ftpuser用户的主目录。
root /home/ftpuser; # 注意,这里的root是根目录root哦,
#root html; # 映射的根目录root。html是相对路径,html是nginx里面的html目录。
index index.html index.htm; # 欢迎页面。
} # 如果出现错误,显示的页面。
error_page /50x.html;
location = /50x.html {
root html;
}
}
}

如何在vim里面复制大段。

vim环境进行复制,粘贴。a、shift+v(即V)进入行选模式。b、上下箭头选中要复制的块。c、y进行复制。d、p进行粘贴。

3、如何根据端口进行区分虚拟主机。

nginx对外提供80和81两个端口监听服务。

请求80端口则请求html目录下的html,请求81端口则请求html81目录下的html。

将原来nginx的html目录拷贝一个目录"html81",为了方便测试需要修改每个目录下的index.html内容使之个性化。

修改/usr/local/nginx/conf/nginx.conf文件,新添加一个虚拟主机,如下:

 #user  nobody;
worker_processes ; events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; sendfile on; keepalive_timeout ;
# 配置虚拟主机
server {
# 监听的ip和端口,配置80
listen ;
# 虚拟主机名称这里配置ip地址
server_name 192.168.110.142;
# 所有的请求都以/开始,所有的请求都可以匹配此location
location / { # 比如访问http://192.168.110.142/login.html将找到/usr/local/nginx/html/login.html
# 比如访问http://192.168.110.142/register/login.html将找到/usr/local/nginx/html/register/register.html
# root html;
root /usr/local/nginx/html;
# 指定欢迎页面,按从左到右顺序查找
index index.html index.htm;
} }
#配置虚拟主机
server {
listen ;
server_name 192.168.110.142; location / {
root /usr/local/nginx/html81;
index index.html index.htm;
} } }

演示效果如下所示:

4、基于域名来区分的虚拟主机配置。

首先在C:\Windows\System32\drivers\etc\host文件里面添加如下配置内容:

然后你的nginx的nginx.conf配置如下所示:

 #user  nobody;
worker_processes ; #pid logs/nginx.pid; events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ; #gzip on; server {
listen ;
server_name localhost; location / {
# 比如访问http://192.168.110.142/login.html将找到/usr/local/nginx/html/login.html
# 比如访问http://192.168.110.142/register/login.html将找到/usr/local/nginx/html/register/register.html
# root html;
root /usr/local/nginx/html;
index index.html index.htm;
} # redirect server error pages to the static page /50x.html
error_page /50x.html;
location = /50x.html {
root html;
}
} server {
listen ;
server_name localhost; location / {
root html81;
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
error_page /50x.html;
location = /50x.html {
root html81;
} } server {
listen 192.168.110.142:; # 相同的端口号80
server_name www.baidu.com; # 域名,用来区分虚拟主机 location / { # 使用root指令指定虚拟主机目录即网页存放目录。
root html-baidu; # http://www.baidu.com/访问的是这个目录html-baidu下面的index.html,没有index.html就访问index.htm。如果都咩有就访问错误页面。
index index.html index.htm; # 指定欢迎页面,按从左到右顺序查找
}
# redirect server error pages to the static page /50x.html
error_page /50x.html;
location = /50x.html {
root html-baidu;
} } server {
listen 192.168.110.142:; # 相同的端口号80
server_name www.taobao.com; # 虚拟主机名称是www.baidu.com,请求域名www.baidu.com的url将由此server配置解析。# 域名,用来区分虚拟主机 location / { # 所有的请求都以/开始,所有的请求都可以匹配此location。
root html-taobao; # http://www.taobao.com/访问的是这个目录html-taobao下面的index.html,没有index.html就访问index.htm。如果都咩有就访问错误页面。
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
error_page /50x.html;
location = /50x.html {
root html-taobao;
} } }

效果如下所示:

6、什么是反向代理。

  通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中由代理服务器向Internet上的web服务器发起请求,最终达到客户机上网的目的。
  而反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

7、如何首先Nginx的反向代理呢?

  注意:你的请求,到达了Nginx反向代理服务器,然后由Nginx转发到应用服务器(例如Tomcat), Nginx实际是不处理请求的,做的事情即是请求的转发。反向代理服务器后面应该有应用服务器的(例如Tomcat服务器)。

  如何实现Nginx的反向代理呢,这里部署三台Tomcat服务器。第一台tomcat的端口号默认即可,第二台(8006、8081、8010)、第三台(8007、8082、8011)虚拟机的server.xml端口号依次加1。为了区分访问的tomcat服务器的不同,将/usr/local/tomcat/tomcat01/webapps/ROOT/index.jsp这个界面进行修改,以示区分。

  开始配置Nginx的请求转发,反向代理。修改完毕以后,重启你的Nginx,启动你的三台tomcat。

 #user  nobody;
worker_processes ; #pid logs/nginx.pid; events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ; #gzip on; server {
listen ;
server_name localhost; location / {
# 比如访问http://192.168.110.142/login.html将找到/usr/local/nginx/html/login.html
# 比如访问http://192.168.110.142/register/login.html将找到/usr/local/nginx/html/register/register.html
# root html;
root /usr/local/nginx/html;
index index.html index.htm;
} # redirect server error pages to the static page /50x.html
error_page /50x.html;
location = /50x.html {
root html;
}
} server {
listen ;
server_name localhost; location / {
root html81;
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
error_page /50x.html;
location = /50x.html {
root html81;
} } server {
listen 192.168.110.142:; # 相同的端口号80
server_name www.baidu.com; location / {
root html-baidu; # http://www.baidu.com/访问的是这个目录html-baidu下面的index.html,没有index.html就访问index.htm。如果都咩有就访问错误页面
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
error_page /50x.html;
location = /50x.html {
root html-baidu;
} } server {
listen 192.168.110.142:; # 相同的端口号80
server_name www.taobao.com; location / {
root html-taobao; # http://www.taobao.com/访问的是这个目录html-taobao下面的index.html,没有index.html就访问index.htm。如果都咩有就访问错误页面
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
error_page /50x.html;
location = /50x.html {
root html-taobao;
} } # 定义一个upstream tomcat01。配置一个代理即tomcat01服务器。
upstream tomcat01 {
# proxy_pass http://tomcat01找到upstream tomcat01的时候。指定ip和端口号。
server 192.168.110.142:;
} # 定义一个upstream tomcat02。配置一个代理即tomcat02服务器。
upstream tomcat02 {
# proxy_pass http://tomcat02 找到upstream tomcat02 的时候。指定ip和端口号。
server 192.168.110.142:;
} # 定义一个upstream tomcat03。配置一个代理即tomcat03服务器。
upstream tomcat03 {
# proxy_pass http://tomcat03 找到upstream tomcat03 的时候。指定ip和端口号。
server 192.168.110.142:;
} server {
listen 192.168.110.142:; # 相同的端口号80
server_name www.tomcat01.com; location / { # 域名www.tomcat01.com的请求全部转发到http://tomcat01即tomcat01服务上
# http请求过来以后找到http://tomcat01,proxy_pass http://tomcat01;找到上面定义的upstream tomcat01。
proxy_pass http://tomcat01; # 访问静态资源可以使用root,现在做反向代理使用的是proxy_pass(代理,请求转发)。
index index.html index.htm;
} } server {
listen 192.168.110.142:; # 相同的端口号80
server_name www.tomcat02.com; location / {
# http请求过来以后找到http://tomcat02,proxy_pass http://tomcat02;找到上面定义的upstream tomcat02。
proxy_pass http://tomcat02; # 访问静态资源可以使用root,现在做反向代理使用的是proxy_pass(代理,请求转发)。
index index.html index.htm;
} } server {
listen 192.168.110.142:; # 相同的端口号80
server_name www.tomcat03.com; location / {
# http请求过来以后找到http://tomcat03,proxy_pass http://tomcat03;找到上面定义的upstream tomcat03。
proxy_pass http://tomcat03; # 访问静态资源可以使用root,现在做反向代理使用的是proxy_pass(代理,请求转发)。
index index.html index.htm;
} } }

C:\Windows\System32\drivers\etc\host配置文件里面添加了如下配置:

192.168.110.142 www.tomcat01.com
192.168.110.142 www.tomcat02.com
192.168.110.142 www.tomcat03.com

效果如下所示:

8、什么是负载均衡?

  负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
  负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。

9、如何实现负载均衡?

  这里再次拷贝一个tomcat服务器。然后修改配置文件的端口号,和index.jsp界面用于区分,是否实现了负载均衡。这里是在上面反向代理的基础上添加了负载均衡,默认是使用的轮询。然后启动你新加的tomcat,重启Nginx即可。

 # 定义一个upstream tomcat01。
# upstream节点里面存在两台服务器。
upstream tomcat01 {
# proxy_pass http://tomcat01找到upstream tomcat01的时候。指定ip和端口号。
server 192.168.110.142:;
# 实现负载均衡,这个时候默认使用的是轮询,一个一次。以此循环。
server 192.168.110.142:;
}

实现效果如下所示:

负载均衡如何调整权重,如下所示:然后重启你的Nginx,进行查看效果即可。

 # 定义一个upstream tomcat01。
# upstream节点里面存在两台服务器。
upstream tomcat01 {
# proxy_pass http://tomcat01找到upstream tomcat01的时候。指定ip和端口号。
# 可以使用weight参数调整权重,默认都是1。如果想向某个机器多发送请求,可以配置如下所示。
server 192.168.110.142: weight=;
# 实现负载均衡,这个时候默认使用的是轮询,一个一次。以此循环。
# 权重weight=2的机会大于weight=1的。调整不同服务器的权重,可以根据服务器的性能进行调整的。
server 192.168.110.142: weight=;
}

权重weight=2的访问机会大于weight=1的。调整不同服务器的权重,可以根据服务器的性能进行调整的。效果如下所示:

9、为什么实现nginx负载均衡高可用?

  比如说,Nginx挂了,那么你的请求转发不到应用服务器,那么如何解决这个问题呢,这个时候就要保障Nginx的高可用的。如何实现Nginx的高可用呢。

10、什么是负载均衡高可用?

  Nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务,影响严重。
  为了屏蔽负载均衡服务器的宕机,需要建立一个备份机。主服务器和备份机上都运行高可用(High Availability)监控程序,通过传送诸如“I am alive”这样的信息来监控对方的运行状况。当备份机不能在一定的时间内收到这样的信息时,它就接管主服务器的服务IP并继续提供负载均衡服务;当备份管理器又从主管理器收到“I am alive”这样的信息时,它就释放服务IP地址,这样的主服务器就开始再次提供负载均衡服务。

待续......

最新文章

  1. DRY(Don't Repeat Yourself )原则
  2. jsp+servlet 中文乱码问题
  3. ubuntu引导修复
  4. [转] 多进程下数据库环境的恢复:DB_REGISTER
  5. VR快速发展下,从业者如何把握机会?
  6. Android 内存优化 (防Memory Leak)
  7. http协议分析工具【转】
  8. chrome密码管理
  9. Android——requestWindowFeature
  10. uva 437 hdu 1069
  11. 9月18日,SQL学习基础1
  12. Vim识别编码
  13. 数塔(dp)
  14. UI篇之——用户体验
  15. 基于C语言的UTF-8中英文替换密码设计
  16. 股票K线图
  17. Javascript高级编程学习笔记(95)—— WebGL(1) 类型化数组
  18. 【nodejs】初识 NodeJS(一)
  19. XBOX360
  20. e816. 创建工具栏

热门文章

  1. SpringMVC详解------参数绑定
  2. Java生鲜电商平台-B2B生鲜的互联网思维
  3. HTTP面试常见题
  4. SpringBoot(三) 配置文件 篇章
  5. PHP开发人员对JAVA的WEB开发入门(初版-基础知识)
  6. windows linux 子系统及windows terminal的使用。
  7. CSS 2D 转换
  8. log4cxx日志库在Windows+VS2017上的编译使用
  9. Android应用打开外部文件
  10. Troubleshooting ORA-01555 - Snapshot Too Old: Rollback Segment Number "String" With Name "String" Too Small (Doc ID 1580790.1)