阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

137次阅读
没有评论

共计 4534 个字符,预计需要花费 12 分钟才能阅读完成。

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

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

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

1、下载 Nginx——Windows 版,(nginx-1.6.2.zip)

可以到 Linux 公社资源站下载:

—————————————— 分割线 ——————————————

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是 www.linuxidc.com

具体下载目录在 /2017 年资料 / 2 月 / 7 日 /Nginx 简单实现网站的负载均衡 /

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

—————————————— 分割线 ——————————————

解压

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

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

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

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<service>
<id>nginx</id>
<name>ngixServer</name>
<description>High Performance Nginx Service</description>
<executable>E:\2014newT\Practise\nginx1.62Server\nginx.exe</executable>
<logpath>E:\2014newT\Practise\nginx1.62Server\</logpath>
<logmode>roll</logmode>
<depend></depend>
<startargument>-p E:\2014newT\Practise\nginx1.62Server</startargument>
<stopargument>-p E:\2014newT\Practise\nginx1.62Server -s stop</stopargument>
</service>

winsw 文件配置

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

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

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

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

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

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

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

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

#user  nobody;
worker_processes  4;# 启动的线程数

# 错误的位置和级别 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;#pid 进程文件的位置 

events {
    worker_connections  1024;# 每个进程的最大连接数 
}

http {
    include      mime.types;
    default_type  application/octet-stream;
    #nginx 日志格式定义,在下面可以进行引用 
    #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  0;
    keepalive_timeout  65;
    upstream linuxidc.com{
        server 127.0.0.1:8091;    #服务器集群 A
        server 127.0.0.1:8092;    #服务器集群 B
    }
    #gzip  on;

    server {
        listen      8090;
        server_name  localhost;

        #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;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root  html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass  http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root          html;
        #    fastcgi_pass  127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache’s document root
        # concurs with nginx’s one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen      8000;
    #    listen      somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root  html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen      443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root  html;
    #        index  index.html index.htm;
    #    }
    #}

}

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://www.linuxidc.com/Linux/2017-02/140351.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4534字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中