Nginx部署以及反向代理和负载均衡!

一、Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定
二、nginx安装
官方网站:
http://nginx.org/
2.1 环境安装
2.1.1 需要安装gcc的环境。
#yum install gcc-c++
2.1.2 第三方的开发包。
PCRE
#yum install -y pcre pcre-devel
zlib
#yum install -y zlib zlib-devel
openssl
#yum install -y openssl openssl-devel
2.2安装步骤
第一步:把nginx的源码包上传到linux系统
第二步:解压缩
[root@localhost ~]# tar zxf nginx-1.8.0.tar.gz
第三步:使用configure命令创建一makeFile文件

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录。
[root@localhost sbin]# mkdir /var/temp/nginx/client -p
Mkdir -p 的意思是创建多级目录
第四步:# make
第五步:# make install
2.3启动nginx
[root@localhost sbin]# ./nginx
查看状态
[root@localhost sbin]# ps aux | grep nginx
2.4 关闭
关闭nginx:
[root@localhost sbin]# ./nginx -s stop
推荐使用:
[root@localhost sbin]# ./nginx -s quit
2.5 修改配置文件后需要重启nginx:
1、先关闭后启动
2、刷新配置文件:
[root@localhost sbin]# ./nginx -s reload
2.6 访问nginx
在这里插入图片描述
默认是80端口。
注意:是否关闭防火墙。
三、配置虚拟主机
就是在一台服务器启动多个网站;如何区分不同的网站:
1、域名不同
2、端口不同
3.1 通过端口区分不同虚拟机
Nginx的配置文件:
[root@localhost sbin] vi /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  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        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 #gzip  on;
   #一个server节点就是一个虚主机
    server {                                            
  
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
#Html是nginx安装目录下的html目录
            root   html;
            index  index.html index.htm;
        }
    }
}

添加虚拟主机

#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  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        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 
    server {
        listen       80;
         server_name  www.souhu.com;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            proxy_pass   http://tomcat1;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
          server_name  www.sina.com; 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            proxy_pass   http://tomcat2;
            index  index.html index.htm;
        }
    }
}

3.2重新加载配置文件
[root@localhost nginx]# sbin/nginx -s reload
四、反向代理
4.1 第一步:安装两个tomcat,分别运行在8080和8081端口。
第二步:启动两个tomcat。
[root@hadoop03 ROOT]# vi index.jsp
<div id="asf-box"> <h1>${pageContext.servletContext.serverInfo}--souhu.com</h1> </div>
4.3修改三处!!!!
[root@hadoop03 conf]# vi server.xml
将8005改为8006

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

将8080改为8081

  <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

将8009改为8010

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

4.4 修改配置文件nginx.conf
4.5nginx重新加载配置文件
#./nginx -s reload
4.6:配置域名(在Window系统中C:\Windows\System32\drivers\etc)
在hosts文件中添加域名和ip的映射关系
192.168.22.134 www.souhu.com
192.168.22.134 www.sina.com
五、负载均衡
5.1需要重新添加一个tomcat并修改配置文件
[root@hadoop03 ROOT]# vi index.jsp
[root@hadoop03 conf]# vi server.xml
5.2配置nginx.conf(最终的配置包含反射代理和负载平衡)

 
	upstream tomcat1 {
		server 192.168.22.134:8080;
    }
	upstream tomcat2 {
		server 192.168.22.134:8081;
		server 192.168.22.134:8082;
    }
	 server {
        listen       80;
        server_name  www.souhu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat1;
            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;
        #}
    }
server {
        listen       80;
        server_name  www.sina.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat2;
            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;
        #}
    }
	
    server {
        listen       80;
        server_name  www.sina333.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat2;
            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;
        #}
    }

5.3可以根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1

upstream tomcat2 {
	server 192.168.22.134:8081;
	server 192.168.22.134:8082 weight=5;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值