Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务。
配置解析
# 全局配置
...
# 配置影响nginx服务器或与用户的网络连接
events {
}
# 配置代理
http {
# 虚拟主机配置
server {
listen # 监听端口
server_name # 指定虚拟主机域名
# 配置请求的路由,以及各种页面的处理情况
location / {
root # 定义服务器的默认网站根目录位置
index # 定义首页索引文件的名称
proxy_pass #
}
location /test {...}
}
server {
...
}
# 配置后端服务器地址
upstream name {
# least_conn、 fair 最快响应时间策略、 ip_hash
}
}
server_name
精确匹配 > 左侧通配符匹配 > 右侧通配符匹配 > 正则匹配
精确 www.dxx.com
左通配符 *.dxx.com
右通配符 www.dxx.*
正则 ~^www\.nginx\.*$
root 与 alias
root 可以在 http 、server、location 等配置,alias 只能在 location 中
location /image {
root /web/image;
}
location /image {
alias /web/image/; # 最后加 /
}
访问 www.test.com/image/1.png 实际找服务器 /web/image/1.png
location 语法
格式:location [修饰符] 匹配模式 {...}
nginx location 匹配的优先级:= > ^~ > ~/~* > 不带任何修饰符
- 没有修饰符 表示路径前缀匹配
server {
server_name www.dxx.com
location /test {}
}
匹配 www.dxx.com/test www.dxx.com/test/may
访问 xx/test 时,先找 test 下的 index.html
location /test {} 找不到,继续找 test 文件
location /test/ {} 找不到,不会找 test 文件
=精确匹配
server {
server_name www.dxx.com
location = /test {}
}
www.dxx.com/test
~正则匹配区分大小写
server {
server_name www.dxx.com
location ~ ^/test$ {}
}
匹配 www.dxx.com/test 不匹配 www.dxx.com/TEST
~*正则匹配不区分大小写^~该符号后面的字符是最佳匹配,采用该规则,不再进行后续查找
return
location 后续指令不会执行,直接返回 响应码或 URL
rewrite
根据正则规则重写 URL
语法:rewrite 正则 替换内容 [flag]
flag:
last 重写后继续匹配
break 使用重写后的 URL ,不在匹配其他 location
redirect 302 临时重定向
permanent 301 永久重定向
location /search {
rewrite ^/(.*) https://www.baidu.com redirect;
}
location /images {
rewrite /images/(.*) /pics/$1;
}
location /pics {
rewrite /pics/(.*) /photos/$1;
}
location /photos {}
访问 www.dxx.com/search -> https://www.baidu.com
访问 www.dxx.com/images/1.png -> www.dxx.com/pics/1.png -> www.dxx.com/photos/1.png
若改为 rewrite /images/(.*) /pics/$1 break; 就变成 www.dxx.com/images/1.png -> www.dxx.com/pics/1.png
if
条件判断
= 相等
~/~* 正则
-f 检查文件存在
-d 检查目录存在
-e 检查文件、目录、链接存在
-x 检查文件可执行
条件不满足前面加 ! 判断
// pc 手机端公用一套代码怎么区分
server {
location / {
//移动、pc设备agent获取
if ($http_user_agent ~* '(Android|webOS|iPhone)') {
set $mobile_request '1';
}
if ($mobile_request = '1') {
rewrite ^.+ http://m.baidu.com;
}
}
}
autoindex
用户请求以 / 结尾时,列出目录结构,可以用于快速搭建静态资源下载网站
location /download/ {
root /opt/source;
autoindex on; # 打开
autoindex_exact_size on; # 修改为off,以KB、MB、GB显示文件大小,默认为on,以bytes显示出⽂件的确切⼤⼩
autoindex_format html; # 以html的方式进行格式化,可选参数有 html | json | xml
autoindex_localtime off; # 显示的⽂件时间为⽂件的服务器时间。默认为off,显示的⽂件时间为GMT时间
}
变量
| 变量 | 功能 |
|---|---|
| $host | 请求信息中的Host,如果请求中没有Host行,则等于设置的服务器名 |
| $request_method | 客户端请求类型,如GET、POST |
| $remote_addr | 客户端的IP地址 |
| $remote_port | 客户端的端口 |
| $uri | 请求的 URL,不包含参数 |
| $request_uri | 请求的 URL,包含参数 |
| $args | 请求中的全部参数 |
| $content_length | 请求头中Content-Length |
| $http_user_agent | 客户端 agent 信息 |
| $http_cookie | 客户端 cookie 信息 |
| $http_via | 每经过一层代理服务器,都会添加相应的信息 |
| $server_protocol | 请求使用的协议,如HTTP/1.0、HTTP/1.1 |
| $server_addr | 服务器地址 |
| $server_name | 服务器名称 |
| $server_port | 服务器端口号 |
upstream
{
upstream name {
server # 服务器地址
keepalive # 启动长连接
keepalive_requests # 一个长连接最多请求 HTTP 个数
keepalive_timeout # 空闲时,长连接的超长时间
# 负载均衡相关
hash # 哈希
ip_hash # 根据 IP 进行哈希计算
least_conn # 最少连接数
least_time # 最短响应时间
random # 随机
}
}
- server
语法:server address [parameters]
parameters 取值:
weight=number 权重值,默认为 1; max_conns=number 上游服务器的最大并发连接数; fail_timeout=time 服务器不可用的判定时间; max_fails=numer 服务器不可用的检查次数; backup 备份服务器,仅当其他服务器都不可用时才会启用; down 标记服务器长期不可用,离线维护;
proxy_pass
location /test/ {
proxy_pass: http://127.0.0.0:8080;
}
location /test/ {
proxy_pass: http://127.0.0.0:8080/;
}
URL 最后不带 / 给上游的URL不会改变 -> http://127.0.0.0:8080/test/abc/t.html
URL 最后带 / 给上游的URL会改变 -> http://127.0.0.0:8080/abc/t.html
应用
正向代理:代理客户端,服务器不知道实际发起请求的客户端。
反向代理:代理服务器端,客户端不知道实际的服务器端。
好处:隐藏真实服务器;负载均衡;动静分离,提升系统健壮性;
反向代理
location /api {
# 请求host传给后端
proxy_set_header Host $http_host;
# 请求ip 传给后端
proxy_set_header X-Real-IP $remote_addr;
# 请求协议传给后端
proxy_set_header X-Scheme $scheme;
# 路径重写
rewrite /api/(.*) /$1 break;
# 代理服务器
proxy_pass http://localhost:9000;
}
gzip 压缩
# 配置 GZip 压缩
gzip on;
# 支持压缩的资源格式
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# HTTP 最低版本
gzip_http_version 1.1;
gzip_comp_level 6;
# 默认 off,nginx做为反向代理时启用,用于设置启用或禁用从代理服务器上收到相应内容 gzip 压缩;
gzip_proxied any;
# 用于在响应消息头中添加 Vary:Accept-Encoding,使代理服务器根据请求头中的 Accept-Encoding 识别是否启用 gzip 压缩;
gzip_vary on;
# 默认 off,该模块启用后,Nginx 首先检查是否存在请求静态文件的 gz 结尾的文件,如果有则直接返回该 .gz 文件内容;
gzip_static on;
# 只有大于此长度的进行压缩
gzip_min_length 1000;
gzip_buffers 32 8k;
负载均衡
- 默认轮询
upstream backend {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
}
- 权重
upstream backend {
server 127.0.0.1:3000 weight=2 ;
server 127.0.0.1:3001 weight=1;
}
- 备份服务器,主服务器不可用时与备份服务器连接
upstream backend {
server 127.0.0.1:3000 backup;
server 127.0.0.1:3001;
}
- hash、ip_hash、least_conn
upstream backend {
hash $request_uri;
server 127.0.0.1:3000 backup;
server 127.0.0.1:3001;
}
upstream backend {
ip_hash;
server 127.0.0.1:3000 backup;
server 127.0.0.1:3001;
}
upstream backend {
least_conn;
server 127.0.0.1:3000 backup;
server 127.0.0.1:3001;
}
缓存配置
# 设置缓存存放路径 目录层级 共享内存 指定时间没访问清理
proxy_cache_path /etc/nginx/cache_temp levels=2:2 keys_zone=cache_zone:30m max_size=2g inactive=60m use_temp_path=off;
upstream cache_server{
server 121.42.11.34:1010;
server 121.42.11.34:1020;
}
server {
listen 80;
server_name cache.lion.club;
location / {
proxy_cache cache_zone; # 设置缓存内存,上面配置中已经定义好的
proxy_cache_valid 200 5m; # 缓存状态为200的请求,缓存时长为5分钟
proxy_cache_key $request_uri; # 缓存文件的key为请求的URI
add_header Nginx-Cache-Status $upstream_cache_status # 把缓存状态设置为头部信息,响应给客户端
proxy_pass http://cache_server; # 代理转发
}
}
# 条件判断不缓存
server {
listen 80;
server_name cache.lion.club;
# URI 中后缀为 .txt 或 .text 的设置变量值为 "no cache"
if ($request_uri ~ \.(txt|text)$) {
set $cache_name "no cache"
}
location / {
proxy_no_cache $cache_name; # 判断该变量是否有值,如果有值则不进行缓存,如果没有值则进行缓存
proxy_cache cache_zone; # 设置缓存内存
proxy_cache_valid 200 5m; # 缓存状态为200的请求,缓存时长为5分钟
proxy_cache_key $request_uri; # 缓存文件的key为请求的URI
add_header Nginx-Cache-Status $upstream_cache_status # 把缓存状态设置为头部信息,响应给客户端
proxy_pass http://cache_server; # 代理转发
}
}
HTTPS
server {
# ssl参数
listen 443 ssl; //监听443端口,因为443端口是https的默认端口。80为http的默认端口
server_name example.com;
# 证书文件存放地址
ssl_certificate example.com.crt;
# 私钥文件
ssl_certificate_key example.com.key;
}
常用命令
# 陌生环境找 nginx 配置文件
ps -ef | grep nginx 找可执行文件路径
nginx -t 检查时会显示主配置文件路径
# nginx 环境变量配置
vim /etc/profile
# 添加内容 begin
PATH=$PATH:/usr/local/nginx/sbin
export PATH
# end
# 生效配置
source /etc/profile
# 检查配置是否有问题
nginx -t
# 重新加载配置文件
nginx -s reload
# 重启
nginx -s reopen
#关闭
nginx -s stop
# 等待工作进程处理完成后关闭
nginx -s quit
systemctl enable nginx
systemctl start nginx
systemctl stop nginx
systemctl rstart nginx
systemctl reload nginx
systemctl status nginx
ps -ef | grep nginx
kill -9 pid