nginx学习笔记

# 一、常见服务器
Apache \ IIS \ Tomcat \ Lighttpd \ Nginx

# 二、nginx.conf文件的结构

```
...              #全局块

events {         #events块
   ...
}

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

## 2.1 全局块
通常包括配置运行Nginx服务器的用户(组)、允许生成的worker_process数、Nginx进程PID存放路径、日志存放路径和类型以及配置文件的引入等

``` user user  [group]; ```

``` user  nobody  nobody; ``` :所有用户都可启动Nginx进程

## 2.2 events块
涉及的指令主要影响Nginx与用户的网络连接。
常用设置包括:

是否开启对多worker_process下的网络连接进行序列化:``` accept_mutex on | off; ```

是否允许同时接收多个网络连接: ``` multi_accept on | off; ```

选取哪种事件驱动模型处理连接请求: ``` use method(select\poll\kqueue\epoll\rtsig\eventport); ```

每个worker_process可以同时支持的最大连接数等: ``` worker_connections 512; ```

## 2.3 http块
是Nginx配置重要部分!代理、缓存、日志定义等绝大部分和第三方模块配置。

http全局块中配置指令包括:

文件引入: ``` include  mime.type; ```

MIME-Type定义: ``` types{  ...  } ```通过include引入mime类型定义

日志自定义: ``` access_log  logs/access.log   combined; ```

是否使用sendfile传输文件: ``` sendfile on | off; ```

连接超时时间: ``` keepalive_timeout  timeout  [header_timeout]; ```
    
    timeout:服务器端对连接的保持时间  默认75s
    header_timeout:在应答报文头部的Keep-Alive域设置超时时间“Keep-Alive:timeout=header_timeout”

单连接请求数上限: ``` keepalive_requests  number;  ```
    
    用于限制用户通过某一连接向Nginx服务器发送请求的次数,默认为100

## 2.4 server块
与虚拟主机关系密切!

server全局块中,常见配置项有:虚拟主机监听配置、虚拟主机的名称和IP配置。

## 2.5 location块
每个sever可以包含多个location块!

主要作用:对接收到的请求字符串(除虚拟主机名称或IP之外)进行匹配,对特定的请求进行处理

语法结构:``` location [ = | ~ | ~* | ^~ ] uri { ... } ```

    "=" : 严格匹配,如果匹配成功就停止向下搜索并立即处理请求
    "~" : 包含,区分大小写
    "~*" : 包含,不区分大小写
    
    
下面是最简单的配置
```
user  nobody;
worker_processes  3;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;

pid        nginx.pid;


events {
    use epoll;
    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"';

    sendfile        on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        access_log  logs/host.access.log  main;
        error_page  404              /404.html;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }


        # redirect server error pages to the static page /50x.html
        location = /50x.html {
            root   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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
```

# 三、Gzip压缩
主要 ngx_http_gzip_module \ ngx_http_gzip_static_module \ ngx_http_gunzip_module 模块

其中 ngx_http_gzip_module 模块处理的9个指令:

```
1、gzip on | off;
2、gzip_buffers  number  size;  //number 指定向系统申请缓存空间个数; size指每个缓存空间的大小
3、gzip_comp_level  2;  //Gzip压缩程度,1-9;由低到高
4、gzip_disble   MSIE [4-6]\.;  //针对不同客户端发起的请求进行开启和关闭Gzip功能
5、gzip_http_version  1.0|1.1;   //针对不同http版本选择性开启关闭Gzip功能
6、gzip_min_length  1024;    //针对压缩很小数据反而数据量变大的情况设置最小字节数
7、gzip_proxied    off;     //使用反向代理时有效。不常用
8、gzip_types   text/plain   application/x-javascript    text/css    text/html    application/xml; //选择性开启功能
9、gzip_vary  on | off;   //设置响应头部  Accept-Encoding:Gzip,告诉接收方发送的数据经过了压缩处理
```

ngx_http_gzip_static_module模块的指令:

主要负责搜索和发送经过Gzip预压缩的数据,并以“.gz”为后缀存储在服务器上
```
gzip_static on | off;
gizp_http_version;
gizp_proxied;
gzip_disable;
gizp_vary;
```

ngx_http_gunzip_module 模块的指令暂不深入了解。

常见gizp配置如下,一般放在http全局块中:
```
gzip on;
gzip_min_length  1024;
gzip_buffers 4 16k;
gzip_comp_lever  2;
gzip_types  text/plain  application/x-javascript  text/css   application/xml;
gzip_vary  on;
gunzip_static  on;
```

# 四、Rewrite功能

# 五、代理服务

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值