Nginx基础
nginx
版本区别
开源版本编译安装
Centos7
nginx-1.21.6
注意access.log文件的大小
上传tar包解压:tar zxvf nginx-1.21.6.tar.gz
安装依赖
yum install -y gcc / gcc-c++ pcre pcre-devel / zlib zlib-devel / gcc-c++
|
如果提示:./configure: error: the HTTP rewrite module requires the PCRE library.:参考
wget https://sourceforge.net/projects/pcre/files/pcre/8.43/pcre-8.43.tar.gz
tar -zxvf pcre-8.43.tar.gz
cd pcre-8.43
./configure --enable-utf8
make -j4 && make install
|
基本安装
mdir -p /apps/local/nginx/
./configure --prefix=/apps/local/nginx/
make -j4 && make install
|
安装示例
./configure --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_gzip_static_module \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre
|
参数讲解
--prefix= 指向安装目录。 --sbin-path= 指定执行程序文件存放位置。 --modules-path= 指定第三方模块的存放路径。 --conf-path= 指定配置文件存放位置。 --error-log-path= 指定错误日志存放位置。 --pid-path= 指定pid文件存放位置。 --lock-path= 指定lock文件存放位置。 --user= 指定程序运行时的非特权用户。 --group= 指定程序运行时的非特权用户组。 --builddir= 指向编译目录。 --with-rtsig_module 启用rtsig模块支持。 --with-select_module 启用select模块支持,一种轮询处理方式,不推荐在高并发环境中使用,禁用:--without-select_module。 --with-poll_module 启用poll模块支持,功能与select相同,不推荐在高并发环境中使用。 --with-threads 启用thread pool支持。 --with-file-aio 启用file aio支持。 --with-http_ssl_module 启用https支持。 --with-http_v2_module 启用ngx_http_v2_module支持。 --with-ipv6 启用ipv6支持。 --with-http_realip_module 允许从请求报文头中更改客户端的ip地址,默认为关。 --with-http_addition_module 启用ngix_http_additon_mdoule支持(作为一个输出过滤器,分部分响应请求)。 --with -http_xslt_module 启用ngx_http_xslt_module支持,过滤转换XML请求 。 --with-http_image_filter_mdoule 启用ngx_http_image_filter_module支持,传输JPEG\GIF\PNG图片的一个过滤器,默认不启用,需要安装gd库。 --with-http_geoip_module 启用ngx_http_geoip_module支持,用于创建基于MaxMind GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_module变量。 --with-http_sub_module 启用ngx_http_sub_module支持,允许用一些其他文本替换nginx响应中的一些文本。 --with-http_dav_module 启用ngx_http_dav_module支持,增加PUT、DELETE、MKCOL创建集合,COPY和MOVE方法,默认为关闭,需要编译开启。 --with-http_flv_module 启用ngx_http_flv_module支持,提供寻求内存使用基于时间的偏移量文件。 --with-http_mp4_module 启用ngx_http_mp4_module支持,启用对mp4类视频文件的支持。 --with-http_gzip_static_module 启用ngx_http_gzip_static_module支持,支持在线实时压缩输出数据流。 --with-http_random_index_module 启用ngx_http_random_index_module支持,从目录中随机挑选一个目录索引。 --with-http_secure_link_module 启用ngx_http_secure_link_module支持,计算和检查要求所需的安全链接网址。 --with-http_degradation_module 启用ngx_http_degradation_module 支持允许在内存不足的情况下返回204或444代码。 --with-http_stub_status_module 启用ngx_http_stub_status_module 支持查看nginx的状态页。 --without-http_charset_module 禁用ngx_http_charset_module这一模块,可以进行字符集间的转换,从其它字符转换成UTF-8或者从UTF8转换成其它字符。它只能从服务器到客户端方向,只有一个字节的字符可以转换。 --without-http_gzip_module 禁用ngx_http_gzip_module支持,同--with-http_gzip_static_module功能一样。 --without-http_ssi_module 禁用ngx_http_ssi_module支持,提供了一个在输入端处理服务器包含文件(SSI)的过滤器。 --without-http_userid_module 禁用ngx_http_userid_module支持,该模块用来确定客户端后续请求的cookies。 --without-http_access_module 禁用ngx_http_access_module支持,提供了基于主机ip地址的访问控制功能。 --without-http_auth_basic_module 禁用ngx_http_auth_basic_module支持,可以使用用户名和密码认证的方式来对站点或部分内容进行认证。 --without-http_autoindex_module 禁用ngx_http_authindex_module,该模块用于在ngx_http_index_module模块没有找到索引文件时发出请求,用于自动生成目录列表。 --without-http_geo_module 禁用ngx_http_geo_module支持,这个模块用于创建依赖于客户端ip的变量。 --without-http_map_module 禁用ngx_http_map_module支持,使用任意的键、值 对设置配置变量。 --without-http_split_clients_module 禁用ngx_http_split_clients_module支持,该模块用于基于用户ip地址、报头、cookies划分用户。 --without-http_referer_module 禁用ngx_http_referer_modlue支持,该模块用来过滤请求,报头中Referer值不正确的请求。 --without-http_rewrite_module 禁用ngx_http_rewrite_module支持。该模块允许使用正则表达式改变URI,并且根据变量来转向以及选择配置。如果在server级别设置该选项,那么将在location之前生效,但如果location中还有更进一步的重写规则,location部分的规则依然会被执行。如果这个URI重写是因为location部分的规则造成的,那么location部分会再次被执行作为新的URI,这个循环会被执行10次,最后返回一个500错误。 --without-http_proxy_module 禁用ngx_http_proxy_module支持,http代理功能。 --without-http_fastcgi_module 禁用ngx_http_fastcgi_module支持,该模块允许nginx与fastcgi进程交互,并通过传递参数来控制fastcgi进程工作。 --without-http_uwsgi_module 禁用ngx_http_uwsgi_module支持,该模块用来使用uwsgi协议,uwsgi服务器相关 --without-http_scgi_module 禁用ngx_http_scgi_module支持,类似于fastcgi,也是应用程序与http服务的接口标准。 --without-http_memcached_module 禁用ngx_http_memcached支持,用来提供简单的缓存,提高系统效率。 --without-http_limit_conn_module 禁用ngx_http_limit_conn_module支持,该模块可以根据条件进行会话的并发连接数进行限制。 --without-http_limit_req_module 禁用ngx_limit_req_module支持,该模块可以实现对于一个地址进行请求数量的限制 --without-http_empty_gif_module 禁用ngx_http_empty_gif_module支持,该模块在内存中常驻了一个1*1的透明gif图像,可以被非常快速的调用。 --without-http_browser_module 禁用ngx_http_browser_mdoule支持,创建依赖于请求报头的值 。如果浏览器为modern,则$modern_browser等于modern_browser_value的值;如果浏览器为old,则$ancient_browser等于$ancient_browser_value指令分配的值;如果浏览器为MSIE,则$msie等于1。 --without-http_upstream_ip_hash_module 禁用ngx_http_upstream_ip_hash_module支持,该模块用于简单的负载均衡。 --with-http_perl_module 启用ngx_http_perl_module支持,它使nginx可以直接使用perl或通过ssi调用perl。 --with-perl_modules_path= 设定perl模块路径 --with-perl= 设定perl库文件路径 --http-log-path= 设定access log路径 --http-client-body-temp-path= 设定http客户端请求临时文件路径 --http-proxy-temp-path= 设定http代理临时文件路径 --http-fastcgi-temp-path= 设定http fastcgi临时文件路径 --http-uwsgi-temp-path= 设定http scgi临时文件路径 --http-scgi-temp-path= 设定http scgi临时文件路径 --without-http 禁用http server功能 --without-http-cache 禁用http cache功能 --with-mail 启用POP3、IMAP4、SMTP代理模块 --with-mail_ssl_module 启用ngx_mail_ssl_module支持 --without-mail_pop3_module 禁用pop3协议。 --without-mail_iamp_module 禁用iamp协议。 --without-mail_smtp_module 禁用smtp协议。 --with-google_perftools_module 启用ngx_google_perftools_mdoule支持,调试用,可以用来分析程序性能瓶颈。 --with-cpp_test_module 启用ngx_cpp_test_module支持。 --add-module= 指定外部模块路径,启用对外部模块的支持。 --with-cc= 指向C编译器路径。 --with-cpp= 指向C预处理路径。 --with-cc-opt= 设置C编译器参数,指定--with-cc-opt="-I /usr/lcal/include",如果使用select()函数,还需要同时指定文件描述符数量--with-cc-opt="-D FD_SETSIZE=2048"。 (PCRE库) --with-ld-opt= 设置连接文件参数,需要指定--with-ld-opt="-L /usr/local/lib"。(PCRE库) --with-cpu-opt= 指定编译的CPU类型,如pentium,pentiumpro,...amd64,ppc64... --without-pcre 禁用pcre库。 --with-pcre 启用pcre库。 --with-pcre= 指向pcre库文件目录。 --with-pcre-opt= 在编译时为pcre库设置附加参数 。 --with-md5= 指向md5库文件目录。 --with-md5-opt= 编译时为md5库设置附加参数。 --with-md5-asm 使用md5汇编源。 --with-sha1= 指向sha1库文件目录。 --with-sha1-opt= 编译时为sha1库设置附加参数。 --with-sha1-asm 使用sha1汇编源。 --with-zlib= 指向zlib库文件目录。 --with-zlib-opt= 在编译时为zlib设置附加参数。 --with-zlib-asm= 为指定的CPU使用汇编源进行优化。 --with-libatomic 为原子内存的更新操作的实现提供一个架构。 --with-libatomic= 指向libatomic_ops的安装目录。 --with-openssl= 指向openssl安装目录。 --with-openssl-opt= 在编译时为openssl设置附加参数。 --with-debug 启用debug日志。
|
启动nginx
进入安装好的目录 /usr/apps/local/nginx/sbin
./nginx 启动 ./nginx -s stop 快速停止 ./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求 ./nginx -s reload 重新加载配置
|
firewall-cmd --zone=public --add-port=80/tcp --permanent
|
注册service文件
vim /usr/lib/systemd/system/nginx.service
|
内容
[Unit] Description=nginx - web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/apps/local/nginx/logs/nginx.pid ExecStartPre=/apps/local/nginx/sbin/nginx -t -c /apps/local/nginx/conf/nginx.conf ExecStart=/apps/local/nginx/sbin/nginx -c /apps/local/nginx/conf/nginx.conf ExecReload=/apps/local/nginx/sbin/nginx -s reload ExecStop=/apps/local/nginx/sbin/nginx -s stop ExecQuit=/apps/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
|
重新加载系统服务:
systemctl daemon-reload
启动并设置开机自启动
systemctl start nginx.service && systemctl enable nginx.service
|
查看nginx进程
docker安装
配置文件与应用场景
**目录结构 **
默认:client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp 临时文件:client_body_temp fastcgi_temp proxy_temp scgi_temp
|
conf 用来存放配置文件相关
html 用来存放静态文件的默认目录 html、css等
sbin nginx的主程序
access.log:日志文件
nginx.pid:主进程ID号
运行原理
最小配置
worker_processes
worker_processes 1; 默认为1,表示开启一个业务进程
worker_connections
worker_connections 1024; 单个业务进程可接受连接数
include mime.types;
include mime.types; 引入http mime类型。可以手动添加特定格式的问题。以什么方式展现在网页。
default_type application/octet-stream;
default_type application/octet-stream; 如果mime类型没匹配上,默认使用二进制流的方式传输。
sendfile on;
sendfile on; 使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝。
未开启sendfile
虚拟主机配置
server:可以开启多可nginx主机。以端口号区分
location / :配平ura,域名之后。加域名称之为url
root:html根目录
默认配置: server { listen 80; 监听端口号 server_name localhost; 主机名或者域名 location / { 匹配路径 root html; 文件根目录。此处的root表示根 index index.html index.htm; 默认页名称 } e rror_page 500 502 503 504 /50x.html; 报错编码对应页面 location 到 /50x.html location = /50x.html { root html; } }
|
虚拟主机
域名解析
server {:一个server 为一个虚拟主机
泛域名解析*.com
解析从上往下,先匹配那个则解析那个
*.com都解析到通一个ip地址
server_name :后面可以写多个域名
当a.com域名过来则到/apps/local/bolg下
server { listen 80; server_name a.com; location / { root /apps/local/bolg; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
}
|
同时解析到统一个ip
#user nobody; worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
sendfile on;
#虚拟主机A server { listen 80; server_name a.com;
location / { root /apps/local/bolg; index index.html index.htm; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
} #虚拟主机B server { listen 80; server_name b.com;
location / { root /apps/local/www; index index.html index.htm; }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
}
}
|
servername匹配规则
我们需要注意的是servername匹配分先后顺序,写在前面的匹配上就不会继续往下匹配了。
完整匹配
我们可以在同一servername中匹配多个域名
server_name vod.mmban.com www1.mmban.com;
|
通配符匹配
通配符结束匹配
正则匹配
server_name ~^[0-9]+\.mmban\.com$;
|
反向代理
字段:proxy_pass http://baidu.com;
• 网关、代理与反向代理
• 反向代理在系统架构中的应用场景
• Nginx的反向代理配置
• 基于反向代理的负载均衡器
• 负载均衡策略
流程图
配置
proxy_pass后面英文封号结尾
server { listen 80; server_name a.com; 域名
location / { proxy_pass http://mail.boloni.com; 代理与到的地址或者域名, # root /apps/local/bolg; # index index.html index.htm; }
|
负载均衡器
http {
# ... 省略其它配置
upstream tomcats { server 192.168.0.100:8080; server 192.168.0.101:8080; server example.com:8080; }
server { listen 80;
location / { proxy_pass http://tomcats; 结合上面upstream 后面的名称。 } }
# ... 省略其它配置 }
|
轮询策略
weight :权重比例
示例:
upstream tomcats { server 192.168.0.100:8080 weight=2; # 2/6次 server 192.168.0.101:8080 weight=3; # 3/6次 server 192.168.0.102:8080 weight=1; # 1/6次 }
|
Down:表示暂时不参与负载。不在访问
backup:b:备用机,没有可以访问的机器才会访问它
server 192.168.0.100:8080 weight=2 down ; server 192.168.0.100:8080 weight=2 backup;
|
不常用
ip_hash
根据客户端的ip地址转发同一台服务器,可以保持回话。
least_conn
最少连接访问
url_hash
根据用户访问的url定向转发请求
fair
根据后端服务器响应时间转发请求
动静分离
location匹配顺序
- 多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配
- 普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)
- 当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配
- 所有类型location存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配
配置反向代理
动态资源 location / { proxy_pass http://127.0.0.1:8080; root html; index index.html index.htm; }
|
增加每一个location
静态资源 location /css { root /usr/local/nginx/static; index index.html index.htm; } location /images { root /usr/local/nginx/static; index index.html index.htm; } location /js { root /usr/local/nginx/static; index index.html index.htm; }
|
使用正则
正则匹配 location ~*/(css|img|js) { root /usr/local/nginx/static; index index.html index.htm; }
|
location 前缀
/ 通用匹配,任何请求都会匹配到
= 精准匹配,不是以指定模式开头
~ 正则匹配,区分大小写
~* 正则匹配,不区分大小写
^~ 非正则匹配,匹配以指定模式开头的location
alias与root
root用来设置根目录,而alias在接受请求的时候在路径上不会加上location。
**alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;root指定
的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;使用
alias标签的目录块中不能使用rewrite的break(具体原因不明);另外,alias指定的目录后面必须要加上”/“符
号!!alias虚拟目录配置中,location匹配的path目录如果后面不带”/“,那么访问的url地址中这个path目录后
面加不加”/“不影响访问,访问时它会自动加上”/“; 但是如果location匹配的path目录后面加上”/“,那么访问的url地
址中这个path目录必须要加上”/“,访问时它不会自动加上”/“。如果不加上”/“,访问就会失败! root目录配置
中,location匹配的path目录后面带不带”/“,都不会影响访问 **
location /css { alias /usr/local/nginx/static/css; index index.html index.htm; }
|
测试
UrlRewrite
使用场景
域名变更
用户跳转 (从某个连接跳到另一个连接)
伪静态场景 (便于CDN缓存动态页面数据
URL重写语法
rewrite <regex> <replacement> [flag];
|
regex :可以使用正则或者字符串来表示相匹配的地址。 replacement:可以表示重定向的地址。 flag :flag标志的作用是用于控制当匹配到对应的rewrite规则后是否继续检查后续的rewrite规则。 flag值为如下四种,分别是:
|
last:停止处理当前的rewrite指令集,而后通过重写后的规则重新发起请求,浏览器地址栏URL地址不变。 break``:和``break``指令一样,都是停止处理当前上下文中的其他重写模块指令。 redirect:如果替换字符串不以“ http:``/``/``”,“ https:``/``/``”或“ $scheme” 开头,返回带有``302``代码的临时重定向,浏览器地址会显示跳转后的URL地址。 permanent:返回``301``代码的永久重定向,浏览器地址栏会显示跳转后的URL地址。
|
防盗链
valid_referers none | blocked | server_names | strings ....;
|
- none, 检测 Referer 头域不存在的情况。
- blocked,检测 Referer 头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值不以
“http://” 或 “https://” 开头。
- server_names ,设置一个或多个 URL ,检测 Referer 头域的值是否是这些 URL 中的某一个
在需要防盗链的location中配置
valid_referers 192.168.44.101; #域名或者IP if ($invalid_referer) { return 403; }
|
使用curl测试
curl -I http://192.168.1.101/img/logo.png
|
高可用配置
安装Keepalived
编译安装 :
wget https://www.keepalived.org/software/keepalived-2.2.8.tar.gz
|
yum 安装
yum install keepalived
/etc/keepalived/keepalived.conf
第一台机器: ! Configuration File for keepalived
global_defs { router_id nginx01 ID名
}
vrrp_instance SL { #实例名 state MASTER #主 interface eth0 #本机网卡名 virtual_router_id 51 priority 100 advert_int 1 authentication { #同一组的认证 auth_type PASS auth_pass 1111 } virtual_ipaddress { #虚拟网卡的地址 192.168.0.230 } }
|
另一台
! Configuration File for keepalived global_defs { router_id lb110 } vrrp_instance test(需要一致) { state BACKUP interface ens33 virtual_router_id 51 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.44.200 } }
|
HTTPs
集成部署LNMP环境
**证书安装 **
# 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; } }
|
80指向443
server { listen 80; server_name www.域名.com; rewrite ^(.*)$ https://${server_name}$1 permanent; } server { listen 443; server_name www.域名.com; root /home/wwwroot; ssl on; ssl_certificate /etc/nginx/certs/server.crt; ssl_certificate_key /etc/nginx/certs/server.key; .... }
|