Nginx

1、什么是Nginx

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

2、应用场景

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

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

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

     的情况。

3、Nginx安装

  1、官网下载源码包(http://nginx.org)

  2、安装环境要求

     ①需要安装gcc的环境。yum install gcc-c++

    ②第三方的开发包:

      yum install -y pcre pcre-devel

      yum install -y zlib zlib-devel

      yum install -y openssl openssl-devel

  3、安装步骤

    ①把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

    ④make

    ⑤make install

    

4、启动Nginx

  进入sbin目录

  [root@localhost sbin]# ./nginx 

  查看nginx是否启动

  

  关闭nginx

  [root@localhost sbin]# ./nginx -s stop

  推荐使用

  [root@localhost sbin]# ./nginx -s quit

  重启nginx

  ①先关闭后启动

  ②刷新配置文件:

  [root@localhost sbin]# ./nginx -s reload

5、访问Nginx

  Nginx默认是80端口

6、配置虚拟主机

  就是在一台服务器启动多个网站

  如何区分不同的网站:

    1、域名不同

    2、端口不同

  通过端口区分不同主机

  Nginx配置文件:/usr/local/nginx/conf/nginx.conf

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

  一个server节点就是一个虚拟主机,可以配置多个server。添加完server后需要重新加载配置文件:

  [root@localhost nginx]# sbin/nginx -s reload

  通过域名区分虚拟主机

  一个域名对应一个IP地址,一个IP地址可以被多个域名绑定。

  本地测试可以通过修改hosts文件,修改window的hosts文件:(C:\Windows\System32\drivers\etc)可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器。

  Nginx配置 

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

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-taobao;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  www.baidu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

  域名配置,修改hosts文件:

  192.168.25.148 www.taobao.com

  192.168.25.148 www.baidu.com

7、反向代理

  ①什么是反向代理

    

  反向代理服务器决定哪台服务器提供服务。反向代理服务器只提供请求的转发。

  ②Nginx实现反向代理

    两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。

      两个域名是www.sina.com和www.souhu.com

    

    

      第一步:安装两个tomcat,分别运行在8080和8081端口。

    第二步:启动两个tomcat。

    第三步:反向代理服务器的配置 

upstream tomcat1 {
    server 192.168.25.148:8080;
    }
    server {
        listen       80;
        server_name  www.sina.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
#通过http://tomcat1关联upstream tomcat1 proxy_pass http:
//tomcat1; index index.html index.htm; } } upstream tomcat2 { server 192.168.25.148:8081; } server { listen 80; server_name www.souhu.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://tomcat2; index index.html index.htm; } }

    第四步:nginx重新加载配置文件

    第五步:配置域名

    在hosts文件中添加域名和ip的映射关系

    192.168.25.148 www.sina.com

    192.168.25.148 www.souhu.com

  ③负载均衡

    如果一个服务由多条服务器提供,需要把负载分配到不同的服务器处理,需要负载均衡。

     upstream tomcat2 {

           server 192.168.25.148:8081;

           server 192.168.25.148:8082;

      }

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

    upstream tomcat2 {

           server 192.168.25.148:8081;

           server 192.168.25.148:8082 weight=2;

        }

posted @ 2017-09-07 11:06  IT-執念  阅读(210)  评论(0编辑  收藏  举报