一、初识Nginx软件

Nginx是一款非常优秀的web服务软件,不但可以做web服务软件,还可以做反向代理负载均衡和前端业务的缓存服务

  1. 作为web服务软件 Nginx是一个支持高性能高并发的web服务软件,它具有很多优秀的特性,作为web服务器与apache相比nginx可以支持更多的并发连接访问,但占用的资源却更少,效率更高,在功能上也强大了许多
  2. 作为反向代理或负载均衡服务 在反向代理或负载均衡方面nginx可以作为web服务、php等动态服务及Memcached缓存代理服务,它具有类似专业反向代理软件(如haproxy)的功能,同时也是一个优秀的邮件代理服务软件
  3. 作为前端业务数据缓存服务 在web缓存服务方面,nginx可以通过自身的proxy_cache模块实现类似squid等专业缓存软件的功能 Nginx这三大功能是目前公司使用比较多的,特别是前两个功能 下面对nginx作为web服务器进行举例说明

    二、作为web服务软件

(一)nginx web服务应用的场景

  • 使用nginx运行html,js,css小图片等静态数据
  • nginx结合FastCGI运行php动态程序(fastcgi_pass)
  • Nginx结合tomcat/Resin等支持java动态程序(常用proxy_pass)

(二) nginx 软件安装

1、安装ngix所需要的依赖包

[root@jiangjunwang ~]# yum install -y pcre-devel openssl-devel 说明:pcre-devel: perl语言正则表达式兼容软件包、openssl-devel:使系统支持https方式访问

2、创建一个管理nginx进程的虚拟用户

[root@jiangjunwang ~]# useradd www -s /sbin/nologin/ -M

3、下载并解压nginx软件

[root@jiangjunwang ~]# mkdir /server/tools -p [root@jiangjunwang ~]# cd /server/tools/ [root@jiangjunwang tools]# wget http://nginx.org/download/nginx-1.12.2.tar.gz [root@jiangjunwang tools]# tar xf nginx-1.12.2.tar.gz

4、编译nginx软件

[root@jiangjunwang nginx-1.12.2]# ./configure --prefix=/application/nginx-12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module 说明:

  • --with-http_ssl_module   使nginx程序可以支持HTTPsF访问功能  
  • --with-http_stub_status_module   用于监控用户访问nginx服务情况

[root@jiangjunwang nginx-1.12.2]# make && make install

5、创建软连接

[root@jiangjunwang nginx-1.12.2]# ln -s /application/nginx-12.2 /application/nginx

6、启动nginx

[root@jiangjunwang nginx-1.12.2]# /application/nginx/sbin/nginx

7、访问测试

出现以上界面表示nginx安装成功建议使用谷歌浏览器进行访问

(三)、利用nginx配置基于域名的虚拟主机

简化配置文件 [root@jiangjunwang conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf b sed -n '10,21p' nginx.conf>../conf/extra/www.conf 编辑主配置文件

[root@jiangjunwang conf]# vim /application/nginx/conf/nginx.conf

worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; }

编辑虚拟机配置文件

[root@jiangjunwang html]# vim /application/nginx/conf/extra/bbs.conf

server { listen 80; server_name bbs.av.org; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

[root@jiangjunwang html]# vim /application/nginx/conf/extra/www.conf

server { listen 80; server_name www.av.org; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

编写测试index.html文件

[root@jiangjunwang html]# echo "bbs.av.org">/application/nginx/html/bbs/index.html [root@jiangjunwang html]# echo "www.av.org">/application/nginx/html/www/index.html

检查配置文件语法并重启服务

[root@jiangjunwang html]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-12.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-12.2/conf/nginx.conf test is successful [root@jiangjunwang html]# /application/nginx/sbin/nginx -s reload

**编写本机hosts文件并测试

看到如上效果表示一个基于域名的nginx虚拟主机配置完成

(四)配置主机的别名

所谓主机别名就是让一个IP地址对应多个域名主机,这也是在实际应用中常用的功能 配置方法接着上边的配置我们以bbs.av.com站点为例配置一个别名为bbs.a.com

[root@jiangjunwang extra]# vim bbs.conf

server { listen 80; server_name bbs.av.org bbs.a.com; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

然后重启nginx进行访问测试即可

(五)、nginx日志配置实例

1、错误日志

  • ** error_log的默认值为 ** default:error_log logs/error.log error;
  • 可放置的标签段为 context:main、http、server、location
  • 配置实例 编辑主配置文件添加error_log行即可

[root@jiangjunwang conf]# cat nginx.conf worker_processes 1; error_log logs/error.log error; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; }

2、访问日志

  • 默认配置access_log/acess.log combined;
  • 可放置的位置为 http、server、location、if inlocation 、limit_except; 配置实例: 这里我们已刚才配置的bbs站点为例,生成一个bbs.access.log日志文件

编辑主配置文件先定义一个log_format日志格式的参数

[root@jiangjunwang conf]# vim nginx.conf

worker_processes 1; error_log logs/error.log error; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; include extra/www.conf; include extra/bbs.conf; }

然后编辑bbs站点目录文件使其使用主配置文件定义的格式生成相应站点的访问日志

[root@jiangjunwang extra]# vim bbs.conf

server {

listen       80;

server_name bbs.av.org bbs.a.com; location / { root html/bbs; index index.html index.htm; } access_log logs/bbs.access.log main; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 最后检查语法重启nginx服务即可

3、access日志切割

在实际生产环境中access日志会逐渐变大,等大到一定程度的时候会打不开这个日志文件,所以需要我们定期的对日志文件进行切割

[root@jiangjunwang logs]# vim /server/scripts/cut_access.sh

#!/bin/bash

data_info=$(date +%F-%H:%M)

mv /application/nginx/logs/bbs.access.log /application/nginx/logs/access.log.$data_info /application/nginx/sbin/nginx -s reload

#cut nginx log cron 00 00 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null

(六)、location区块

location 指令的作用是根据用户请求的uri来执行不同的应用或者炒作 location基本的语法为

  • location [ = | ~ | ~* | ^~ ] uri { ... }

= --- 精确匹配网站uri资源信息 ~ --- 区分大小写匹配网站uri资源信息 ~* --- 不区分大小写匹配网站uri资源信息 ^~ --- 优先匹配网站uri资源信息 /AV/ --- 指定匹配网站资源目录信息 / --- 默认匹配网站资源信息 ! --- 对匹配的内容进行取反

  • 一个栗子 需求:内网用户可以访问www站点AV目录而外网用户不能访问 编写www站点文件

[root@jiangjunwang extra]# vim www.conf

server { listen 80; server_name www.av.org; root html/www; index index.html index.htm; location /AV { allow 172.16.1.0/24; deny 10.0.0.0/24; } } [root@jiangjunwang www]# echo "AV info" >AV/oldboy.html 检查语法重启nginx服务分别用内网172网段和外网10网段访问测试即可

(七)、Nginx rewrite区块

Nginx rewrite主要的功能就是实现URL地址重写,Nginx rewrite需要pcre软件的支持,通过prel兼容的正则表达式语法进行规则匹配; 一个栗子 通过rewrite模块实现访问av.org自动跳转到bbs.av.org 编辑bbs站点配置文件 方法一

[root@jiangjunwang extra]# vim bbs.conf

server { listen 80; server_name av.org; root html/bbs; index index.html index.html; rewrite ^/(.*) http://bbs.av.org/$1 permanent;

}

server {

 listen       80;
    server_name  bbs.av.org bbs.a.com;
    location / {
        root   html/bbs;
        index  index.html index.htm;
    }

access_log logs/bbs.access.log main; error_page 500 502 503 504 /50x.html; location = /50x.html { root html;

>    }
}

然后检查语法重启nginx服务即可

方法二 通过location区块和rewrite结合实现

[root@jiangjunwang extra]# vim bbs.conf

server {

listen       80;
    server_name  bbs.av.org bbs.a.com;
    location / {
        root   html/bbs;
        index  index.html index.htm;
    if ($host ~* "^av.org$") {
       rewrite ^/(.*) http://bbs.bbs.org/$1 permanent;
    }

access_log logs/bbs.access.log main; error_page 500 502 503 504 /50x.html; location = /50x.html { root html;

  }
}

} ~
然后检查语法重启nginx服务即可

(八)、Nginx 访问认证

有的时候在实际工作工作中企业要求我们为网站设置访问密码权限,这样操作后只有拥有账号密码的用户才可以访问网站,一般在企业内部使用 这里我们一前面配置的bbs网站为例进行说明

1、获取htpasswd命令设置账号和密码

[root@jiangjunwang extra]# yum -y install httpd

2、创建访问认证的账号和密码

[root@jiangjunwang extra]# htpasswd -bc /application/nginx/conf/htpasswd jiang 123456 [root@jiangjunwang extra]# chmod 400 /application/nginx/conf/htpasswd [root@jiangjunwang extra]# chown www /application/nginx/conf/htpasswd

3、编辑配置bbs站点配置文件

[root@jiangjunwang extra]# vim bbs.conf

server { listen 80; server_name av.org; rewrite ^/(.*) http://bbs.av.org/$1 permanent; }

server {

 listen       80;
    server_name  bbs.av.org;
    location / {
        root   html/bbs;
        index  index.html index.htm;
        auth_basic        "jun wang";
        auth_basic_user_file /application/nginx/conf/htpasswd;
    }
}

说明:auth_basic “jun wang” 这一行为验证的提示符;auth_basic_user_file 后边接认证密码文件;

4、访问测试

看到如下界面表示认证成功