nginx入门笔记

nginx是什么

可以理解为一个http服务器、反向代理服务器、imap/pop3/smtp代理服务器。

有什么用

http服务、反向代理、负载均衡。

怎么用

搭建方式

windows下安装方式如下

  • 下载
    http://nginx.org/
    选择的是nginx-1.13.10版本。

  • 解压压缩包到固定的文件夹
    这里写图片描述

  • 双击nginx.exe运行
    运行后访问http://127.0.0.1/ 看到如下页面
    这里写图片描述

nginx.conf配置

最简化的nginx.conf

# user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}

http {
    include       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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;

    server {
        listen       80;
        server_name  localhost;        
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }

}
配置详解

nginx.conf
nginx.conf有多个块组成,最外面的块是main,然后下一层是event和http。
main为全局设置,将影响其他所有设置。
其中,server继承main,location继承server。upstream跟任何块都没有关系。

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}

  • 全局块:配置影响nginx全局的指令。
  • events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
  • http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。
  • server块:配置虚拟主机的相关参数,一个http中可以有多个server。
  • location块:配置请求的路由,以及各种页面的处理情况。
main

全局配置包含5个。

user  nobody nobody;
worker_processes  2;
error_log  logs/error.log  notice;
pid        logs/nginx.pid;
worker_rlimit_nofile 65535;

events{
     use epoll;
     worker_connections      65536;
}
配置名示例值说明
userroot运行用户
worker_process2、autoworker个数
error_loglogs/error.log notice日志配置。有多个级别(debug、info、notice、warn、error、crit)
access_loglogs/access.log正常日志
pidlogs/nginx.pid进程存储位置
worker_rlimit_nofile65535打开文件数限制
events模块

events模块中包含nginx中所有处理连接的设置。

events {
    worker_connections 2048;
    multi_accept on;
    use epoll;
}

worker_connections设置可由一个worker进程同时打开的最大连接数。
multi_accept 告诉nginx收到一个新连接通知后接受尽可能多的连接。
use 设置用于复用客户端线程的轮询方法。

http

与提供http服务相关的一些配置参数。例如:是否使用keepalive啊,是否使用gzip进行压缩等。


http {
    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    access_log off;
    error_log /var/log/nginx/error.log crit;

    keepalive_timeout 10;
    client_header_timeout 10;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 10;

    limit_conn_zone $binary_remote_addr zone=addr:5m;
    limit_conn addr 100;


    include /etc/nginx/mime.types;
    default_type text/html;
    charset UTF-8;


    gzip on;
    gzip_disable "msie6";
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    open_file_cache max=100000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

}

配置名示例值说明
sendfileon开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,减少用户空间到内核空间的上下文切换
keepalive_timeout65长连接超时时间,单位是秒,这个参数很敏感,涉及浏览器的种类、后端服务器的超时设置、操作系统的设置,可以另外起一片文章了。
send_timeout60默认60,用于指定响应客户端的超时时间。这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。
client_max_body_size10m允许客户端请求的最大单文件字节数。如果有上传较大文件,请设置它的限制值。出现413 # Request entity too large一般是要调整这个配置。
client_body_buffer_size128k缓冲区代理缓冲用户端请求的最大字节数
access_log设置日志位置
error_log异常日志位置
server虚拟主机

http服务上支持若干虚拟主机。每个虚拟主机一个对应的server配置项,配置项里面包含该虚拟主机相关的配置。在提供mail服务的代理时,也可以建立若干server。每个server通过监听地址或端口来区分。

 server {
        keepalive_requests 120; #单连接请求上限次数。
        listen       80;   #监听端口
        server_name  127.0.0.1;   #监听地址       
        location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           #root path;  #根目录
           #index vv.txt;  #设置默认页
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 172.18.5.54; #允许的ip           
        } 
    }
配置名示例值说明
listen80监听端口,默认80,小于1024的要以root启动。可以为listen *:80、listen 127.0.0.1:80等形式。
server_namelocalhost服务器名,如localhost、www.example.com,可以通过正则匹配。
location

http服务中,某些特定的URL对应的一系列配置项。
root /var/www/html
定义服务器的默认网站根目录位置。如果locationURL匹配的是子目录或文件,root没什么作用,一般放在server指令里面或/下。

工作模式


如上图,nginx是典型的master-worker模型。其实时候会有一个master进程和多个worker进程。worker进程数可以配置。
master接收到外界信号,并向worker发送,其中包括监控worker状态的信号(当worker停止,会启动新的worker替代)。
具体另外文章详解。

日志压缩问题

每天网站访问的日志都写在一个文件里面,随着时间的推移,这个文件势必越来越大,最终成为问题。
可以写一个切割压缩脚本,对access_log和error_log进行切割压缩,使用linux定时任务。

gzip和缓存

为了提高网页的访问速度,减少带宽的消耗,我们可以设置nginx的gzip对一些资源进行压缩。

gzip on;    #开启gzip。默认为off
gzip_disable "msie6";    #禁用IE6
# gzip_static on;    
gzip_proxied any;    #无条件启用压缩
    # off - 关闭所有的代理结果数据的压缩
    #expired - 启用压缩,如果header头中包含 "Expires" 头信息
    #no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息
    #no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息
    #private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息
    #no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息
    #no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息
    #auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息

gzip_min_length 1000;    #压缩文件最小大小。小于该值不压缩
gzip_comp_level 4;    #压缩级别。1-9数字越大压缩越好,越占用cpu
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;    #进行压缩的文件类型。mime.types

同时也可以开启缓存,时间由具体而定。

location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ { 
        access_log   off; 
        expires      30d;
}

location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
    access_log   off;
    expires      24h;
}

location ~* ^.+\.(html|htm)$ {
        expires      1h;
}
https
server {
    #ssl参数
    listen              443 ssl;
    server_name         example.com;
    #证书文件
    ssl_certificate     example.com.crt;
    #私钥文件
    ssl_certificate_key example.com.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    #...
}

访问时公钥文件会分发到每一个客户端,而私钥文件在一个访问限制的文件夹中。只有master进程有读取权限。
当然https还有很多优化配置点,具体后续更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值