一、一些基础知识
1.1 nginx特点
- 支持高并发
- 资源消耗少
- 可以做http反向代理及加速缓存,也就是负载均衡。(正向代理|反向代理)
- 可以实现squid等专业缓存功能。
- 支持异步IO模型epoll(同步:select,异步epoll)。
1.2 nginx应用场景
- 纯静态网站。
- 结合fastcgi运行php动态程序。
- Linux Nginx Mysql Php。
- LAMP(LAP、CGI Command Gateway Interface、fastcgi、fpm)
- 结合tomcat运行java动态程序(proxy_pass)
- 反向代理功能(upstream)
1.3 nginx安装
- 在centos系统下
方法1
- SSL功能:yum install openssl
- gzip模块所需:yum install zlib
- rewrite模块所需:yum install pcre
- 安装nginx所需的库:rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 安装nginx:yum install nginx
- 启动nginx:systemctl start nginx
- 测试,直接访问对应ip地址
方法2
- 安装依赖工具:yum install gcc automake pcre-devel openssl-devel zlib-devel
- 下载源码包:wget http://nginx.org/download/nginx-1.12.1.tar.gz
- 进行配置:./configure --prefix=/usr/local/nginx --with-http_ssl_module
- 安装Makefile编译软件,生成目标代码:make
- 安装到系统:make install
- 通用基本操作
- 启动:nginx
- 关闭:nginx -s stop
- 重启:nginx -s reload
- 开机自启
- 写入rc.local
1.4 nginx目录结构
[root@tysonscloud nginx-1.12.1]# tree /usr/local/nginx/
/usr/local/nginx/
|-- client_body_temp #客户端post一个比较大的文件时,长度超过了nginx缓冲区的大小,需要把这个文件的部分或者全部内容暂存到这里。
|-- conf #nginx所有配置文件的目录。
| |-- fastcgi.conf #fastcgi相关参数配置文件。
| |-- fastcgi.conf.default #fastcgi相关参数配置文件的备份
| |-- fastcgi_params #fastcgi参数文件
| |-- fastcgi_params.default #fastcgi参数文件的备份
| |-- koi-utf
| |-- koi-win
| |-- mime.types #媒体类型
| |-- mime.types.default #媒体类型的备份
| |-- nginx.conf #nginx主配置文件
| |-- nginx.conf.default #nginx主配置文件的部分
| |-- scgi_params #scgi相关参数
| |-- scgi_params.default
| |-- uwsgi_params #uwsgi相关参数
| |-- uwsgi_params.default
| `-- win-utf
|-- fastcgi_temp #fastcgi临时数据目录
|-- html #编译安装nginx时,默认的网页文件存放目录,类似于apache中的/var/www
| |-- 50x.html #错误替换文件,如果出现502会用此页面内容显示
| `-- index.html #默认返回的首页文件
|-- logs #nginx默认的日志文件,包括错误日志和访问日志
| |-- access.log #nginx默认的访问文件
| `-- error.log #nginx默认的错误文件
|-- proxy_temp #临时文件
|-- sbin
| `-- nginx #nginx的守护进程
|-- scgi_temp #临时目录
`-- uwsgi_temp #临时目录
二、nginx的配置文件
2.1 nginx的主配置文件(nginx.conf)
[root@tysonscloud nginx-1.12.1]# cat /usr/local/nginx/conf/nginx.conf
#user nobody;
#worker进程执行者
worker_processes 1;
#worker进程数量
#error_log logs/error.log; #错误日志
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#pid文件路径
#每个worker进程支持最大连接数,该区域是nginx的核心功能模块
events {
worker_connections 1024;
}
#http区块
http {
include mime.types;
#nginx支持的媒体类型
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区块
location / {
root html;
#站的根目录
index index.html
#站的默认首页
index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#错误状态码为50x时返回/50x.html
error_page 500 502 503 504 /50x.html;
#location区块
location = /50x.html {
root html;
}
#代理服务器配置
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#结合fastcgi实现php动态程序
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#新server区块,用于创虚拟主机
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
#http区块结束
2.2 nginx的虚拟主机
- 简介
- 虚拟主机在web服务中心相当于一个独立的网站站点,这个站点一般使用独立的域名或IP或端口,可以独立向外提供web服务。
- 虚拟主机类型
- 基于IP的虚拟主机
- 基于端口的虚拟主机
- 基于域名的虚拟主机
例如我们的规划是这样的:
- 有个域名
- www.pl1.com,网页目录/data/pl1
- www.pl2.com,网页目录/data/pl2
- 那么在nginx.conf中,我们应该这样设置。
server {
listen 80;
server_name www.pl1.com;
error_log logs/error_pl1.log;
access_log logs/access_pl1.log ;
location / {
root /data/pl1;
index index.html;
}
}
server {
listen 80;
server_name www.pl2.com;
error_log logs/error_pl2.log;
access_log logs/access_pl2.log;
location / {
root /data/pl2;
index index.html;
}
}
- 然后测试配置文件:
nginx -t
- 重启nginx服务:
nginx -s reload
- 优化虚拟主机配置
- include FILE
- nginx状态简则模块(http_stub_status_module)
- 我们通常在主配置文件中这么设置。
- 然后我们通过这样检测:
www.pl1.com/status
server{
listen 80;
server_name www.pl1.com;
location /status {
stub_status on; #<==打开状态信息开关
access_log off;
allow 10.1.1.0/24;
deny all;
}
}
2.3 nginx日志配置
- 错误日志
- error_log FILE LEVEL
- eg:
error_log logs/pl.com_err.log
- 访问日志
- access_log FILE 日志格式名称
- eg:
access_log logs/pl.com_access.log combined
- 日志格式默认combined
- 日志格式定义
log_format main '$remote_addr -$remote_user [$time_local] $request'
- 日志变量说明
Nginx日志变量 | 说明 |
---|---|
$remote_addr | 记录访问网站的客户端地址 |
$http_x_forwarded_for | 当前端有代理服务器时,设置web结点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的x_forwarded_for设置 |
$remote_user | 远程客户端用户名称 |
$time_local | 记录访问时间与时区 |
$request | 用户的http请求起始行信息 |
$status | http状态码,记录请求返回的状态 |
$body_bytes_sents | 服务器发送给客户端的响应body字节数 |
$http_referer | 记录此次请求是从哪个链接访问过来的,可以根据referer进行防盗链设置 |
$http_user_agent | 记录客户端访问信息 |
- 日志切割
- nginx日志逐渐增大,我们编写shell脚本定时切割。
#!/bin/bash
#nginx_log_rotate.sh
Dateformat=`date +%Y%m%d`
Basedir="/usr/local/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_pl.com"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
2.4 nginx的location配置
- 语法
- location [匹配标识] 匹配网站网址(URI){匹配URI后执行的配置段}
- 匹配规则(按优先级排序)
- location = URI:进行普通字符精确匹配
- location URI:普通字符模糊匹配
- location ~ URI:波浪线表示执行一个正则匹配,区分大小写
- location ~* URI:表示执行一个正则匹配,不区分大小写
- location ^~ URI:表示普通字符匹配,是一种拍他的匹配规则。
- 在主配置文件中我们这么写
server {
listen 80;
server_name www.pl.com;
root html/www;
location / {
return 401;
}
location = / {
return 402;
}
location /documents/ {
return 403;
}
location ^~ /images/ {
return 404;
}
location ~* \.(gif|jpg|jpeg)$ {
return 500;
}
}
curl -I http://www.pl1.com
402curl -I http://www.pl1.com/
402curl -I http://www.pl1.com/index.html
401curl -I http://www.pl1.com/documents/xx.html
403curl -I http://www.pl1.com/images/1.gif
404curl -I http://www.pl1.com/documents/q.jpg
500
2.5 nginx rewrite模块
- 应用场景
- 可以调整用户浏览的URL,看起来更规范,更加合乎开发及产品人员的需求。
- 为了让搜索引擎收录网站内容,并让用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。
- 网站换新域名后,让旧域名跳转到新的域名上。
- 根绝特殊变量、目录、客户端的信息进行URL跳转。
- 作用
- rewrite功能用来实现对url地址的重写。
- 格式
rewrite regx replacement [flag]
- 其中flag
flag标记符号 | 说明 |
---|---|
last | 本条规则匹配完成后,继续向下匹配新的location URI规则 |
break | 本条规则匹配完成即终止,不再匹配后面的任何规则 |
redirect | 返回302临时重定向,浏览器地址栏会显示跳转后的URL地址 |
pemanent | 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址 |
- 示例
rewrite ^/(.*) http://www.newpl.com/$1 permanent
- $1代表第一个分组。
- 例如,从
www.newpl.com/index.php
跳转到http://www.newpl.com/index.php
三、nginx各模块介绍
3.1 模块分类
- 核心功能模块(Core Functionality)
- 主要负责nginx的main区块和event区块,定义了nginx的全局参数。
- http标准功能模块集合
Nginx http 功能模块 | 模块说明 |
---|---|
ngx_http_core_module | 包括一些核心的http参数配置,对应Nginx的配置为HTTP区块部分 |
ngx_http_access_module | 访问控制模块,用来控制网站用户对Nginx的访问 |
ngx_http_gzip_module | 压缩模块,对Nginx返回的数据压缩,属于性能优化模块 |
ngx_http_fastcgi_module | FastCGI模块,和动态应用相关的模块,例如PHP |
ngx_http_proxy_module | proxy代理模块 |
ngx_http_upstream_module | URL地址重写模块 |
ngx_http_rewrite_module | URL地址重写模块 |
ngx_http_limit_conn_module | 限制用户并发连接数及请求数模块 |
ngx_http_limit_req_module | 根据定义的key限制Nginx请求过程的速率 |
ngx_http_log_module | 访问日志模块,以指定的格式记录Nginx客户访问日志等信息 |
ngx_http_auth_basic_module | Web认证模块,设置Web用户通过账号、密码访问Nginx |
ngx_http_ssl_module | ssl模块,用于加密的http连接,如https |
ngx_http_stub_status_module | 记录Nginx基本访问状态信息等的模块 |