Nginx
Nginx
Nginx是一款轻量级Web服务器/反向代理服务器,占用内存少,并发能力强
下载和安装
源码安装
安装依赖包
GCC编译器:Nginx是使用C语言编写的程序,因此想要运行Nginx就需要安装一个编译工具。GCC就是一个开源的编译器集合,用于处理各种各样的语言,其中就包含了C语言。
PCRE:Nginx在编译过程中需要使用到PCRE库(perl Compatible Regular Expressoin 兼容正则表达式库),因为在Nginx的Rewrite模块和http核心模块都会使用到PCRE正则表达式语法。
zlib:在Nginx的各个模块中需要使用gzip压缩
OpenSSL:是一个开放源代码的软件库包,应用程序可以使用这个包进行安全通信,并且避免被窃听。
1
2# -y表示需要用户确认的地方默认yes
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel解压nginx安装包
最好放到一个特定的目录下管理,以后删除还需要
1
tar -zxvf nginx-1.16.1.tar.gz
进入目录执行命令来安装nginx
1
2
3
4检查环境,配置环境变量,设置安装到哪
./configure --prefix=安装路径
编译安装
make && make install
yum安装
相比源码安装,会添加有很多额外的配置信息
通过yum进行安装
1
yum install -y nginx
查看安装的目录
1
whereis nginx
启动
1
2
3
4# 默认安装到了/usr/sbin,配置文件在/etc/ngin/nginx.conf
cd /usr/sbin
# 启动
./nginx
卸载
源码方式
停止nginx
1
./nginx -s stop
删除nginx文件夹
1
rm -rf nginx/
到之前解压出来的源码目录下
1
make clean
yum方式
停止nginx
1
2./nginx -s stop
service nginx stop删除nginx自动启动
1
chkconfig nginx off
删除nginx本地文件
1
2
3rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginxyum清理
1
yum remove nginx
目录结构
- conf/nginx.conf nginx配置文件
- conf/mime.types HTTP协议中Content-Type的值和文件后缀名对应关系
- html 存放静态文件
- logs 存放日志
- sbin 二进制文件,用于启动、停止nginx
启动/关闭方式
信号控制方式
信号 | 作用 |
---|---|
TERM/INT | 立即关闭整个服务 |
QUIT | “优雅”地关闭整个服务 |
HUP | 重读配置文件并使用服务对新配置项生效 |
USR1 | 重新打开日志文件,可以用来进行日志切割 |
USR2 | 平滑升级到最新版的nginx |
WINCH | 所有子进程不在接收处理新连接,相当于给work进程发送QUIT指令 |
通过kill -signal PID
来使用
命令方式
./nginx -h
查看所有参数
-t
:检查配置文件是否有错误
-s
:signal信号,后面可以跟stop[快速关闭]、quit[优雅关闭]、reopen[重新加载日志文件]、reload[重新加载配置文件]
-c
:指定配置文件
平滑升级
不需要停止nginx服务器就可以升级版本
准备两个版本的Nginx分别是 1.14.2和1.16.1,1.14.2的编译安装,1.16.1的只需要编译
1
2
3
4
5
6# 1.14.2
./configure
make && make install
# 1.16.1
./configure
make首先备份老版本的nginx下的
sbin/nginx
1
2cd /usr/local/nginx/sbin
mv nginx nginxold拷贝新版本的nginx的
/objs/nginx
到老版本nginx的sbin/
目录下1
2cd ~/nginx/core/nginx-1.16.1/objs
cp nginx /usr/local/nginx/sbin执行
make upgrade
进行升级./nginx -v
查看版本
常用命令
1 |
|
配置文件
分为三大块:全局块、events块、http块
1 |
|
全局块
user指令
指定启动运行工作进程的用户及用户组,这样对于系统的权限访问控制的更加精细,也更加安全。
在配置文件中设置用户信息
1
user www;
创建一个linux用户
1
useradd www
创建
/home/www/html/index.html
文件,在www用户目录下用户有访问权限1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
<p><em>I am WWW</em></p>
</body>
</html>修改配置,测试访问
1
2
3
4location / {
root /home/www/html;
index index.html index.htm;
}
work process指令
master_process on|off
:用来指定是否开启工作进程,默认onworker_processes num/auto
:配置Nginx生成工作进程的数量,建议将该值和服务器CPU的内核数保存一致
这两个配置完要停止nginx再启动才会生效
其他指令
指令 | 作用 |
---|---|
daemon on|off | 设定Nginx是否以守护进程的方式启动,默认on |
pid file | 配置Nginx当前master进程的进程号ID存储的文件路径 |
error_log file [日志级别] | 配置Nginx的错误日志存放路径,日志级别越低显示的信息越多 |
include file | 引入其他配置文件 |
events块
主要用来设置nginx和用户的网络连接
指令 | 作用 |
---|---|
accept_mutex on|off | 设置Nginx网络连接序列化。off时为当来了一个请求,会唤醒多个worker来竞争; on时则将会对多个Nginx进程接收连接进行序列号,一个个来唤醒接收 |
multi_accept on|off | 设置一个工作进程是否允许同时接收多个网络连接 |
worker_connections number | 配置单个worker进程最大的连接数,默认512 |
use method | 设置Nginx服务器选择哪种事件驱动来处理网络消息,select/poll/epoll/kqueue |
例子
1 |
|
http块
指定MIME-Type
通过default_type mime-type;
来指定返回数据的类型,可以配置在http、server、location中
例子
1 |
|
自定义服务日志
指令 | 作用 |
---|---|
access_log path [format] | 设计用户访问请求日志的位置,后面可以跟日志输出格式 |
log_format name | 指定日志的输出格式 |
sendfile on|off | 是否使用sendfile()传输文件,该属性可以大大提高Nginx处理静态资源的性能 |
keepalive_timeout time | 设置长连接的超时时间 |
keepalive_requests number | 设置一个keep-alive连接使用的次数 |
server块和location块
server块
指令 | 作用 |
---|---|
listen address[:port] | 配置监听端口 |
server_name name | 设置虚拟主机服务名称。可用正则,如果同时匹配多个,则选最精确的 |
location块
location [ = |~ |
* |^|@ ] uri{…}设置请求的URI,=精确匹配,
正则,*正则不区分大小写,^~用于不包含正则表达式的uri前(和不加符号功能一致,但如果模式匹配,就停止搜索其他模式)root / alias path
root的处理结果是: root路径+location路径
alias的处理结果是:使用alias路径替换location路径
alias是一个目录别名的定义,root则是最上层目录的含义。
如果location路径是以/结尾,则alias也必须是以/结尾,root没有要求1
2
3
4
5
6
7
8
9# 浏览器'/images/1.png' 匹配到 '/usr/local/nginx/html/images/1.png'
location /images {
root /usr/local/nginx/html;
}
# 浏览器'/images/1.png' 匹配到 '/usr/local/nginx/html/1.png'
location /images {
alias /usr/local/nginx/html;
}index path
设置网站的默认首页
error_page code … [=[response]] uri
设置网站的错误页面
后面可以跟具体地址、重定向地址
1
2
3
4
5
6
7
8
9
10
11server {
error_page 404 http://www.baidu.com;
}
server{
error_page 404 /50x.html;
error_page 500 502 503 504 /50x.html;
location =/50x.html{
root html;
}
}还可以用来修改状态码
1
2
3
4
5
6
7# 返回的状态码为200
server{
error_page 404 =200 /50x.html;
location =/50x.html{
root html;
}
}
静态资源优化配置语法
1 |
|
1 |
|
添加模块步骤
以添加http_gzip_static_module
为例
查询当前Nginx的配置参数
1
nginx -V
将nginx安装目录下sbin目录中的nginx二进制文件进行更名(保存旧的)
1
mv nginx nginxold
进入Nginx的安装目录
1
cd /root/nginx/core/nginx-1.16.1
执行make clean清空之前编译的内容
1
make clean
使用configure来配置参数
1
./configure --with-http_gzip_static_module 后面把第一步查到的配置参数加上
使用make命令进行编译
1
make
将objs目录下的nginx二进制执行文件移动到nginx安装目录下的sbin目录中
1
mv objs/nginx /usr/local/nginx/sbin
执行更新命令
1
make upgrade
修改配置文件
1
2
3
4# 开启gzip_static
gzip_static on;
# reload重新加载配置文件,之后如果存在gz压缩好的文件,就会直接返回压缩好的
# gzip压缩文件指令:gzip 文件路径
缓存相关指令
- 指定缓存时间,填off不缓存
1 |
|
添加响应头
1
add_header name value
Cache-Control作为响应头信息,可以设置如下值
指令 说明 must-revalidate 可缓存但必须再向源服务器进行确认 no-cache 缓存前必须确认其有效性 no-store 不缓存请求或响应的任何内容 no-transform 代理不可更改媒体类型 public 可向任意方提供响应的缓存 private 仅向特定用户返回响应 proxy-revalidate 要求中间缓存服务器对缓存的响应有效性再进行确认 max-age=<秒> 响应最大Age值 s-maxage=<秒> 公共缓存服务器响应的最大Age值
跨域问题
同源: 协议、域名(IP)、端口相同即为同源
如果从服务器A的页面发送异步请求到服务器B获取数据,如果服务器A和服务器B不满足同源策略,则就会出现跨域问题。
解决方式
添加Access-Control-Allow-Origin来设置允许跨域访问的源地址信息
添加Access-Control-Allow-Methods来设置允许跨域访问的请求方式
例如
1 |
|
防盗链
盗链:将别人网页的内容放到自己的页面上
防盗链原理
通过判断HTTP的头信息Referer来判断获取资源的请求来源,只对允许的来源开放链接
防盗链步骤
例子
1 |
|
Rewrite功能配置
主要的作用是用来实现URL的重写
可以用来做域名跳转、域名镜像、给每个模块设置独立域名、合并目录
规则
set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28set $variable value
例
server {
listen 8081;
server_name localhost;
location /server {
set $name Tom;
set $age 18;
default_type text/plain;
return 200 $name=$age;
}
}
nginx有预设的全局变量
例:用全局变量记录日志
log_format main '$remote_addr - $request - $status - $request_uri - $http_user_agent';
server {
listen 8081;
server_name localhost;
location /server {
access_log logs/access.log main;
set $name Tom;
set $age 18;
default_type text/plain;
return 200 $name=$age;
}
}if
1
2
3
4
5
6
7
8
9
10if (condition){...}
1. 如果变量名对应的值为空或者是0,if都判断为false,其他条件为true。
2. 使用"="和"!="比较变量和字符串是否相等,满足条件为true,不满足为false
例
if ($request_method = POST){
return 405;
}
3. 使用正则表达式对变量进行匹配,匹配成功返回true,否则返回false。变量与正则表达式之间使用"~","~*","!~","!~\*"来连接。
4. 判断请求的文件是否存在使用"-f"和"!-f", 判断请求的目录是否存在使用"-d"和"!-d"break
1
2该指令用于中断当前相同作用域中的其他Nginx配置
会去找与访问路径对应目录下的index.html文件return
1
2
3return code [text];
return code URL;
return URL;rewrite
1
2
3
4
5
6
7rewrite regex replacement
replacement:匹配成功后,用于替换URI中被截取内容的字符串
flag:用来设置rewrite对URI的处理行为,可选值有如下:
- last: 替换完后重新匹配
- break: 替换完后去找与访问路径对应目录下的index.html文件
- redirect:永久重定向
- permanent:临时重定向rewrite_log
1
2
3
4
5rewrite_log on\|off
配置是否开启URL重写日志的输出功能
rewrite_log的日志输出级别为notice
所以需要设置日志输出级别为notice才能看到日志
error_log logs/error.log notice;
反向代理
proxy_pass
1
2
3
4
5
6
7
8
9
10
11
12
13proxy_pass URL
设置被代理服务器地址,可以是主机名称、IP地址加端口号形式
例
server{
listen 80;
server_name localhost;
location /server{
#proxy_pass http://192.168.200.146;
proxy_pass http://192.168.200.146/;
}
}
url后面加/则最终找的是url/index.html,不加则为url/server/index.htmlproxy_set_header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18proxy_set_header field value
更改Nginx服务器接收到的客户端请求的请求头信息
例
server {
listen 8080;
server_name localhost;
default_type text/plain;
return 200 $http_username;
}
server {
listen 8081;
server_name localhost;
location /server {
proxy_pass http://47.96.80.163:8080;
proxy_set_header username Cat;
}
}proxy_redirect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23proxy_redirect redirect replacement;
redirect:目标,Location的值
replacement:要替换的值
重置头信息中的"Location"和"Refresh"的值
例
服务端[192.168.200.146]
server {
listen 8081;
server_name localhost;
if (!-f $request_filename){
return 302 http://192.168.200.146;
}
}
代理服务端[192.168.200.133]
server {
listen 8081;
server_name localhost;
location / {
proxy_pass http://192.168.200.146:8081/;
proxy_redirect http://192.168.200.146 http://192.168.200.133;
}
}具体案例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39案例:将不同请求代理到不同服务器
代理服务器
server {
listen 8082;
server_name localhost;
location /server1 {
proxy_pass http://192.168.200.146:9001/;
}
location /server2 {
proxy_pass http://192.168.200.146:9002/;
}
location /server3 {
proxy_pass http://192.168.200.146:9003/;
}
}
服务端
server1
server {
listen 9001;
server_name localhost;
default_type text/html;
return 200 '<h1>192.168.200.146:9001</h1>'
}
server2
server {
listen 9002;
server_name localhost;
default_type text/html;
return 200 '<h1>192.168.200.146:9002</h1>'
}
server3
server {
listen 9003;
server_name localhost;
default_type text/html;
return 200 '<h1>192.168.200.146:9003</h1>'
}
第七层负载均衡
负载均衡作用
- 解决服务器的高并发压力,提高应用程序的处理性能。
- 提供故障转移,实现高可用。
- 通过添加或减少服务器数量,增强网站的可扩展性。
- 在负载均衡器上进行过滤,可以提高系统的安全性。
实例
被代理服务器:47.96.80.163:9001、47.96.80.163:9002、47.96.80.163:9003
负载均衡器:47.96.80.163:8081
默认方式为轮询
1 |
|
负载均衡状态
状态 | 概述 |
---|---|
down | 当前的server暂时不参与负载均衡 |
backup | 预留的备份服务器,其他主服务器挂了才会被用到 |
max_fails | 允许请求失败的次数 |
fail_timeout | 经过max_fails次失败后, 服务暂停的时间 |
max_conns | 限制最大的接收连接数 |
设置方式示例
1 |
|
负载均衡策略
算法名称 | 说明 |
---|---|
轮询 | 默认方式 |
weight | 权重方式 |
ip_hash | 依据ip分配方式 |
least_conn | 依据最少连接方式 |
url_hash | 依据URL分配方式 |
fair | 依据响应时间方式 |
示例
1 |
|
第四层负载均衡
需要安装stream模块,在配置参数的时候加上--with-stream
示例
stream和http块是平级的
1 |
|
集成缓存
Module ngx_http_proxy_module (nginx.org)
相关指令
例
1 |
|
清除缓存
方式1:删除对应的缓存目录(删除所有缓存)
1 |
|
方式2:ngx_cache_purge(删除指定缓存块的缓存)
安装ngx_cache_purge第三方模块
1 |
|
设置不缓存的资源
例
1 |
|
安全控制
Nginx使用SSL(Secure Sockets Layer),需要安装--with-http_ssl_module
模块
相关指令
Module ngx_http_ssl_module (nginx.org)
搭建集群
使用Keepalived来解决,Keepalived 软件由 C 编写的,最初是专为 LVS 负载均衡软件设计的,Keepalived 软件主要是通过 VRRP 协议实现高可用功能。
VRRP(Virtual Route Redundancy Protocol)
1 |
|
安装keepalived
创建keepalived目录,将压缩包解压到目录
1
tar -zxf 压缩包名 -C keepalived/
编译、安装
1
2
3
4
5
6
7cd keepalived/keepalived-2.0.20
# sysconf:配置文件所在目录
# prefix:keepalived要安装的目录
./configure --sysconf=/etc --prefix=/usr/local
make && make install
配置keepalived
配置文件在/安装目录/keepalived/keepalived.conf
1 |
|
1 |
|
启动keepalived
启动
1
2cd /安装目录/local/sbin
./keepalivedip a
查看ip,虚拟IP(VIP)会在MASTER节点上,当MASTER节点上的keepalived出问题以后,因为BACKUP无法收到MASTER发出的VRRP状态通过信息,就会直接升为MASTER。VIP也会”漂移”到新的MASTER。可以通过编写脚本来检测当nginx进程挂了的时候关闭keepalived,从而自动切换master到另一台nginx上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21# 编写脚本ck_nginx.sh,该脚本的意思是检查nginx进程是否在运行,
# 如果检测不到nginx进程,则关闭keepalived
#!/bin/bash
num=`ps -C nginx --no-header | wc -l`
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]; then
killall keepalived
fi
# 给脚本设置权限
chmod 755 ck_nginx.sh
# 在keepalived配置文件中添加对应的配置像
vrrp_script 脚本名称
{
script "脚本位置"
interval 3 #执行时间间隔
weight -20 #动态调整vrrp_instance的优先级
}
制作下载站点
将要被下载的资源放到一个目录下
修改nginx配置
1
2
3
4
5
6
7
8
9
10
11
12location /download{
# 被下载资源路径
root /usr/local;
# 开启功能
autoindex on;
# 是否展示文件大小
autoindex_exact_size on;
# 目录列表的格式,可以是html/xml/json/jsonp,默认html
autoindex_format html;
# 是否在目录列表上显示时间
autoindex_localtime on;
}
Lua
Lua是用标准C语言编写并以源代码形式开发。设计的目的是为了嵌入到其他应用程序中,从而为应用程序提供灵活的扩展和定制功能。应用在游戏开发、独立应用脚本、web应用脚本、扩展和数据库插件、系统安全上。
安装
下载压缩包到服务器
1
wget https://www.lua.org/ftp/lua-5.4.4.tar.gz
检查环境
1
make linux test
编译安装
1
make install
查看版本
1
lua -v
交互方式
1.交互式
通过命令lua
进入控制台
1 |
|
2.脚本式
创建hello.lua文件,编写脚本
1 |
|
基本语法
注释
单行注释
1 |
|
多行注释
1 |
|
多行注释只需要在前面多加一个-就可以取消
运算符
逻辑运算符
1 |
|
其他运算符
1 |
|
定义变量
默认为全局变量,定义局部变量要在前面加local
1 |
|
数据类型
1 |
|
表
可以用来表示数组、字典或混合使用,下标从1开始
1 |
|
方法
1 |
|
条件判断
if
1 |
|
循环
while
1 |
|
repeat
直到满足条件才退出循环
1 |
|
for
1 |
|
OpenResty
OpenResty是一个基于Nginx与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。OpenResty内部已经集成了Nginx和Lua。
安装
下载、解压压缩包 OpenResty - Download
1
2wget https://openresty.org/download/openresty-1.15.8.2.tar.gz
tar -zxvf openresty-1.15.8.2.tar.gz进入openresty目录,编译安装
1
2./configure
make && make install默认安装位置
/usr/local/openresty/nginx/
1
cd /usr/local/openresty/nginx/
测试
1
2
3
4
5
6
7
8
9
10
11
12# 修改配置
location /lua{
default_type 'text/html';
content_by_lua 'ngx.say("<h1>HELLO,OpenRestry</h1>")';
}
# 测试
./sbin/nginx -t
# 启动
./sbin/nginx
# 浏览器访问
使用ngx_lua指令
init_by_lua
1 |
|
init_worker_by_lua
1 |
|
set_by_lua
1 |
|
rewrite_by_lua
1 |
|
access_by_lua
1 |
|
content_by_lua
1 |
|
header_filter_by_lua
1 |
|
body_filter_by_lua
1 |
|
log_by_lua
1 |
|
balancer_by_lua
1 |
|
ssl_certificate_by_lua
1 |
|
例
1 |
|
ngx_lua操作Redis
1 |
|
ngx_lua操作mysql
1 |
|
具体应用
部署静态资源
将静态资源放到html文件夹下
反向代理
正向代理:代理的是客服端,对客服端负责,帮助客服端访问服务器
反向代理:代理的是服务端,对服务端负责,帮助服务器提供服务
1
2
3
4
5
6
7#在配置文件中的http中配置反向代理,记得开放端口
server {
listen 80; #监听的端口,当访问这个端口时转发到指定服务器
server_name localhost;
location / {
proxy_pass:http://192.168.229.130:8080; #转发的服务器
}负载均衡
基于反向代理,根据算法将用户请求分发到集群中的一台服务器上
名称 说明 weight 权重方式,值越大分配几率越高 ip_hash 根据客户端ip least_conn 根据最少连接方式 url_hash 根据url分配方式 fair 根据响应时间方式 轮询 默认 1
2
3
4
5
6
7
8
9
10
11
12
13#配置负载均衡
upstream targetserver{ #upstream指令来定义一组服务器
server 192.168.229.130:8080;
server 192.168.229.131:8080;
server 192.168.229.131:8081;
}
server {
listen 80; #监听的端口,当访问这个端口时转发到指定服务器
server_name localhost;
location / {
proxy_pass:http://targetserver; #转发的服务器
}
记得./sbin/nginx -s reload
重新加载