• Nginx简单实现网站的负载均衡


     在大型网站搭建时,都会考虑如果用户量每日不断增加,大量的并发访问,会不会给网站、数据库带来崩盘的灾难。今天我们就讨论一下,现实中如何解决这些问题的一套最为容易实现的方案。

    控制并发,大家都会首先考虑的就是分布式、负载均衡等经常听到的It名词。那网站如何才能实现负载均衡呢,除了世面上的一些负载均衡器外,我们有哪些软件上的解决方案呢,这时候,Nginx、lvs 等名词就会在脑海中浮现。那这些负载均衡的软件如何使用呢,如何读者是.net工程师,大家会选择Nginx,因为它支持Windows服务器,这时候,好多网友会批判一下,说lvs更好更优秀。其实大家不必太在意,其实都一样,只要你能掌控它们就Ok,各有优劣。Nginx配置简单,在中小型项目中使用更为方便,下面我们看下Niginx在Windows下的配置,lvs在Linux的配置下一篇再写。

    概述:使用Nginx搭建反向服务器,实现网站服务器集群负载均衡

    1、下载Nginx——Windows版,(nginx-1.6.2.zip)在博客末端可下载,解压

    2、使用winsw-1.8-bin(windows服务工具).exe工具将Niginx发布成Domain模式,通过Windows服务的方式控制Niginx的运行。在博客末端可下载

    (1)配置 winsw运行的Xml文件,如上图:将Winsw工具移植到解压的Niginx文件下,将工具名字修改成“nginxServer.exe”,创建一个xml配置文件“nginxServer.xml”文件,它跟工具的名字一致,当然你也可以不要改名字。xml文件如下:

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <service>
     3 <id>nginx</id>
     4 <name>ngixServer</name>
     5 <description>High Performance Nginx Service</description>
     6 <executable>E:2014newTPractise
    ginx1.62Server
    ginx.exe</executable>
     7 <logpath>E:2014newTPractise
    ginx1.62Server</logpath>
     8 <logmode>roll</logmode>
     9 <depend></depend>
    10 <startargument>-p E:2014newTPractise
    ginx1.62Server</startargument>
    11 <stopargument>-p E:2014newTPractise
    ginx1.62Server -s stop</stopargument>
    12 </service>
    winsw 文件配置

    配置很简单主要指定Nginx.exe的位置log位置等,可以谷歌一下Winsw看看具体的配置信息。

    (2)点击nginxServer.exe安装服务,如果你是Win8以上的系统可能装不上,是因为兼容问题,调制兼容Win7模式,以管理员的身份运行即可,如图:

    这时查看Windows服务,启动NginxServer服务,如图:

    这时,ngixServer服务成功启动了。

    3、修改Nginx配置,将代理指向服务器集群,实现网站负载均衡

    在解压的Nginx文件夹下找到conf/nginx.conf文件,打开进行配置:

      1 #user  nobody;
      2 worker_processes  4;#启动的线程数
      3 
      4 #错误的位置和级别  
      5 #error_log  logs/error.log;
      6 #error_log  logs/error.log  notice;
      7 #error_log  logs/error.log  info;
      8 
      9 #pid        logs/nginx.pid;#pid进程文件的位置  
     10 
     11 
     12 events {
     13     worker_connections  1024;#每个进程的最大连接数  
     14 }
     15 
     16 
     17 http {
     18     include       mime.types;
     19     default_type  application/octet-stream;
     20      #nginx日志格式定义,在下面可以进行引用  
     21     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
     22     #                  '$status $body_bytes_sent "$http_referer" '
     23     #                  '"$http_user_agent" "$http_x_forwarded_for"';
     24 
     25     #access_log  logs/access.log  main;
     26 
     27     sendfile        on;
     28     #tcp_nopush     on;
     29 
     30     #keepalive_timeout  0;
     31     keepalive_timeout  65;
     32     upstream linuxidc.com{
     33         server 127.0.0.1:8091;    #服务器集群A 
     34          server 127.0.0.1:8092;    #服务器集群B
     35     }
     36     #gzip  on;
     37 
     38     server {
     39         listen       8090;
     40         server_name  localhost;
     41 
     42         #charset koi8-r;
     43 
     44         #access_log  logs/host.access.log  main;
     45 
     46         location / {
     47            root  html;
     48            index  index.html index.htm default.aspx;
     49            proxy_pass  http://linuxidc.com;
     50            proxy_redirect  default;
     51         }
     52 
     53         #error_page  404              /404.html;
     54 
     55         # redirect server error pages to the static page /50x.html
     56         #
     57         error_page   500 502 503 504  /50x.html;
     58         location = /50x.html {
     59             root   html;
     60         }
     61 
     62         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
     63         #
     64         #location ~ .php$ {
     65         #    proxy_pass   http://127.0.0.1;
     66         #}
     67 
     68         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     69         #
     70         #location ~ .php$ {
     71         #    root           html;
     72         #    fastcgi_pass   127.0.0.1:9000;
     73         #    fastcgi_index  index.php;
     74         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     75         #    include        fastcgi_params;
     76         #}
     77 
     78         # deny access to .htaccess files, if Apache's document root
     79         # concurs with nginx's one
     80         #
     81         #location ~ /.ht {
     82         #    deny  all;
     83         #}
     84     }
     85 
     86 
     87     # another virtual host using mix of IP-, name-, and port-based configuration
     88     #
     89     #server {
     90     #    listen       8000;
     91     #    listen       somename:8080;
     92     #    server_name  somename  alias  another.alias;
     93 
     94     #    location / {
     95     #        root   html;
     96     #        index  index.html index.htm;
     97     #    }
     98     #}
     99 
    100 
    101     # HTTPS server
    102     #
    103     #server {
    104     #    listen       443 ssl;
    105     #    server_name  localhost;
    106 
    107     #    ssl_certificate      cert.pem;
    108     #    ssl_certificate_key  cert.key;
    109 
    110     #    ssl_session_cache    shared:SSL:1m;
    111     #    ssl_session_timeout  5m;
    112 
    113     #    ssl_ciphers  HIGH:!aNULL:!MD5;
    114     #    ssl_prefer_server_ciphers  on;
    115 
    116     #    location / {
    117     #        root   html;
    118     #        index  index.html index.htm;
    119     #    }
    120     #}
    121 
    122 }
    nginx.conf 配置

    不用害怕,修改的地方很少。

    (1)worker_processes  4;#启动的线程数 一般为你代理服务器的内核数

    (2)在“HTTP”括弧中配置服务器群的网站发布的ip地址和端口号

    upstream linuxidc.com{
    server x.x.x.x:8091; #服务器A
    server x.x.x.x:8092; #服务器B

    }

    (3)配置代理服务器的地址,即Nginx安装的服务器地址、监听端口、默认地址

    server {
    listen 8090;    #监听端口
    server_name localhost;   #服务器Ip地址

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
    root html;
    index index.html index.htm default.aspx;  #默认网站首页地址
    proxy_pass http://linuxidc.com;
    proxy_redirect default;
    }

    重启Nginx Windows服务,收工完成,创建一个网站,ip、端口号、默认首页要与代理服务器Server配置一致哦,试试吧。。

    代码奉上:

    http://pan.baidu.com/s/1pJukQ2R

  • 相关阅读:
    vue-cli(vue脚手架)简单流程
    windows环境之node.js安装与环境配置
    fiddler的下载和简单使用
    Linux 配置nginx 代理tomcat,配置ssl
    来聊一聊导出数据问题
    作为一个开发人员应该具备怎么样技术栈和职业素养
    NODEJS的误打误撞
    聊一下程序员的日常
    openstack安装部署私有云详细图文
    openstack:OpenStack架构详解,
  • 原文地址:https://www.cnblogs.com/alvin_xp/p/4161162.html
Copyright © 2020-2023  润新知