NGINX 1

时间:2020-01-09 19:04:58   收藏:0   阅读:106

nginx

模型

编译安装步骤

#编译工具
yum -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate
gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel nettools
iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2
libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed make

# 下载包
cd 
wget http://nginx.org/download/nginx-1.16.1.tar.gz

#解压
tar xf  nginx-1.16.1.tar.gz

#如果有需要额外模块,需提前下载到当前目录例echo模块,编译的时候,--add-module=模块名,没有需求则跳过此步
wget https://github.com/openresty/lua-nginx-module



# 编译,指定安装目录 用户 等~
cd nginx-1.16.1 
./configure --prefix=/data/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-debug

#--add-module=/root/lua-nginx-module (额外模块)

make && make install 

配置文件

#配置文件路径
/data/nginx/conf/nginx.conf 

user  nginx; # 运行用户,编译安装需要自己创建(groupadd -g 80 nginx ;useradd -r -u 80  -g  80  -s /sbin/nologin nginx)

worker_processes  1; #工作进程数量, 可设定worker_processes auto;(更具核心数量开启工作进程,且不会切换到别的cpu核心上)也可以绑定,例4个核心 worker_processes 0001 0010 0100 1000;


error_log  logs/error.log; # 错误日志 可基于不同级别定义多个错误日志文件,例如error_log  logs/error.log  notice;

pid        logs/nginx.pid; # nginx 的进程id 可设置/usr/lib/systemd/system/下定义nginx.service 由systemctl 来启动

events {  #事件模型配置参数
    worker_connections  1024;# 定义每个工作进程处理多少个连接数,默认1024个可设置更多, 但是超过固定数量,需要修改内核参数以提供支持
}

http{

include ....; # 引用外部文件,引用外部文件有前后关系,调用需要在引用之后 例调用配置文件,但是配置文件需要使用其他全局参数的时候, 如果调用在引用文件前,将会导致引用失败

default_type  application/octet-stream; #这个类型会让浏览器认为响应是普通的文件流,并提示用户下载文件,兜底的,如果有不知道的文件类型就下载,这里可以设置上传或下载的大小 且location /download  的时候也默认为这个选项,让用户下载

log_format  onemat  ... ;  # 定义日志格式,可创建多个。 全局创建或者server , location 中创建, 关系是父级调用关系, 即: http > server > location ,子级可调用父级定义的日志格式, 

access_log  logs/access.log  main; #成功日志 调用格式可设置全局, server  ,location
sendfile no; #零拷贝,需要很大篇幅讲,大概就是说省去一些不必要的内核空间与用户空间的内存交互

keepalive_timeout  65; #持久连接 超时时长,可在后面 keepalive_timeout  65 20; 用户看到的就是后面定义的20 隐藏了65 

gzip on ; #传输压缩功能 开启后还有压缩级别的调动1~9 个压缩比,也可设置文件多大开开始压缩功能

    server { # 全局下可定义多个例如定义多个域名后,设置server_name 即可实现pc端 移动端的分开调用

    listen 80; # 监听端口。 可自定义, 每个server 都可定义不同的端口

    server_name  localhost; #设置访问名 用于区分访问的哪一个server_name 相同会报警告 
    charset utf-8 # 设定默认编码格式,默认为俄罗斯的编码,可修改为其他编码格式 

        location / {  #location 相当于一个房间里面的多个门,每个门就是对于不同URI的匹配,且匹配方式也有多种, 分别是 精确匹配 =  基础匹配 /  正则表达式 ~ 等。。。

        root /alias  html  ; # 指定访问目录,可自定义绝对路径例: root    /data/nginx/imags,alias 用的很少
        index  index.html; # 设置默认访问的文件
        deny  all ; # 拒绝访问/ allow 允许访问
        }

        error_page 404 /404.html; # 定义错误状态码访问页 与下面的location不能分离
        location /404.html { 
            root   html;
            index  index.html;
        }

    }

}

包含关系


include ...;
http{
    include ...;
    server{
        
        location 1 {
            ...
        }
        location 2 {
            ...
        }
    }

    server{
        location 1 {
            ...
        }
        location 2 {
            ...
        }
        
    }

}

# include 引用可在多个地方, 如在http外 则与http同级,如果在http内,则与http内的server 同级,默认关系http包含server 包含location  ,且server ,location 都可有多个

状态页


location /nginx_status {
stub_status;
allow 192.168.0.0/16;
allow 127.0.0.1;
deny all;
}

curl http://127.0.0.1/nginx_status 即可

json 日志

#创建
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';

# 调用
access_log /apps/nginx/logs/access_json.log access_json;


Nginx 压缩功能


http{

    ...
    #启用或禁用gzip压缩,默认关闭
    gzip on | off;
    #压缩比由低到高从1到9,默认为1
    gzip_comp_level level;
    #禁用IE6 gzip功能
    gzip_disable "MSIE [1-6]\.";
    #gzip压缩的最小文件,小于设置值的文件将不会压缩
    gzip_min_length 1k;
    #启用压缩功能时,协议的最小版本,默认HTTP/1.1
    gzip_http_version 1.0 | 1.1;
    #指定Nginx服务需要向服务器申请的缓存空间的个数*大小,默认32 4k|16 8k;
    gzip_buffers number size;
    #指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错
    gzip_types mime-type ...;
    #如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”
    gzip_vary on | off;
    ...
}

https

server{
listen 80 ;
listen 443 ssl;
ssl_certificate /apps/nginx/certs/www.xhg.zone.crt;
ssl_certificate_key /apps/nginx/certs/www.xhg.zone.key;
}   

隐藏web服务器版本


#编译之前,改变这个文件14行后编译 
vim +14  root/nginx-1.16.0/src/core/nginx.h 
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!