Nginx基础配置

yum安装,默认配置文件为/etc/nginx/nginx.conf

  • Main位于nginx.conf配置⽂文件的最⾼高层
  • Main层下可以有Event、HTTP层
  • HTTP层下⾯面有允许有多个Server层, ⽤用于对不不同的⽹网站做不不同的配置
  • Server层也允许有多个Location, ⽤用于对不不同的路路径进⾏行行不不同模块的配置
[root@localhost ~]# cat /etc/nginx/nginx.conf

user nginx;  #设置nginx的服务用户
worker_processes 1; #工作进程  等于cpu的个数

error_log /var/log/nginx/error.log warn;             #错误日志
pid /var/run/nginx.pid; #pid
 

events {
worker_connections 1024; #每个worker进程支持的最大连接数

use epoll; #内核模型 select poll epoll
}


http {
        include /etc/nginx/mime.types;
       default_type application/octet-stream;
       server {
            listen   80;
            server_name   www.jcwit.com;
       location    /  {
           root   /data/html  ;
           index  index.html;     
        }  
        error_page   500 502 503  /50x.html;
        location  = /50x.html   {
            root  /html;      
} 

}  
}

Nginx 日志配置

配置在http字段内

log_format main   '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

Nginx日志变量:

  • $remote_addr 客户端地址
  • $remote_user http客户端请求nginx认证用户名
  • $time_local nginx服务器时间
  • $request 请求行 GET POST等方法 http协议版本
  • $status response返回状态码 比如200
  • $body_bytes_sent 从服务端响应给客户端body信息⼤大⼩小
  • $http_referer http上⼀一级⻚页⾯面, 防盗链、⽤用户⾏行行为分析
  • $http_user_agent http头部信息, 客户端访问设备
  • $http_x_forwarded_for http请求携带的http信息

Nginx状态监控

–with-http_stub_status_module 该模块记录

配置在server或者location字段

location /mystatus {
      stub_status on;
      access_log off;

}

Nginx_status概述
Active connections:2
server accepts handled requests
16 16 19
server表示Nginx处理理接收握⼿手总次数。
accepts表示Nginx处理理接收总连接数。 请求丢失数=(握⼿手数-连接数)可以看出,本次状态显示没有丢失请求。
handled requests,表示总共处理理了了19次请求。
Reading Nginx读取数据
Writing Nginx写的情况
Waiting Nginx开启keep-alive⻓长连接情况下, 既没有读也没有写, 建⽴立连接情况

Nginx访问限制

连接频率限制 limit_conn_module

请求频率限制 limit_req_module

连接限制配置

http {
//http段配置连接限制, 同⼀一时刻只允许⼀一个客户端IP连接
 limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
 server { 
         location / {
             //同⼀一时刻只允许⼀一个客户端IP连接 
                limit_conn conn_zone 1;
                     }
            }
}

请求限制配置

http {
//http段配置请求限制, rate限制速率,限制⼀一秒钟最多⼀一个IP请求 
       limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
     server {
            location / { 
             //1r/s只接收⼀一个请求,其余请求拒绝处理理并返回错误码给客户端
              limit_req zone=req_zone;
             //请求超过1r/s,剩下的将被延迟处理理,请求数超过burst定义的数量量, 多余的请求返回503#
               limit_req zone=req_zone burst=3 nodelay;}

Nginx访问控制

基于IP的访问控制 http_access_module

基于⽤用户登陆认证 http_auth_basic_module

基于IP的访问控制

location ~  / {
 root html;
 index index.html;
 deny 192.168.1.1;
 allow all;
}

基于用户认证

首先创建认证用户 auth_conf为保存的文件名 admin为用户名 密码会提示输入

htpasswd -c auth_conf admin

配置

在需要认证的地方插入以下内容 可以是http server或者location

auth_basic "Auth access Blog Input your Passwd!"; 
auth_basic_user_file /etc/nginx/auth_conf;

Nginx基础配置
http://www.jcwit.com/article/43/
作者
Carlos
发布于
2019年7月12日
许可协议