本次的命令资料全部来自官网除全局定义以及events,地址: 

Nginx 配置组成:

 

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

一、全局定义

########### 每个指令必须有分号结束。##################user administrator administrators;  #配置用户或者组,默认为nobody nobody。#worker_processes 2;  #允许生成的进程数,默认为1#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg

二、events定义

events {    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport    worker_connections  1024;    #最大连接数,默认为512}

三、http定义,最为重点

 Modules reference下的

1、alias path;

使用场景:location

[root@CentOS7_30 nginx]# grep "download\|alias" nginx.conf    #查看配置	location /download/ {	    alias /data/site/;[root@CentOS7_30 nginx]# tail /data/site/index.html           #查看site下index.html内容

Sunshine alias

[root@CentOS7_30 nginx]# curl http://www.sunshine.com/download/index.html    #curl获取信息,获取的是/data/site/index.html内容

Sunshine alias

2、root path;

使用场景:httpserverlocationif in location

[root@CentOS7_30 nginx]# grep "download\|root" nginx.conf     #查看配置	location /download/ {	    root /data/site;[root@CentOS7_30 nginx]# cat /data/site/download/index.html   #查看download下index.html内容,注意看获取的结果

Sunshine root

[root@CentOS7_30 nginx]# curl    #更上面alias路径一样,获取到的内容却不一样 

Sunshine alias

#alias与root区别在于,alias把请求/download/变成/data/site/,而root则是把请求的/download/变更/data/site/download/

3、http { ... } 

定义:提供服务配置内容,这个没什么好讲的

4、limit_rate rate;

使用场景http, server, location, if in location  定义:现在访问资源的速率

[root@CentOS7_30 nginx]# grep "location\|root\|limit_rate" nginx.conf      #查看下配置	location /download/ {	    root /data/site;	    limit_rate 20k;[root@CentOS7_29 ~]# wget http://www.sunshine.com/download/sunshine.img    #测试下,看看下载速率--2016-10-13 19:22:15--  http://www.sunshine.com/download/sunshine.imgResolving www.sunshine.com (www.sunshine.com)... 192.168.11.30Connecting to www.sunshine.com (www.sunshine.com)|192.168.11.30|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 1073741824 (1.0G) [application/octet-stream]Saving to: ‘sunshine.img’ 0% [                                                                                                                                                      ] 6,103,040   19.6KB/s  eta 14h 29m

5、limit_except method ... { ... }

使用场景:location 定义:使用的方法

[root@CentOS7_30 nginx]# grep "limit\|allow\|deny" nginx.conf             #查看配置	    limit_rate 20k;	    limit_except GET POST { 		   #allow 192.168.220.19;		   allow 192.168.11.29;		   deny all;[root@CentOS7_30 nginx]# ifconfig | grep "inet 192.168"                   #查看IP地址        inet 192.168.11.30  netmask 255.255.255.0  broadcast 192.168.11.255[root@CentOS7_30 nginx]# curl  #使用GET获取 

Sunshine root

[root@CentOS7_30 nginx]# curl -X PUT   #使用PUT,报错403 403 Forbidden

403 Forbidden


nginx/1.8.1
[root@CentOS7_29 ~]# ifconfig | grep "inet 192.168"                       #查看IP地址        inet 192.168.11.29  netmask 255.255.255.0  broadcast 192.168.11.255        [root@CentOS7_29 ~]# curl http://www.sunshine.com/download/index.html     #使用GET获取

Sunshine root

[root@CentOS7_29 ~]# curl -X PUT  #使用PUT,提示405,提示没权 
405 Not Allowed

405 Not Allowed


nginx/1.8.1

6、listen address[:port]    listen port     listen unix:path    

使用场景:server 定义:监听,ip+port  port  unix:path

[root@CentOS7_30 nginx]# grep "server\|listen" nginx.conf                  #查看配置    server {        listen       80;        server_name  [root@CentOS7_29 ~]# curl http://www.sunshine.com/download/index.html      #访问

Sunshine root

7、server {......}

场景:http 定义:虚拟主机

[root@CentOS7_30 nginx]# grep "server\|listen" nginx.conf                #查看配置,在http{server}    server {        listen       80;        server_name  www.sunshine.com;

8、server_name name ...;    

场景:server 定义:主机名称

[root@CentOS7_30 nginx]# grep "server\|listen" nginx.conf                #查看配置,在server{server_name}    server {        listen       80;        server_name  www.sunshine.com;

其他的上官网看吧~其实挺简单的。多看看就会了~