运维学习————nginx2-配置详解及负载均衡

目录

一、配置文件详解

1.1、结构

1.2、重要配置解释 

 1.3、详细配置

全局配置

 Events

HTTP 服务器配置 

 server虚拟主机配置

location URL匹配配置 

1.4、完整配置 

二、负载均衡

2.1、概念

2.2、集群规划及实现 

2.3、具体实现

2.3.1、克隆

2.3.2、修改tomcat1配置

启动测试

2.3.3、修改tomcat2配置 

启动测试

 2.3.4、负载均衡配置

2.3.4.1、轮询roundrobin(默认)

2.3.4.2、weight:指定轮询权值

2.3.4.3、ip_hash 

 2.3.4.4、fair(公平)

2.3.4.5、url_hash 


一、配置文件详解

1.1、结构

全局设置

       全局设置包含Events和HTTP

              HTTP包含upstream和多个Server

                       Server又包含多个location

1.2、重要配置解释 

main(全局设置,设置的指令将影响其他所有设置)

server(主机设置,用于指定主机和端口)

upstream(负载均衡服务器设置,设置一系列的后端服务器)

    location(URL匹配特定位置的设置,用于匹配网页位置)

 1.3、详细配置

全局配置

user是个主模块指令,指定Nginx Worker进程运行用户以及用户组,默认由nonody账号运行。

worker_processes是个主模块指令,指定了Nginx要开启的进程数。每个Nginx进程平均耗费10M~12M内存 ,支持5W并发。建议指定和CPU的数量一致即可。

error_log是个主模块指令,用来定义全局错误日志文件。日志输出级别有debug、info、notice、warn、error、 crit可供选择,其中,debug输出日志最为最详细,而crit输出日志最少。  (web 网站中常见的日志类别有:trace  debug  info   warn   error  fatal   每一个级别配置后向后兼容 )    log.error("日志信息-------进入了什么方法!!")

pid是个主模块指令,用来指定进程pid的存储文件位置。

worker_rlimit_nofile用于绑定worker进程和CPU, Linux内核2.4以上可用

 Events

事件指令是设定Nginx的工作模式及连接数上限

HTTP 服务器配置 

include是个主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度。类似于Apache中的include方法。

default_type属于HTTP核心模块指令,这里设定默认类型为二进制流,也就是当文件类型未定义时使用这种方式

log_format是Nginx的HttpLog模块指令,用于指定Nginx日志的输出格式。main为此日志输出格式的名称,可以在下面的access_log指令中引用。

client_max_body_size用来设置允许客户端请求的最大的单个文件字节数;

client_header_buffer_size用于指定来自客户端请求头的headerbuffer大小。对于大多数请求,1K的缓冲区大小已经足够,如果自定义了消息头或有更大的Cookie,可以增加缓冲区大小。这里设置为32K;

large_client_header_buffers用来指定客户端请求中较大的消息头的缓存最大数量和大小, “4”为个数,“128K”为大小,最大缓存量为4个128K;

 sendfile 参数用于开启高效文件传输模式。将tcp_nopush和tcp_nodelay两个指令设置为on用于防止网络阻塞;

 keepalive_timeout 设置客户端连接保持活动的超时时间。在超过这个时间之后,服务器会关闭该连接;

 client_header_timeout设置客户端请求头读取超时时间。如果超过这个时间,客户端还没有发送任何数据,Nginx将返回“Request time out(408)”错误;

 client_body_timeout设置客户端请求主体读取超时时间。如果超过这个时间,客户端还没有发送任何数据,Nginx将返回“Request time out(408)”错误,默认值是60;

 send_timeout指定响应客户端的超时时间。这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。

在安装目录的conf下nginx.conf配置(加入红字部分)

 server虚拟主机配置

server标志定义虚拟主机开始

     listen 用于指定虚拟主机的服务端口

     server_name 用来指定IP地址或者域名,多个域名之间用空格分开

     index 用于设定访问的默认首页地址

     root指令用于指定虚拟主机的网页根目录,这个目录可以是相对路径,也可以是绝对路径。

     Charset用于 设置网页的默认编码格式。

      access_log用来指定此虚拟主机的访问日志存放路径

      main用于指定访问日志的输出格式

location URL匹配配置 

URL地址匹配是进行Nginx配置中最灵活的部分。 location支持正则表达式匹配,也支持条件判断匹配,用户可以通过location指令实现Nginx对动、静态网页进行过滤处理。

    

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {

    root  /root/baidu;

    expires 30d;

}

通过location指令来对网页URL进行分析处理,所有扩展名以.gif、.jpg、.jpeg、.png、.bmp、.swf结尾的静态文件都交给nginx处理,而expires用来指定静态文件的过期时间,这里是30天。

这段设置是将upload和html下的所有文件都交给nginx来处理,当然,upload和html目录包含在/root/baidu/test目录中。

    location ~ ^/(upload|html)/ {

  root /root/baidu/test;

  expires 30d;

}

location是对此虚拟主机下动态网页的过滤处理,也就是将所有以.jsp为后缀的文件都交给本机的8080端口处理。

location ~ .*.jsp$ {

 index index.html;

 proxy_pass http://localhost:8080;

}

1.4、完整配置 


   
   
  1. #user nobody;
  2. worker_processes 1;
  3. # error_log logs / error.log;
  4. # error_log logs / error.log notice;
  5. # error_log logs / error.log info;
  6. #pid logs /nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_ type application /octet-stream;
  13. #log_ format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. # access_log logs / access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. upstream my_server {
  23. server 192.168.23.112: 8080;
  24. keepalive 2000;
  25. }
  26. server {
  27. listen 80;
  28. server_name localhost;
  29. #charset koi 8-r;
  30. # access_log logs /host. access.log main;
  31. location / {
  32. root html;
  33. index index.html index.htm;
  34. }
  35. location /my / {
  36. proxy_pass http: / /my_server /;
  37. }
  38. # error_ page 404 / 404.html;
  39. # redirect server error pages to the static page / 50x.html
  40. #
  41. error_ page 500 502 503 504 / 50x.html;
  42. location = / 50x.html {
  43. root html;
  44. }
  45. # proxy the PHP scripts to Apache listening on 127.0.0.1: 80
  46. #
  47. # location ~ \.php$ {
  48. # proxy_pass http: / / 127.0.0.1;
  49. #}
  50. # pass the PHP scripts to FastCGI server listening on 127.0.0.1: 9000
  51. #
  52. # location ~ \.php$ {
  53. # root html;
  54. # fastcgi_pass 127.0.0.1: 9000;
  55. # fastcgi_ index index.php;
  56. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  57. # include fastcgi_params;
  58. #}
  59. # deny access to .htaccess files, if Apache 's document root
  60. # concurs with nginx's one
  61. #
  62. # location ~ /\.ht {
  63. # deny all;
  64. #}
  65. }
  66. # another virtual host using mix of IP-, name-, and port-based configuration
  67. #
  68. #server {
  69. # listen 8000;
  70. # listen somename: 8080;
  71. # server_name somename alias another.alias;
  72. # location / {
  73. # root html;
  74. # index index.html index.htm;
  75. # }
  76. #}
  77. # HTTPS server
  78. #
  79. #server {
  80. # listen 443 ssl;
  81. # server_name localhost;
  82. # ssl_certificate cert.pem;
  83. # ssl_certificate_ key cert. key;
  84. # ssl_session_cache shared:SSL: 1m;
  85. # ssl_session_timeout 5m;
  86. # ssl_ciphers HIGH:!aNULL:!MD 5;
  87. # ssl_prefer_server_ciphers on;
  88. # location / {
  89. # root html;
  90. # index index.html index.htm;
  91. # }
  92. #}
  93. }

二、负载均衡

2.1、概念

为了保证服务的高可用,服务单元往往都是集群化(相同服务部署多份)部署的,当服务消费者消费服务时,负载均衡组件(F5(硬负载),nginx,ribbon,dubbo(软负载))获取服务提供者所有实例的注册信息,并通过一定的负载均衡策略(可以自己配置)选择一个服务提供者实例,向该实例进行服务消费,这样就实现了负载均衡

2.2、集群规划及实现 

2.3、具体实现

2.3.1、克隆

使用之前的tomcat克隆一个tomcat2,记住要修改tomcat的ip地址,并用远程连接工具连上

2.3.2、修改tomcat1配置

   
   
  1. cp -r /usr /apache-tomcat- 9.0.52 / /usr /tomcat 1
  2. cp -r /usr /apache-tomcat- 9.0.52 / /usr /tomcat 2
  3. cp -r /usr /apache-tomcat- 9.0.52 / /usr /tomcat 3

分别修改tomcat1,2,3的server.xml配置和随便改一个页面,知道访问的是当前端口下的项目就行:拿tomcat1举例

启动测试

   
   
  1. /usr /tomcat 1 /bin /startup.sh
  2. /usr /tomcat 2 /bin /startup.sh
  3. /usr /tomcat 3 /bin /startup.sh

浏览器访问:

http://192.168.37.181:8081/index.jsp

http://192.168.37.181:8082/index.jsp

http://192.168.37.181:8083/index.jsp

2.3.3、修改tomcat2配置 

   
   
  1. cp -r /usr /apache-tomcat- 9.0.52 /  /usr /tomcat 4
  2. cp -r /usr /apache-tomcat- 9.0.52 /  /usr /tomcat 5
  3. cp -r /usr /apache-tomcat- 9.0.52 /  /usr /tomcat 6

修改配置和上面一样,这里就不演示了

启动测试

   
   
  1. /usr /tomcat 4 /bin /startup.sh
  2. /usr /tomcat 5 /bin /startup.sh
  3. /usr /tomcat 6 /bin /startup.sh

浏览器输入:

http://192.168.37.182:8084/index.jsp

http://192.168.37.182:8085/index.jsp

http://192.168.37.182:8086/index.jsp

 2.3.4、负载均衡配置

upstream是Nginx的HTTP Upstream模块,这个模块通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡。

        Nginx的负载均衡模块目前支持5种调度算法,下面进行分别介绍,其中后两项属于第三方的调度方法

2.3.4.1、轮询roundrobin(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响

vim /usr/local/nginx/conf/nginx.conf
   
   


   
   
  1. #重启nginx服务
  2. /usr /local /nginx /sbin /nginx -s reload

测试:

http://192.168.37.183:8088/index.jsp

 你一直刷新这个页面,你会发现页面依次请求tomcat1,2,3,4,5,6这就是轮询方式

2.3.4.2、weight:指定轮询权值

修改配置

vim /usr/local/nginx/conf/nginx.conf
   
   

   
   
  1. upstream loadbalanceserver {
  2. server 192.168.37.181: 8081;
  3. server 192.168.37.181: 8082;
  4. server 192.168.37.181: 8083 weight = 50;
  5. server 192.168.37.182: 8084;
  6. server 192.168.37.182: 8085;
  7. server 192.168.37.182: 8086 weight = 100;
  8. }

重启nginx服务

/usr/local/nginx/sbin/nginx -s reload
   
   

一直刷新页面,你会发现只会出现tomcat6和3,其它的tomcat也会出现,只是次数很少(多刷几次),并且tomcat6出现次数比3多

2.3.4.3、ip_hash 

每个请求按访问IP的hash结果分配,这样来自同一个IP的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题


   
   
  1. upstream  myservers {
  2.          ip_hash;
  3.          server 192.168.170.11: 8081;
  4.          server 192.168.170.11: 8082;
  5.          server 192.168.170.11: 8083 weight = 20;
  6.          server 192.168.170.12: 8084;
  7.          server 192.168.170.12: 8085;
  8.          server 192.168.170.12: 8086;
  9.     }

一直都是一个tomcat 

 2.3.4.4、fair(公平)

不演示了

比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx本身是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块

2.3.4.5、url_hash 

不演示了

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率。Nginx本身是不支持url_hash的,如果需要使用这种调度算法,必须安装Nginx 的hash软件包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值