Nginx的相关问题

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

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

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

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

如何在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文件,新添加一个虚拟主机,如下:

 1 #user  nobody;
 2 worker_processes  1;
 3 
 4 events {
 5     worker_connections  1024;
 6 }
 7 
 8 http {
 9     include       mime.types;
10     default_type  application/octet-stream;
11 
12     sendfile        on;
13     
14     keepalive_timeout  65;
15     # 配置虚拟主机
16     server {
17     # 监听的ip和端口,配置80
18         listen       80;
19     # 虚拟主机名称这里配置ip地址
20         server_name  192.168.110.142;
21     # 所有的请求都以/开始,所有的请求都可以匹配此location
22         location / {
23         
24         # 比如访问http://192.168.110.142/login.html将找到/usr/local/nginx/html/login.html
25         # 比如访问http://192.168.110.142/register/login.html将找到/usr/local/nginx/html/register/register.html
26         # root   html;
27         root /usr/local/nginx/html;
28         # 指定欢迎页面,按从左到右顺序查找
29         index  index.html index.htm;
30         }
31 
32     }
33     #配置虚拟主机
34     server {
35         listen       81;
36         server_name  192.168.110.142;
37 
38         location / {
39             root   /usr/local/nginx/html81;
40             index  index.html index.htm;
41         }
42 
43     }
44 
45 }

 演示效果如下所示:

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

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

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

  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #pid        logs/nginx.pid;
  5 
  6 
  7 events {
  8     worker_connections  1024;
  9 }
 10 
 11 
 12 http {
 13     include       mime.types;
 14     default_type  application/octet-stream;
 15 
 16     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 17     #                  '$status $body_bytes_sent "$http_referer" '
 18     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 19 
 20     #access_log  logs/access.log  main;
 21 
 22     sendfile        on;
 23     #tcp_nopush     on;
 24 
 25     #keepalive_timeout  0;
 26     keepalive_timeout  65;
 27 
 28     #gzip  on;
 29 
 30     server {
 31         listen       80;
 32         server_name  localhost;
 33         
 34         location / {
 35             # 比如访问http://192.168.110.142/login.html将找到/usr/local/nginx/html/login.html
 36             # 比如访问http://192.168.110.142/register/login.html将找到/usr/local/nginx/html/register/register.html
 37             # root   html;
 38             root /usr/local/nginx/html;
 39             index  index.html index.htm;
 40         }
 41 
 42         # redirect server error pages to the static page /50x.html
 43         error_page   500 502 503 504  /50x.html;
 44         location = /50x.html {
 45             root   html;
 46         }  
 47     }
 48 
 49 
 50     server {
 51         listen       81;
 52         server_name  localhost;
 53 
 54         location / {
 55             root   html81;
 56             index  index.html index.htm;
 57         }
 58         # redirect server error pages to the static page /50x.html
 59         error_page   500 502 503 504  /50x.html;
 60         location = /50x.html {
 61             root   html81;
 62         }
 63      
 64     }
 65 
 66     server {
 67         listen       192.168.110.142:80; # 相同的端口号80
 68         server_name  www.baidu.com; # 域名,用来区分虚拟主机
 69 
 70         location / {  # 使用root指令指定虚拟主机目录即网页存放目录。
 71             root   html-baidu; # http://www.baidu.com/访问的是这个目录html-baidu下面的index.html,没有index.html就访问index.htm。如果都咩有就访问错误页面。
 72             index  index.html index.htm; # 指定欢迎页面,按从左到右顺序查找
 73         }
 74         # redirect server error pages to the static page /50x.html
 75         error_page   500 502 503 504  /50x.html;
 76         location = /50x.html {
 77             root   html-baidu;
 78         }
 79      
 80     }
 81     
 82     
 83     server {
 84         listen       192.168.110.142:80; # 相同的端口号80
 85         server_name  www.taobao.com; # 虚拟主机名称是www.baidu.com,请求域名www.baidu.com的url将由此server配置解析。# 域名,用来区分虚拟主机
 86 
 87         location / { # 所有的请求都以/开始,所有的请求都可以匹配此location。
 88             root   html-taobao; # http://www.taobao.com/访问的是这个目录html-taobao下面的index.html,没有index.html就访问index.htm。如果都咩有就访问错误页面。
 89             index  index.html index.htm;
 90         }
 91         # redirect server error pages to the static page /50x.html
 92         error_page   500 502 503 504  /50x.html;
 93         location = /50x.html {
 94             root   html-taobao;
 95         }
 96      
 97     }
 98 
 99    
100 
101 }

效果如下所示:

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。

  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #pid        logs/nginx.pid;
  5 
  6 
  7 events {
  8     worker_connections  1024;
  9 }
 10 
 11 
 12 http {
 13     include       mime.types;
 14     default_type  application/octet-stream;
 15 
 16     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 17     #                  '$status $body_bytes_sent "$http_referer" '
 18     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 19 
 20     #access_log  logs/access.log  main;
 21 
 22     sendfile        on;
 23     #tcp_nopush     on;
 24 
 25     #keepalive_timeout  0;
 26     keepalive_timeout  65;
 27 
 28     #gzip  on;
 29 
 30     server {
 31         listen       80;
 32         server_name  localhost;
 33         
 34         location / {
 35             # 比如访问http://192.168.110.142/login.html将找到/usr/local/nginx/html/login.html
 36             # 比如访问http://192.168.110.142/register/login.html将找到/usr/local/nginx/html/register/register.html
 37             # root   html;
 38             root /usr/local/nginx/html;
 39             index  index.html index.htm;
 40         }
 41 
 42         # redirect server error pages to the static page /50x.html
 43         error_page   500 502 503 504  /50x.html;
 44         location = /50x.html {
 45             root   html;
 46         }  
 47     }
 48 
 49 
 50     server {
 51         listen       81;
 52         server_name  localhost;
 53 
 54         location / {
 55             root   html81;
 56             index  index.html index.htm;
 57         }
 58         # redirect server error pages to the static page /50x.html
 59         error_page   500 502 503 504  /50x.html;
 60         location = /50x.html {
 61             root   html81;
 62         }
 63      
 64     }
 65 
 66     server {
 67         listen       192.168.110.142:80; # 相同的端口号80
 68         server_name  www.baidu.com;
 69 
 70         location / {
 71             root   html-baidu; # http://www.baidu.com/访问的是这个目录html-baidu下面的index.html,没有index.html就访问index.htm。如果都咩有就访问错误页面
 72             index  index.html index.htm;
 73         }
 74         # redirect server error pages to the static page /50x.html
 75         error_page   500 502 503 504  /50x.html;
 76         location = /50x.html {
 77             root   html-baidu;
 78         }
 79      
 80     }
 81     
 82     
 83     server {
 84         listen       192.168.110.142:80; # 相同的端口号80
 85         server_name  www.taobao.com;
 86 
 87         location / {
 88             root   html-taobao; # http://www.taobao.com/访问的是这个目录html-taobao下面的index.html,没有index.html就访问index.htm。如果都咩有就访问错误页面
 89             index  index.html index.htm;
 90         }
 91         # redirect server error pages to the static page /50x.html
 92         error_page   500 502 503 504  /50x.html;
 93         location = /50x.html {
 94             root   html-taobao;
 95         }
 96      
 97     }
 98     
 99     # 定义一个upstream tomcat01。配置一个代理即tomcat01服务器。
100     upstream tomcat01 {
101         # proxy_pass http://tomcat01找到upstream tomcat01的时候。指定ip和端口号。
102         server 192.168.110.142:8080;
103     }
104     
105     # 定义一个upstream tomcat02。配置一个代理即tomcat02服务器。
106     upstream tomcat02 {
107         # proxy_pass http://tomcat02 找到upstream tomcat02 的时候。指定ip和端口号。
108         server 192.168.110.142:8081;
109     }
110     
111     # 定义一个upstream tomcat03。配置一个代理即tomcat03服务器。
112     upstream tomcat03 {
113         # proxy_pass http://tomcat03 找到upstream tomcat03 的时候。指定ip和端口号。
114         server 192.168.110.142:8082;
115     }
116 
117    server {
118         listen       192.168.110.142:80; # 相同的端口号80
119         server_name  www.tomcat01.com;
120 
121         location / { # 域名www.tomcat01.com的请求全部转发到http://tomcat01即tomcat01服务上
122             # http请求过来以后找到http://tomcat01,proxy_pass http://tomcat01;找到上面定义的upstream tomcat01。
123             proxy_pass http://tomcat01; # 访问静态资源可以使用root,现在做反向代理使用的是proxy_pass(代理,请求转发)。
124             index  index.html index.htm;
125         }
126      
127     }
128     
129     server {
130         listen       192.168.110.142:80; # 相同的端口号80
131         server_name  www.tomcat02.com;
132 
133         location / {
134             # http请求过来以后找到http://tomcat02,proxy_pass http://tomcat02;找到上面定义的upstream tomcat02。
135             proxy_pass http://tomcat02; # 访问静态资源可以使用root,现在做反向代理使用的是proxy_pass(代理,请求转发)。
136             index  index.html index.htm;
137         }
138      
139     }
140     
141     server {
142         listen       192.168.110.142:80; # 相同的端口号80
143         server_name  www.tomcat03.com;
144 
145         location / {
146             # http请求过来以后找到http://tomcat03,proxy_pass http://tomcat03;找到上面定义的upstream tomcat03。
147             proxy_pass http://tomcat03; # 访问静态资源可以使用root,现在做反向代理使用的是proxy_pass(代理,请求转发)。
148             index  index.html index.htm;
149         }
150      
151     }
152     
153 
154 }

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即可。

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

实现效果如下所示:

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

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

权重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地址,这样的主服务器就开始再次提供负载均衡服务。

待续......

猜你喜欢

转载自www.cnblogs.com/biehongli/p/11600125.html