码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx 模块常用命令介绍

时间:2016-10-14 00:39:49      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:nginx 模块常用命令介绍


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


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内容
<h1>Sunshine alias</h1>
[root@CentOS7_30 nginx]# curl http://www.sunshine.com/download/index.html    #curl获取信息,获取的是/data/site/index.html内容
<h1>Sunshine alias</h1>

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内容,注意看获取的结果
<h1>Sunshine root</h1>
[root@CentOS7_30 nginx]# curl    #更上面alias路径一样,获取到的内容却不一样 
<h1>Sunshine alias</h1>

#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.img
Resolving www.sunshine.com (www.sunshine.com)... 192.168.11.30
Connecting to www.sunshine.com (www.sunshine.com)|192.168.11.30|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 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获取 
<h1>Sunshine root</h1>
[root@CentOS7_30 nginx]# curl -X PUT   #使用PUT,报错403 
<html
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.8.1</center>
</body>
</html>
[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获取
<h1>Sunshine root</h1>
[root@CentOS7_29 ~]# curl -X PUT  #使用PUT,提示405,提示没权 
<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.8.1</center>
</body>
</html>

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      #访问
<h1>Sunshine root</h1>

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;


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

本文出自 “SunshineBoySZF” 博客,请务必保留此出处http://sunshineboyszf.blog.51cto.com/12087328/1861638

Nginx 模块常用命令介绍

标签:nginx 模块常用命令介绍

原文地址:http://sunshineboyszf.blog.51cto.com/12087328/1861638

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!