zoukankan      html  css  js  c++  java
  • Nginx-基础篇

    Nginx-基础篇

    一、环境:

    • 系统硬件:CPU>=2Core,内存>=256M
    • 操作系统:CentOS 7.2 x64

    二、环境调试确认:

    1、四个确认

    1. 确认系统网络
      • ping www.baidu.com
    2. 确认yum可用
      • yum list
    3. 确认关闭iptables规则
      • iptables -L(查看是否有iptables规则)
      • iptables -F(关闭规则)
      • iptables -t nat -L(查看net表里有没有规则)
      • 如果net表中有规则可以执行:iptables -t nat -F
    4. 确认停用selinux
      • getenforce(查看selinux是否开启)
      • setenforce 0 (关闭selinux)

    2、两项安装

    • 安装gcc等:
      • yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
    • 安装基本工具:
      • yum -y install wget httpd-tools vim

    3、一次初始化

    • cd /opt;mkdir app download logs work backup
      • app:代码目录
      • download:网上下载的源码包
      • logs:自定义日志
      • work:shell脚本
      • backup:备份

    三、什么是Nginx:

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


    四、Nginx优势:

    1. IO多路复用epoll
    2. 轻量级
      • 功能模块少
      • 代码模块少
    3. CPU亲和(affinity)
      • 把CPU核心和Nginx工作进程绑定,把每个worker进程固定在一个cpu上执行,减少切换cpu的cache miss,获得更好的性能。
    4. sendfile
      • 把文件的传输只通过 kernel space传输给用户,不经过 user space

    五、Nginx的快速安装

    1. 进入官网 http://nginx.org/
    2. 点击 download
    3. 点击 Linux packages for stable version
    4. 修改/etc/yum.repos.d/nginx.repo,并添加官网指定内容

    注意:baseurl需要修改OS和OSRELEASE为你对应的服务器版本

    1. 直接 yum install nginx
    2. nginx -v 出现nginx的版本信息说明安装成功!

    六、Nginx的目录和配置语法

    • rpm -ql nginx:可以查询nginx安装的文件
    • 目录
      • /etc/logrotate.d/nginx:配置文件,Nginx日志轮转,用于logrotate服务的日志切割
      • /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/negix、/etc/sysconfig/negix-debug:配置文件,配置守护进程管理器的管理方式
      • /usr/lib64/nginx/modules、/etc/nginx/modules:目录,Nginx模块目录
      • /usr/sbin/nginx、/usr/sbin/nginx-debug:命令,Nginx服务的启动管理的终端命令
      • /var/cache/nginx:目录,Nginx的缓存目录
      • /var/log/nginx:目录,Nginx的日志目录
    • nginx -V:
    • 编译参数
      • 安装目的目录或路径
        • --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
      • 执行对应模块时,nginx所保留的临时性文件
        • --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
      • 设置额外的参数将被添加到CFLAGS变量
        • --with-cc-opt=parameters
      • 设置附加的参数,链接系统库
        • --with-ld-opt=parameters
      • 目录中随机选择一个主页
        • --with-http_random_index_module
      • HTTP内容替换
        • --with-http_sub_module
      • Nginx的客户端状态
        • --with-http_stub_status_module
    • Nginx默认配置语法
      • user:设置nginx服务的系统使用用户
      • worker_processes:工作进程数(最好跟cpu的数量保持一致)
      • error_log:nginx的错误日志
      • pid:nginx服务启动的pid
      • events:
        • worker_connections:每个进程允许最大连接数
        • use:工作进程数

    七、Nginx日志类型

    1. 包括了:error.log和access.log
    2. 通过nginx.conf配置文件中log_format来定义要记录的变量格式来记录日志
    3. 可以被记录到日志中的变量
      • HTTP请求变量
        • arg_PARAMETER:request请求的参数
        • http_HEADER:request请求的header
        • sent_http_HEADER:服务端返回的header
      • 内置变量
        • Nginx内置
      • 自定义变量

    八、Nginx模块

    nginx -tc /etc/nginx/nginx.conf:查询配置文件语法是否正确
    nginx -s reload -c /etc/nginx/conf:重启

    1. http_stub_status_module(展示Nginx相关信息)
      • 配置语法:stub_status
      • 默认:无
      • Context:server,location
    2. random_index_module
      • 配置语法:random_index on|off
      • 默认:random_index off
      • Context:location
    3. http_sub_module
      • sub_filter string replacement
        • default:无
        • string:需要被替换的字符串
        • replacement:替换的字符串
      • sub_filter_last_modified on|off
        • default:sub_filter_last_modified off
      • sub_filter_once on|off
        • default:sub_filter_once on
        • on:只匹配第一个,off:全局匹配

    注意:上述的Context:http,server,location

    1. limit_conn_module(连接频率限制)
      • limit_conn_zone
        • 配置语法:limit_conn_zone key zone=name:size
        • 默认:无
        • Context:http
      • limit_conn
        • 配置语法:limit_conn zone number
        • 默认:无
        • Context:http,server,location
    2. limit_req_module(请求频率限制)
      • limit_req_zone
        • 配置语法:limit_req_zone key zone=name:size rate=rate
        • 默认:无
        • Context:http
      • limit_req
        • 配置语法:limit_req zone=name [brust=number] [nodelay]
        • 默认:无
        • Context:http,server,location
    3. http_access_module(基于IP的访问控制)
      • allow
        • 配置语法:allow address|CIDR(网段)|unix:|all;
        • 默认:无
        • Context:http,server,location,limit_except
      • deny
        • 配置语法:deny address|CIDR(网段)|unix:|all;
        • 默认:无
        • Context:http,server,location,limit_except

    局限性:通过代理访问会失效

    • 可以使用http_x_forwarded_for
    • 结合geo模块
    • 通过http自定义变量传递
    1. http_auth_basic_module(基于用户的信任登录)
      • auth_basic
        • 配置语法:auth_basic string | off;
        • 默认:无
        • Context:http,server,location,limit_except
      • auth_basic_user_file
        • 配置语法:auth_basic_user_file filePath
        • 默认:无
        • Context:http,server,location,limit_except

    注意:file的格式是指定的,生成密码可以使用httpd-tools
    命令htpasswd -c filePath username

  • 相关阅读:
    POJ1769 Minimizing maximizer(DP + 线段树)
    ZOJ3201 Tree of Tree(树形DP)
    POJ3613 Cow Relays(矩阵快速幂)
    POJ3635 Full Tank?(DP + Dijkstra)
    ZOJ3195 Design the city(LCA)
    POJ3368 Frequent values(RMQ线段树)
    POJ3686 The Windy's(最小费用最大流)
    HDU4871 Shortest-path tree(最短路径树 + 树的点分治)
    POJ3013 Big Christmas Tree(最短路径树)
    Gym100685G Gadget Hackwrench(倍增LCA)
  • 原文地址:https://www.cnblogs.com/ronaldo-coder/p/8887958.html
Copyright © 2011-2022 走看看