nginx基本配置语法

1.http相关

展示每次请求的请求头: curl -v http://www.baidu.com

2.nginx日志类型

  • error.log、 access.log
  • log_format

  *格式*

    syntax: log_format name [escape=default | json] string...;
    default: log_format combined "...";
    context:http

3.nginx变量

  nginx配置的内容:

    user  nginx;
    worker_processes  1;
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    events {
          worker_connections  1024;
    }
    http {
          include       /etc/nginx/mime.types;
          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  /var/log/nginx/access.log  main;

      sendfile        on;
          #tcp_nopush     on;

      keepalive_timeout  65;

       #gzip  on;

       include /etc/nginx/conf.d/*.conf;
    }

 
  变量类型:
    • http请求变量:arg_PARAMETER,http_header,sent_http_header
    • 内置变量:nginx内置的
    • 自定义变量: 自己定义

4.nginx模块

  • nginx官方模块
  • 第三方模块

  nginx开启的模块:

  --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
 

5.安装编译模块

编译选项 作用
--with-http_stub_status_module nginx的客户端状态
--with-http_random_index_module 目录中选择一个随机主页
--with-http_sub_module http内容替换
--limit_conn_module 连接频率限制
--limit_req_module 请求频率限制
http_access_module 基于ip的访问控制
http_auth_basic_module 基于用户的信任登录

5.1 http_stub_status_module 配置(nginx的客户端状态)

  配置语法:

    syntax: stub_status;
    default:-
    context:server, location

  在default.conf中添加:

    # my config
    location /mystatus {
        stub_status;
    }

  检查和重新启动配置:

    nginx -tc /etc/nginx/nginx.conf

  重启服务:

    nginx -s reload -c /etc/nginx/nginx.conf

5.2 http_random_index_module(目录中选择一个随机主页)

  配置语法:
    syntax: random_index on | off;
    default:random_index off;
    context:location
  
  在default.conf中将下面的配置:
    location / {
      root   /usr/share/nginx/html;
      index  index.html index.htm;
    }
  改为:
    location / {
      root   /usr/share/nginx/html;
      random_index on;
      #index  index.html index.htm;
    } 

5.3 http_sub_module (http内容替换)

  配置语法: 

 syntax: sub_filter string replacement;
 default:-
 context:http,server,location
 syntax: sub_filter_last_modified on | off (重要用户缓存)
 default: sub_filter_last_modified off;
 context: http,server,location
 syntax:   sub_filter_once on | off 
 default: sub_filter_once on;
 context: http,server,location

5.4 limit_conn_module(连接频率限制)

 配置语法:

 syntax: limit_conn_zone key zone=name:size;
default:-
context:http syntax:limit_conn zone number;
default:-
context:http, server, location 

5.5 limit_req_module (请求频率限制)

 配置语法:
  

syntax: limit_req_zone key zone=name:size rate=rate;
default: -
context: http
 压力测试:
  ab -n 50 -c 20 http://127.0.0.1/index.html
 -n 表示请求次数 -c 表示并发数
 
  在default.conf中将下面的配置:添加如下代码
  limit_conn_zone $binary_remote_addr zone=conn_zone:1m;
  limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
  
 压测结果:
  限制前如图:
  
  限制后如图:
   
  

5.6 http_access_module(基于ip的访问控制)

  配置语法:

  syntax: allow address | CIDR | unix: | all
  default:-
  context:http,server,location,limit_except

  syntax:deny address | CIDR | unix: | all
  default:-
  context:http, server, location ,limit_except

  测试 配置如下:

  location ~ ^/admin.html {
      root /opt/app/code;
      deny all;
      index index.html index.htm;
  }

5.7 

http_auth_basic_module(基于用户的信任登录)

  配置语法:

  syntax: auth_basic string | off;
  default: -
  context: http,server,location,limit_except

  syntax: auth_basic_user_file file;
  default: -
  context: http, server, location ,limit_except

 生成password文件:
 
htpasswd -c ./auth_conf feixia
 
 修改conf文件:
  location ~ ^/admin.html {
      root /opt/app/code;
      auth_basic "please input you user name and passwd";
      auth_basic_user_file /etc/nginx/auth_conf;
      index index.html index.htm;
  }
局限性
  • 用户信息依赖文件方式
  • 操作管理机械、效率低下
解决方案
  • nginx 结合LUA实现高效验证
  • nginx和LDAP打通,利用nginx-auth-ldap模块

学习nginx从入门到实践(四) 基础知识之nginx基本配置语法的更多相关文章

  1. 《Python编程:从入门到实践》基础知识部分学习笔记整理

    简介 此笔记为<Python编程:从入门到实践>中前 11 章的基础知识部分的学习笔记,不包含后面的项目部分. 书籍评价 从系统学习 Python 的角度,不推荐此书,个人更推荐使用< ...

  2. Nginx从入门到实践(四)

    Nginx常见问题和排错经验,实践应用场景中的方法处理Nginx安全,常见的应用层安全隐患,复杂访问控制,Nignx的sql防注入安全策略,Nginx的整体配置,搭建合理Nginx中间件架构配置步骤. ...

  3. 使用Code First建模自引用关系笔记 asp.net core上使用redis探索(1) asp.net mvc控制器激活全分析 语言入门必学的基础知识你还记得么? 反射

    使用Code First建模自引用关系笔记   原文链接 一.Has方法: A.HasRequired(a => a.B); HasOptional:前者包含后者一个实例或者为null HasR ...

  4. Ant学习-001-ant 基础知识及windows环境配置

    一.Ant 概要基础知识 Apache Ant 是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发,用以构建应用,或结合其他开源测试工具例如 git.T ...

  5. 学习nginx从入门到实践(五) 场景实践之静态资源web服务

    一.静态资源web服务 1.1 静态资源 静态资源定义:非服务器动态生成的文件. 1.2 静态资源服务场景-CDN 1.3 文件读取配置 1.3.1 sendfile 配置语法: syntax: se ...

  6. Nginx从入门到实践(一)

    结合实践.收集各种场景.常见问题,讲解Nginx中最实用的Webserver场景,提供一套整体的搭建配置方式 Nginx中间件,不局限于业务逻辑,有效独立于后台开发框架(不论后端是Java开发.PHP ...

  7. Nginx从入门到实践(二)

    静态资源web服务 静态资源类型 CDN CDN的基本原理是广泛采用各种缓存服务器,将这些缓存服务器分布到用户访问相对集中的地区或网络中,在用户访问网站时,利用全局负载技术将用户的访问指向距离最近的工 ...

  8. Nginx详解四:Nginx基础篇之目录和配置语法

    一.安装目录 命令:rpm -ql nginx 二.编译参数 命令:nginx -V 三.Nginx基本配置语法 修改主配置文件 当Nginx读配置文件读到include /etc/nginx/con ...

  9. APP测试入门篇之APP基础知识(001)

    前言        最近两月比较多的事情混杂在一起,静不下心来写点东西,月初想发表一遍接口测试的总结,或者APP测试相关的内容,一晃就月底了,总结提炼一时半会也整不完.放几个早年总结内部培训PPT出来 ...

随机推荐

  1. Spring 基于设值函数(setter方法)的依赖注入

    当容器调用一个无参的构造函数或一个无参的静态 factory 方法来初始化你的 bean 后,通过容器在你的 bean 上调用设值函数,基于设值函数的 DI 就完成了. 下述例子显示了一个类 Text ...

  2. poj1699 KMP+壮压DP

    Best Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6338   Accepted: 2461 Des ...

  3. hdu4746莫比乌斯反演进阶题

    Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others)Total S ...

  4. 【python爬虫】scrapy入门8:发送POST请求

    scrapy基础知识之发送POST请求与使用 FormRequest.from_response() 方法模拟登陆 https://blog.csdn.net/qq_33472765/article/ ...

  5. Istio的流量管理(实操二)(istio 系列四)

    Istio的流量管理(实操二)(istio 系列四) 涵盖官方文档Traffic Management章节中的inrgess部分. 目录 Istio的流量管理(实操二)(istio 系列四) Ingr ...

  6. 重学 Java 设计模式:实战建造者模式

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 乱码七糟 [luàn qī bā zāo],我时常怀疑这个成语 ...

  7. 读Pyqt4教程,带你入门Pyqt4 _012

    颜色 颜色是指一个代表红(Red).绿(Green).蓝(Blue)(RGB)强度值组合的对象,有效的RGB值在0~255之间.我们可以用多种方式定义颜色,最常用的是RGB十进制或者十六进制值.也可以 ...

  8. QueryRunner的添加与查询操作

    Apache-DBUtils实现CRUD操作,commmons-dbutils是Apache组织提供的开源JDBC工具类, 封装了针对于数据库的增删改查操作,Class QueryRunner Tes ...

  9. 基本的sql-select语句

    插入三张表: @d:/del_data.sql;                   @d:/hr_cre.sql;                   @d:/hr_popul.sql;select ...

  10. Parrot os配置源更新

    每次都是忘了怎么配置,去官网查文档,这记一下 一.源文件配置注意 首先要注意Parrot官方软件库的默认更新源文件不在 /etc/apt/sources.list 而是 /etc/apt/source ...