您的位置:首页 > 运维架构 > Nginx

Nginx简介,及日常应用, 负载均衡

2017-03-20 23:38 351 查看
一、Nginx是什么?

是一款轻量级的WEB服务器、反向代理服务器、以及电子邮件服务器。说是web服务器, 是可以当做apache、Tomcat这一类服务器作用相同。反向代理, 则是所有后台服务器从外网发请求的统一入口, 可以作为负载均衡。

Nginx的优点:

1、占用系统内存少

2、处理静态html等静态内容, 效率特别高

3、作为反向代理, 可以作为负载均衡服务器来分发请求

Nginx的缺点:

处理动态资源, 是鸡肋。 如JSP这种资源。效率低下。

二、Nginx的安装

2.1 Windows安装

官网地址:http://nginx.org/en/download.html

       我这里使用的是:nginx-1.10.3版本, 下载之后, 解压之后, 目录如下:



windows版本的nginx运行非常简单, 双击nginx.exe就可以启动nginx服务了。 因为nginx默认是以后台进程的方式运行, 所以, 没有类似Tomcat、Apache等服务器这种启动后的窗口。所以, 查看的时候, 可以直接在浏览器中输入localhost之后访问,如下图:



到此, 说明nginx服务已经启动好了。

Linux版本的Nginx安装:

将下载的nginx包放在指定目录中, 运行如下命令:

tar -zxvf nginx-1.11.10.tar.gz

cd nginx-1.11.10

./configure  --prefix=/usr/local/nginx

如果运行如上的configure命令报错,且信息如下:

./configure:error: the HTTP rewrite module requires the PCRE library.

You can either disablethe module by using --without-http_rewrite_module

option, orinstall the PCRE library into the system, or build the PCRE library

statically fromthe source with nginx by using --with-pcre=<path> option.

解决办法如下, 运行如下命令:
yum -y install pcre-devel openssl openssl-devel

再依次运行make , make  install  即可。

启动nginx服务:   

cd  /usr/local/nginx 

sbin/nginx  -t  -c  conf/nginx.conf

停止nginx服务:

sbin/nginx  -s  quit

三、Nginx的负载均衡配置

我们日常使用nginx主要用于作为负载均衡来使用。 首先准备两个Tomcat服务器, 分别修改server.xml配置文件中的如下三项内容, 其余默认即可:

tomcat1修改如下三项内容:

<Server port="18005" shutdown="SHUTDOWN">   

<Connector port="18080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

<Connector port="18009" protocol="AJP/1.3" redirectPort="8443" />

Tomcat2修改如下三项内容:

<Server port="28005" shutdown="SHUTDOWN">

<Connector port="28080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

<Connector port="28009" protocol="AJP/1.3" redirectPort="8443" />

为了区分这两个不同的Tomcat服务器, 这里修改webapps\ROOT\index.jsp文件中的标题内容, 如下:

<div id="asf-box">

                <h1>${pageContext.servletContext.serverInfo} ---tomcat1 </h1>

            </div>

Tomcat2中的内容修改如上类似。

之后分别启动Tomcat服务器即可。

接下来开始配置nginx的反向代理工作, 作为这两个Tomcat的负载均衡器。

三、Nginx的负载均衡配置

这里简单的进行配置, 完成负载均衡功能, 实际生产,应根据自己服务特点, 来进行相关配置。首先修改nginx.conf

内容如下:

#user  nobody;

worker_processes  2;  #工作进程的个数,一般与计算机的cpu核数一致 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connectio
4000
ns  1024; #单个进程最大连接数(最大连接数=连接数*进程数)

}

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指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,
#可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。  

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;  #长连接超时时间,单位是秒 

    gzip  on; #启用Gizp压缩  

#服务器的集群  

    upstream  netitcast.com {

        server    127.0.0.1:18080  weight=1;
#服务器配置   weight是权重的意思,权重越大,分配的概率越大。  

        server    127.0.0.1:28080  weight=2;  

    }     

#当前的Nginx的配置

    server {

        listen       80;

        server_name  localhost; #当前服务的域名

        #charset koi8-r;
charset utf-8;

        #access_log  logs/host.access.log  main;
location / {
proxy_pass http://netitcast.com;  

            proxy_redirect default; 
}

#        location / {

#            root   html;

#            index  index.html index.htm;

#        }

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

    #    }

    #}

}

之后, 进入dos命令行, 进入nginx服务目录下运行nginx.exe -t -s reload, 修改host文件, 路径在

C:\Windows\System32\drivers\etc下, 在host问价末尾增加如下内容:127.0.0.1       www.wgl.com

然后在浏览器中输入www.wgl.com , 则出现如下内容:





从上面可以只, 访问同一个www.wgl.com地址, 通过nginx实现了两台Tomcat之间的负载均衡。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: