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;


其他的上官網看吧~其實挺簡單的。多看看就會了~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章