Nginx Web 服务详解

1、初识Nginx软件

Nginx是一款很是优秀的web服务软件,不但能够作web服务软件,还能够作反向代理负载均衡和前端业务的缓存服务php

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

    2、做为web服务软件

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

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

    (二) nginx 软件安装

    一、安装ngix所须要的依赖包

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

    二、建立一个管理nginx进程的虚拟用户

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

    三、下载并解压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 前端

    四、编译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
说明:java

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

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

五、建立软链接

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

六、启动nginx

[root@jiangjunwang nginx-1.12.2]# /application/nginx/sbin/nginx正则表达式

七、访问测试

Nginx  Web 服务详解

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

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

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

[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  Web 服务详解
Nginx  Web 服务详解

看到如上效果表示一个基于域名的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日志配置实例

一、错误日志

  • 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;
    }

二、访问日志

  • 默认配置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服务便可

三、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网站为例进行说明

一、获取htpasswd命令设置帐号和密码

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

二、建立访问认证的帐号和密码

[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

三、编辑配置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 后边接认证密码文件;

四、访问测试

Nginx  Web 服务详解看到以下界面表示认证成功

相关文章
相关标签/搜索