Nginx---笔记一

环境调试确认:
1.确认系统网络
  >>ping www.baidu.com
2.确认yum可用
  >>yum list|grep gcc
3.确认关闭iptables
  >>iptables -L # 查看是否有iptables规则
  >>iptables -F # 关闭iptables规则
  >>iptables -t nat -L # 查看是否有规则
  >>iptables -t nat -F # 关闭规则
4.确认停用selinux
  >> getenforce # 查看selinux是否开启
  >> setenforce 0 # 关闭

  安装:
  >> yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake
  >> yum install -y wget httpd-tools vim
  一次初始化:
  >> cd /opt;mkdir app(代码目录) download(下载的源码包等) logs(自定义的日志) work(shell脚本) backup(默认的配置文件的备份等)

5.什么是Nginx:
  中间件:

  Nginx是一个开源且高性能、可靠的HTTP中间件、代理服务

  常见的HTTP服务:
    HTTPD-Apache基金会
    IIS-微软
    GWS-Google
  Nginx优势:
    1.IO多路复用epoll
      1.1 什么是IO复用:

      1.2什么是IO多路复用:
        多个描述符的I/O操作都能在一个线程内并发交替地顺序完成,这就叫I/O多路复用,这里的复用指的是复用同一个线程
        IO多路复用的实现方式:select,poll,epoll
        epoll:没有最大连接限制;每当FD就绪,采用系统的回调函数之间将fd放入,效率更高

        nginx采用的是epoll模型
   2.轻量级:功能模块少,代码模块化
   3.CPU亲和(affinity)
     3.1为什么需要CPU亲和:
       是一种把CPU核心和Nginx工作进程绑定方式,把每一个worker进程固定在一个cpu上执行,减少切换cpu的cache miss,获得更好的性能
   4.sendfile
     利用了linux在2.2之后出现的零拷贝模式,把所有文件的传输只通过内核空间传递给socket响应给用户
Nginx安装:
  Mainline version -开发版
  Stable version - 稳定版
  Legacy version -历史版本
  官网:
  http://nginx.org/
  参照官网:
    >> sudo yum install yum-utils
    >> vi /etc/yum.repos.d/nginx.repo
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    >> yum list | grep nginxy
    >> yum install nginx
    >> nginx -v # 查看安装版本
    >> nginx -V # 查看编译的参数
Nginx基本参数使用:
1. 安装目录
  >> rpm -ql nginx # 列出我们已经安装的服务所对应安装了哪些配置文件,以及在操作系统上新建了哪一些目录在操作系统上

路径: /etc/logrotate.d/nginx
类型: 配置文件
作用:Nginx日志轮转,用于logratate服务的日志切割

路径:/etc/nginx;/etc/nginx/nginx.conf(主配置文件);/etc/nginx/conf.d;/etc/nginx/conf.d/default.conf
类型:目录、配置文件
作用:Nginx主配置文件

路径:/etc/nginx/fastcgi_params;/etc/nginx/uwsgi_params;/etc/nginx/scgi_params
类型:配置文件
作用:cgi配置相关,fastcgi配置


路径:/etc/nginx/koi-utf;/etc/nginx/koi-win;/etc/nginx/win-utf;
类型:配置文件
作用:编码转换映射文件

路径:/etc/nginx/mime.types
类型:配置文件
作用:设置http协议的content-type与扩展名对应关系

路径:
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
类型:配置文件
作用:用于配置出系统守护进程管理器管理方式

路径:/usr/lib64/nginx/modules;
/etc/nginx/modules
类型:目录
作用:Nginx模块目录

路径:/usr/sbin/nginx;
/usr/sbin/nginx-debug
类型:命令
作用:Nginx服务的启动管理的终端命令

路径:
/usr/share/doc/nginx-1.12.0
/usr/share/doc/nginx-1.12.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
类型:文件、目录
作用:Nginx的手册和帮助文件

路径:/var/cache/nginx
类型:目录
作用:Nginx的缓存目录

路径:;/var/log/nginx
类型:目录
作用:Nginx的日志目录


2. 安装编译参数
>> nginx -V
"""
nginx version: nginx/1.16.0 # 版本
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) # gcc的版本
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled

configure arguments:
...

编译选项:
--prefix=/etc/nginx
--sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock
作用:
安装目的目录或路径


编译选项:
--http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp
作用:
执行对应模块时,Nginx所保留的临时性文件


编译选项:
--user=nginx
--group=nginx
作用:
设定Nginx进程启动的用户和组用户


编译选项:
--with-cc-opt=parameters
作用:
设置额外的参数将被添加到CFLAGES变量


编译选项:
--with-ld-opt=parameters
作用:
设置附加的参数,链接系统库

3. Nginx基本配置语法

>> vi /etc/nginx/nginx.conf
user nginx; # 设置nginx服务的系统使用用户
worker_processes 1; # 工作进程数

error_log /var/log/nginx/error.log warn; # nginx的错误日志,warn错误日志的级别
pid /var/run/nginx.pid; # nginx服务启动时候的pid


events {
worker_connections 1024; # 每个进程允许最大连接数
# use ; # 工作进程数,定义使用的内核模型select,poll,epoll
}


http {
include /etc/nginx/mime.types; # 子配置文件路径
default_type application/octet-stream;

# log_format 定义了日志类型
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;# main是logformat的name

sendfile on;
#tcp_nopush on;

# 设置客户端和服务端超时的时间
keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf; # 包含了此目录得所有.conf子配置文件
}

>> vi /etc/nginx/conf.d/default.conf
# 一个server配置一个独立的站点
server {
listen 80;
server_name baidu.com; # 用域名方式访问的域名的地址

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

# 控制者每一层路径的访问‘/’代表访问首页,定义默认访问的路径的一些配置
location / {
# 返回的页面的路径
root /usr/share/nginx/html;
# 定义首页默认的访问页面,如果index.html没有找到,就会去访问index.htm
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 /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
3.1.Nginx日志类型:
包括:
error.log # 记录nginx处理http请求的错误的状态以及nginx本身服务运行的错误状态
access_log
# 记录nginx每一次http请求的访问状态

这两个log的实现主要依赖于log_format的配置

Syntax: log_format name [escape=default[json]string....;]
Default: log_format combined "..."
Context: http

3.1.1 Nginx哪些变量可以放到日志中:
http请求变量-arg_PARAMETER,http_HEADER,send_http——HEADER(这个是response的head)
例如我要显示user-agent的信息,则可以在format中这样配置:
log_format main '$http_user_agent' '$remote_addr(表示客户端地址) - $remote_user(请求nginx认证的用户名)[$time_local(nginx的时间)] "$request(request头的请求行)" '
'$status(response返回的状态) $body_bytes_sent(服务端响应给客户端body信息的大小) "$http_referer(上一级页面的url地址)" '
'"$http_user_agent" "$http_x_forwarded_for"';
>> nginx -t(表示要检查配置文件是否正确) -c(路径) /etc/nginx/nginx.conf
>> nginx -s reload -c /etc/nginx/nginx.conf
>> ps -aux| grep nginx # 此时就可以看到nginx进行了一次重新加载

内置变量 - Nginx内置的 # 查看官网了解

自定义变量 - 自己定义的
4. 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_realip_module
--with-http_secure_link_module
--with-http_slice_module
--with-http_ssl_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
编译选项:
--with-http_stub_status_module
作用:
Nginx的客户端状态
配置语法:
Systax: stub_status;
Default: ---
Context: server, location
>> vim /etc/nginx/conf.d/default.conf
location /mystatus {
stub_status;
}
>> nginx -t -c /etc/nginx/nginx.conf
>> nginx -s reload -c /etc/nginx/nginx.conf
访问:
192.168.43.2:8080/mystatus

Active connections: 1
server accepts handled requests
9(握手的次数) 9(处理的连接数) 9(请求数)
Reading: 0 Writing: 1 Waiting: 0

编译选项:
--with-http_random_index_module
作用:
目录中选择一个随机主页

配置语法:

Syntax:random_index on | off;
Default: random_index off;
Context: location

>> vim /etc/nginx/conf.d/default.conf
location / {
root /opt/LearnNginx/app/code/;
random_index on;
#index index.html index.htm;
}

>> nginx -tc /etc/nginx/nginx.conf
>> systemctl reload nginx

编译选项:
--with-http_sub_module
作用:
HTTP内容替换
配置语法:
Syntax: sub_filter string replacement;
Default: ---
Context: http,server,location
eg:
location / {
root /opt/LearnNginx/app/code/;
# random_index on;
index index.html index.htm;
sub_filter '<a>imooc' '<a>IMOOC';
sub_filter_once off; # 替换所有
}
>> systemctl reload nginx
访问:192.168.43.2:8080/submodule.html

Syntax: sub_filter_last_modified on|off; # 用来判断是否有更新,如果有返回给用户最新的内容
Default: sub_filter_last_modified off;
Context: http, server, location

Syntax: sub_filter_once on|off; # 匹配html代码的第一个还是所有的
Default: sub_filter_once on;
Context: http,server,location;
第三方模块
5. nginx请求限制:
连接频率限制:limit_conn_module
配置语法:
Syntax: limit_conn_zone key zone=name:size
Default: --
Context:http

Syntax: limit_conn zone number
Default: ---
Context:http,server,location

请求频率限制:limit_req_module
配置语法:
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: ---
Context: http

Syntax: limit_req zone=name[burst=number] [nodelay];
Default: ---
Context: http,server,location

vi /etc/nginx/conf.d/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;
server {
listen 8080;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location /mystatus {
stub_status;
}

location / {
root /opt/LearnNginx/app/code/;
#limit_conn conn_zone 1;
#limit_req zone=req_zone burst=3 nodelay;
#limit_req zone=req_zone burst=3;
#limit_req zone=req_zone;
index index.html index.htm;
}
...
>> systemctl reload nginx
>> ab -n 50 -c 20 http://192.168.43.2:8080/index.html
# -n表示:总共发起的请求数
# -c表示:同时并发的请求

1.打开limit_req zone=req_zone;
>> nginx -s reload -c /etc/nginx/nginx.conf
>> ab -n 50 -c 20 http://192.168.43.2:8080/index.html
>> tail -f /var/log/nginx/error.log # 会发现有很多limiting requests打到日志中去


2.打开limit_req zone=req_zone burst=3 nodelay;
# burst=3,指超过限制之后遗留的三个放到下一秒执行,对客户端起到一个访问限速的作用,nodelay,其余直接返回503
>> nginx -s reload -c /etc/nginx/nginx.conf
>> ab -n 50 -c 20 http://192.168.43.2:8080/index.html
>> tail -f /var/log/nginx/error.log #会发现少了三个被放进错误日志中

3.打开limit_conn conn_zone 1 # 连接的服务端同一时刻只允许一个ip的连接过来
6. nginx的访问控制
基于IP的访问控制 - http_access_module
配置语法:
Syntax: allow address | CIDR(网段) | unix:(linux/unix中的socket访问方式) | all
Default: ---
Context: http,server,location,limit_except

Syntax: deny address |CIDR | unix: | all
Default: --
Context: http,server,location,limit_except
eg:
>> mv default.conf access_mod.conf
>> vi access_mod.conf
# ~:做一个模式的匹配
location ~ ^/admin.html {
root /opt/LearnNginx/app/code/;
deny 112.97.61.13; # 限制我自己的ip不能访问
allow all; # 其他的ip可以访问
index index.html index.htm;
}
访问:www.ip138.com # 查询出口ip

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

http_access_module局限性:
方法一:采用别的HTTP头信息控制访问,如:HTTP_X_FORWARD_FOR
方法二:结合geo模块做
方法三:通过http自定义变量传递

基于用户的信任登录 - http_auth_basic_module
Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http,server,location,limit_except

Syntax: auth_basic_user_file file;
Default: ---
Context: http,server,location,limit_except

>> yum isntall httpd-tools -y
>> mv access_mod.conf auth_mod.conf
>> htpasswd -c ../auth_conf phantom # 生成密码文件
>> vi /etc/nginx/conf.d/auto_mod.conf
location ~ ^/admin.html {
root /opt/LearnNginx/app/code;
auth_basic "Please input you password:"
auth_basic_user_file /etc/nginx/auth_conf;
index index.html index.htm;
}
>> nginx -tc /etc/nginx/nginx.conf
访问:xxx
局限性:
1.用户信息依赖文件方式
2.操作管理机械,效率低下
解决方案:
1.Nginx结合LUA实现高效验证
2.Nginx和LADP打通,利用nginx-auth-ldap模块

 

转载于:https://www.cnblogs.com/wgwblogs/p/11172165.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值