Nginx完整学习笔记(CentOS 7)

学习笔记 Alan 7年前 (2017-08-23) 7208次浏览 0个评论 扫描二维码

系统准备

Nginx安装

查看yum安装的内容

安装目录主要文件讲解

nginx -V查看编译时使用的参数

日志

error_log

access_log

可能过log_format来进行格式的定义

日志参数:

http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

常用配置参数(同样可通过nginx -V查看,以–with开头)

1. stub_status

重新加载配置文件后访问http://your.ip.address/mystatus即可获得类似如下信息

2. random_index

此时再访问网站首页,会随机展示/usr/share/nginx/html下的html文件

3. sub_filter

4. 连接、请求限制

limit_conn_module 连接频率限制
limit_conn_zone可在http中设置,limit_conn可在http, server, location中设置
limit_conn_zone key zone=name:size;
limit_conn zone number
limit_req_module 请求频率限制
limit_req_zone可在http中设置,limit_req可在http, server, location中设置
# key可使用变量如$binary_remote_addr代表针对IP进行限制,$binary_remote_addr比$remote_addr字节数少因而在这里使用,zone用于自定义名称供limit_req使用,rate代表指定时间内的请求数量如1r/s
limit_req_zone key zone=name:size rate=rate;
# burst代表将指定的请求数量放到下一秒执行, nodelay设置有无延时
limit_req zone=name [burst=number] [nodelay];

访问控制

基于IP的访问控制 http_access_module
#CIDR指网段,如192.168.1.0/24,unix:为socket,可在http, server, location, limit_except中进行设置
allow address | CIDR | unix: | all;
deny address | CIDR | unix: | all;

局限性:IP地址可能会发生变化,也可能通过代理来进行访问(ip_addr仅传递代理后的IP, http_x_forwarded_for会传递客户端和代理IP,但VPN有可能不遵循此协议)

基于用户登录认证 http_auth_basic_module

可在http, server, location, limit_except中进行设置

这种方法相对低效,企业方案可采用

Nginx结合LUA实现验证或Nginx和LDAP打通(需安装nignx-auth-ldap模块)

静态资源WEB服务

HTML,CSS,JS; JPEG,GIF,PNG; FLV,MPEG; TEXT等文件(CDN)
可在http, server, location, if in location中设置:
sendfile on | off
扩展:–with-file-aio异步文件读取
可在http, server, location中设置,用于提高网络包的传输效率,需开启sendfile:
tcp_nopush on | off;
可在http, server, location中设置,在keepalive下用于提高网络包的传输实时性:
tcp_nodelay on | off;
压缩, 可在http, server, location, if in location中设置:
gzip on | off;
默认压缩比为1,通过修改level来进行设置(压缩比越高,性能消耗越大),可在http, server, location中设置:
gzip_comp_level level;
控制http协议版本,主流和默认的为1.1,可在http, server, location中设置:
gzip_http_version 1.0 | 1.1;
http_gzip_static_module
http_gunzip_module #用于解决对gzip不支持的浏览器的问题,不常用

浏览器校验过期机制:
Expires, Cache-control(max-age)
协议中Etag头信息校验
Last-Modified头信息校验

Nginx完整学习笔记(CentOS 7)

示例配置

referer防盗链

Nginx代理服务

反向代理示例代码

正向代理示例代码

负载均衡调度器SLB

示例代码:

缓存配置

 

常用操作

常见问题

1. Job for nginx.service failed because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for details.

先执行setenforce 0再进行排查

喜欢 (0)
[]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址