Nginx介绍、安装与配置

来源:互联网 发布:卸载清理软件 编辑:程序博客网 时间:2024/06/06 00:03

一、Nginx简介

是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP代理服务器。

二、Nginx与Apache的区别

2.1、处理速度:

Apache占用很多内存,因此处理速度很慢,而Nginx很快、占内存少。

2.2、功能实现:

Apache的所以模块都支持动、静态编译,而Nginx模块都是静态编译

Apache对Fcgi的支持不好,而Nginx对Fcgi的支持非常好

  2.3、处理连接方式:

Nginx支持epoll,而Apache不支持

2.4、空间使用:

Nginx安装包只有几百K,对于Nginx,Apache就是个庞然大物

三、Nginx的工作原理:

单工作进程与多工作进程,默认单工作

注:详细介绍看《高性能Linux服务器构建实站》。

四、Nginx的安装与配置

ip地址192.168.30.132
安装环境准备:
1  配置好yum源
2  安装 开发工具软件包组    开发库软件包组
      yum -y groupinstall
 "开发工具"  "开发库"
3  编译工具是否安装  

rpm   -q    gcc    make    gcc-c++
4、service  httpd  status
      service  httpd  stop
      chkconfig  --level   35  off

5、创建nginx包提供的网站服务运行时进程所有者
useradd   -s /sbin/nologin  -M    www        

rpm  -qa  |  grep  -i  pcre
yum   -y   install   pcre-devel

6、安装源码nginx 软件包
unzip  nginx-package.zip
cd   nginx-package
tar -zxvf nginx-0.8.55.tar.gz
cd nginx-0.8.55
  ./configure  --help   查看配置参数
./configure  --prefix=/usr/local/nginx    --user=www    --group=www  --with-http_stub_status_module  --with-http_ssl_module 
make   
make   install 

--prefix=/usr/local/nginx   指定安装目录
--user=www   指定进程运行所有者  
--group=www  指定进程运行所属组
--with-http_ssl_module    支持https
--with-http_stub_status_module   安装能够查看nginx访问状态信息的模块

7、介绍Nginx下的相关目录
conf    配置文件存放目录    主配置文件  nginx.conf 
html   网页文件存放目录
logs    日志文件(错误日志   访问日志 )
sbin    脚本文件        nginx 启动服务的启动脚本

8、启动nginx服务
/usr/local/nginx/sbin/nginx 

/usr/local/nginx/sbin/nginx   选项
-v
-V
-t
-c
-s

netstat  -untlap  | grep  :80
进程名   nginx
数据传输协议  TCP
监听端口   80


停止服务  


使用脚本停止
 ./nginx  -s   stop


杀进程或 发送信号方式停止nginx 服务


pkill      -信号     进程名


kill        -信号     进程pid号


信号:
TERM, INT 快速关闭 
QUIT 从容关闭,关闭主进程顺便关闭工作子进程
HUP          重载配置用新的配置开始新的工作进程,从容关闭旧的工作进程 
USR1 重新打开日志文件 
USR2 平滑升级可执行程序
WINCH 从容关闭工作进程,不会立即关闭子进程


kill   -HUP  `cat  /usr/local/nginx/logs/nginx.pid`


二、平滑升级nginx服务软件版本(在不停止服务的情况下,升级服务软件的版本)


[root@pc254 logs]# netstat -untlap | grep :80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      6183/nginx          
[root@pc254 logs]# 


[root@pc254 logs]# ../sbin/nginx -v
nginx version: nginx/0.8.55
[root@pc254 logs]# ../sbin/nginx -V
nginx version: nginx/0.8.55
TLS SNI support disabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
[root@pc254 logs]# 


tar -zxvf nginx-1.0.5.tar.gz
cd nginx-1.0.5
./configure  --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module


make  
mv  /usr/local/nginx/sbin/nginx   /usr/local/nginx/sbin/nginx-low
cp  objs/nginx   /usr/local/nginx/sbin/


[root@localhost nginx-1.0.5]# make   upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
[root@localhost nginx-1.0.5]# 


[root@pc254 logs]# ../sbin/nginx -v
nginx: nginx version: nginx/1.0.5
[root@pc254 logs]#




三、配置nginx   (nginx.conf)
全局配置
http {


        server  { # 虚拟主机


                 location  /  {    #匹配客户端访问url地址


                 }


        }
       
}




1、虚拟主机(域名   端口    ip )
基于端口的虚拟主机 (通过端口区分客户端的访问)


访问方式:
http://localhost    (默认访问网站服务器的80端口)
http://localhost:端口号


案例要求:
http://192.168.1.254     ------------------->  安装目录/html 
http://192.168.1.254:8090    ----------------------> /weddir  


配置
vim  nginx.conf
http {  
    .............
    .............
 server  {
           listen  80;
           #server_name localhost;
           location / {
                  root  html;
                  index  index.html;
           }
    }
    server {
        listen       8090;
        #server_name  localhost;
        location / {
            root   /webdir;
            index  index.html    index.htm;
        }
   }
}                             
:wq


mkdir  /webdir
echo  aaaaaaaaaa   >  /webdir/index.html


./nginx   -t 
./nginx   -s stop
./nginx 




基于域名的虚拟主机 (通过主机名区分客户端的访问请求)


http://www.tarena.com   -----------------------   >  /wwwdir
http://bbs.tarena.com   -------------------------->  /bbsdir


mkdir /wwwdir 
mkdir   /bbsdir
echo   www   >  /wwwdir/index.html
echo   bbs  >  /bbsdir/index.html


vim  nginx.conf
http {  
    .............
    .............
 server  {
           listen  80;
           server_name  www.tarena.com;
           location / {
                  root   /wwwdir;
                  index  index.html;
           }
    }
    server {
        listen       80;
        server_name  bbs.tarena.com;
        location / {
            root   /webdir;
            index  index.html    index.htm;
        }
   }
}                             
:wq




客户端匹配
vim  /etc/hosts
192.168.1.254    www.tarena.com   www
192.168.1.254    bbs.tarena.com  bbs
:wq


elinks  --dump   http://www.tarena.com
                           www


elinks  --dump   http://bbs.tarena.com
                            bbs


基于ip地址的虚拟主机(通过ip地址区分用户访问请)
http://192.168.1.254             ------------------->   /wwwdir
http://192.168.1.253:8000  -------------------->   /bbsdir


* ip地址必须有对应的网卡接口才可以。(ifconfig  eth0:0   192.168.1.253)
vim   nginx.conf
http {  
    .............
    .............
 server  {
           listen  192.168.1.254:80;
           #server_name  www.tarena.com;
           location / {
                  root   /wwwdir;
                  index  index.html;
           }
    }
    server {
        listen       192.168.1.253:80;
        #server_name  bbs.tarena.com;
        location / {
            root   /webdir;
            index  index.html    index.htm;
        }
   }
}                             
:wq




客户端访问
[root@localhost ~]# elinks  --dump http://192.168.1.253
   webdir
[root@localhost ~]# elinks  --dump http://192.168.1.254
[root@localhost ~]# 


练习:根据访问路径配置虚拟主机(15分钟)
http://www.tarena.com  ------------------>  安装目录/html
http://www.tarena.com:8001  ------------>  /webdir




基于域名的虚拟主机  发布给公网用户使用的。
基于端口的虚拟主机,发布网站的管理页面
            192.168.1.254
http://bbs.tarena.com
http://192.168.1.254:8090




2、访问控制  (控制客户端对网站服务器的访问)


案例需求:只允许从ip地址是  192.168.1.1 客户端访问www.tarena.com:8001服务器,访问时要输入正确登录名webadim  密码123456 才可以访问。 


allow   允许 
deny   拒绝


客户端地址表示方式   :   all   匹配所有主机
                                         192.168.1.1      某个ip地址
                                         192.168.1.0/24   网段
location    /   {
#只允许192.168.1.1 和  192.168.1.2访问
               allow 192.168.1.1;
               allow  192.168.1.2;
               deny all;


#只不允许192.168.1.1访问
    #Deny   192.168.1.1;
    #Allow  all;
}




用户认证(访问网页文件时,要输入正确的用户名和密码)
location  /  {
   
         auth_basic  "auth-domain";  #指定认证域名称
         auth_basic_user_file    /usr/local/nginx/conf/authuser.txt;   #指定保存访问网页                                                                      时使用的用户名和密码信息文件的位置和文件名
}


[root@pc254 conf]# which   htpasswd 
/usr/bin/htpasswd
[root@pc254 conf]# rpm -qf /usr/bin/htpasswd 
httpd-2.2.3-74.el5
[root@pc254 conf]# htpasswd -c  /usr/local/nginx/conf/authuser.txt   webadmin
New password: 
Re-type new password: 
Adding password for user webadmin
[root@pc254 conf]# 


[root@pc254 conf]# cat /usr/local/nginx/conf/authuser.txt
webadmin:elL/BVeXruWM2
[root@pc254 conf]# 




3、nginx 反向代理( 分发策略    后端服务器状态设置)


                           clinet   http://1.1.1.254


                           1.1.1.254
                       nginx
                            192.168.1.254


         web_A           web_B    (service  httpd  start )
              10                20


vim  nginx.conf


http {
      upstream  "webgroup"  {
             server   192.168.1.10:80;
             server   192.168.1.20:80;
      }
 
     upstram  "tomcatgroup"  {
          server   192.168.1.100:8080;
     }


     server {
            ......
            ......
             location   /   {
               .....
               .....
               proxy_pass  http://webgroup;
             }
      }
}


分发请求策略:
轮询(默认的) 每个请求按时间顺序逐一分配到不同的后端服务器 
                         如果后断服务器down掉能自动剔除
                         权重值 默认是1 


weight  指定轮询几率 
              权重和访问比率成正比 
              通常用于后断服务器性能不同的情况
               默认值为1


1.1.1.200   http://1.1.1.254


ip_hash  每个请求按访问ip的hash结果分配 
              这样可以让每个访客固定访问一个后端服务器 ,可以解决session的问题


Fair  (默认不支持 要安装第三方软件)  
         按后端服务器的响应时间来分配请求 响应时间短的优先分配


指定后端服务器的状态:
down:    表示当前server暂时不参与负载
max_fails:允许请求失败的次数(默认为1), 当超过此次数时,返回                 proxy_next_upstream模块定义的错误


fail_timeout :max_fails次失败后,暂停提供服务的时间


backup:当其他所有的非backup机器down或者忙的时候,请求会发给backup机器,所以这台机器压力会最轻


upstream   sergrp  {
        #ip_hash;
        #server 192.168.8.5:80    weight=2; 
        server 192.168.8.5:80   down;
        server 192.168.8.4:80;
        server 192.168.8.6:80   backup;
        server 192.168.8.3:80   max_fails=2   fail_timeout=30s;
}


4、防盗链

0 0
原创粉丝点击