Nginx

2017-03-07  本文已影响151人  FTOLsXD

什么是nginx?

Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。开源、免费。

Nginx的应用场景

1.http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。

2.虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。

3.反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

Nginx的安装步骤

第一步:把nginx的压缩包上传到服务器
第二步:解压。
第三步:创建一个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

注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

第四步:编译make
第五步:安装make install


Nginx实现虚拟机

可以实现在同一台服务运行多个网站,而且网站之间互相不干扰。

同一个服务器可能有一个ip,网站需要使用80端口。网站的域名不同。
区分不同的网站有三种方式:
<li>ip区分
<li>端口区分
<li>域名区分

Ip区分虚拟主机

需要一台服务器绑定多个ip地址。

方法一:
使用标准的网络配置工具(比如ifconfig和route命令)添加lP别名:

当前ip配置情况:

屏幕快照 2017-03-07 下午2.35.26.png

在eth0网卡再绑定一个ip:192.168.101.103
/sbin/ifconfig eth0:1 192.168.101.103 broadcast 192.168.101.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.101.103 dev eth0:1

屏幕快照 2017-03-07 下午2.36.32.png

方法二:
1、将/etc/sysconfig/network-scripts/ifcfg-eth0文件复制一份,命名为ifcfg-eth0:1
修改其中内容:
DEVICE=eth0:1
IPADDR=192.168.25.103
其他项不用修改
2、重启系统


配置nginx基于ip地址的虚拟主机

Nginx的配置文件


#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 / {
            root   html;
            index  index.html index.htm;
        }
    }

}

基于ip的虚拟主机配置

server {
        listen       80;
        server_name  192.168.25.141;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-141;
            index  index.html index.htm;
        }

        
    }

    server {
        listen       80;
        server_name  192.168.25.100;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-100;
            index  index.html index.htm;
        }

        
    }

Nginx重新加载配置文件。


基于端口的虚拟主机

server {
        listen       81;
        server_name  192.168.25.141;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-81;
            index  index.html index.htm;
        }

        
    }
    server {
        listen       82;
        server_name  192.168.25.141;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-82;
            index  index.html index.htm;
        }

        
    }


基于域名的虚拟主机

最有用的虚拟主机配置方式。
一个域名只能绑定一个ip地址,一个ip地址可以被多个域名绑定。

可以修改host文件实现域名访问。

设置域名和ip的映射关系


修改window的hosts文件:(C:\Windows\System32\drivers\etc)

基于域名的虚拟主机配置

server {
        listen       80;
        server_name  www.itheima.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-www;
            index  index.html index.htm;
        }

        
    }

    server {
        listen       80;
        server_name  hehe.itheima.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-hehe;
            index  index.html index.htm;
        }

        
    }

修改配置文件后,需要nginx重新加载配置文件。


Nginx的反向代理

什么是反向代理?

正向代理

反向代理

使用nginx实现反向代理

Nginx只做请求的转发,后台有多个http服务器提供服务,nginx的功能就是把请求转发给后面的服务器,决定把请求转发给谁。

安装tomcat

在一个虚拟机上创建两个tomcat实例,模拟多个服务器

需求

通过访问不同的域名访问运行在不同端口的tomcat
8080.hb.com 访问运行8080端口的tomcat
8081.hb.com 访问运行8081端口的tomcat

域名需要配置host文件:

Nginx的配置

upstream tomcatserver1 {
    server 192.168.25.141:8080;
    }
    upstream tomcatserver2 {
    server 192.168.25.141:8081;
    }
   server {
        listen       80;
        server_name  8080.itheima.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcatserver1;
            index  index.html index.htm;
        }

        
    }
    server {
        listen       80;
        server_name  8081.itheima.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcatserver2;
            index  index.html index.htm;
        }

        
    }

如果在同一个域名下有多台服务器提供服务,此时需要nginx负载均衡。


负载均衡

什么是负载均衡

负载均衡 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务.

需求

nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器。
nginx负载均衡服务器:192.168.25.141
tomcat1服务器:192.168.25.141:8080
tomcat2服务器:192.168.25.141:8081

配置nginx的负载均衡

配置负载均衡的权重

节点说明:
在http节点里添加:

#定义负载均衡设备的 Ip及设备状态 
upstream myServer {   

    server 127.0.0.1:9090 down; 
    server 127.0.0.1:8080 weight=2; 
    server 127.0.0.1:6060; 
    server 127.0.0.1:7070 backup; 
}

在需要使用负载的Server节点下添加

proxy_pass http://myServer;

upstream 每个设备的状态:

down 表示单前的server暂时不参与负载 
weight  默认为1.weight越大,负载的权重就越大。 
max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误 
fail_timeout:max_fails 次失败后,暂停的时间。 
backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。


Nginx的高可用

解决高可用的方案就是添加冗余

上一篇下一篇

猜你喜欢

热点阅读