设为首页 收藏本站
查看: 806|回复: 0

[经验分享] Nginx 模块常用命令介绍

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-10-14 08:22:00 | 显示全部楼层 |阅读模式
  本次的命令资料全部来自官网除全局定义以及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下的
  • ngx_http_core_module


1、alias path;
使用场景:location
1
2
3
4
5
6
7
[iyunv@CentOS7_30 nginx]# grep "download\|alias" nginx.conf    #查看配置
    location /download/ {
        alias /data/site/;
[iyunv@CentOS7_30 nginx]# tail /data/site/index.html           #查看site下index.html内容
<h1>Sunshine alias</h1>
[iyunv@CentOS7_30 nginx]# curl http://www.sunshine.com/download/index.html    #curl获取信息,获取的是/data/site/index.html内容
<h1>Sunshine alias</h1>



2、root path;
使用场景:http, server, location, if in location
1
2
3
4
5
6
7
[iyunv@CentOS7_30 nginx]# grep "download\|root" nginx.conf     #查看配置
    location /download/ {
        root /data/site;
[iyunv@CentOS7_30 nginx]# cat /data/site/download/index.html   #查看download下index.html内容,注意看获取的结果
<h1>Sunshine root</h1>
[iyunv@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  定义:现在访问资源的速率
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@CentOS7_30 nginx]# grep "location\|root\|limit_rate" nginx.conf      #查看下配置
    location /download/ {
        root /data/site;
        limit_rate 20k;
[iyunv@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 定义:使用的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[iyunv@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;
[iyunv@CentOS7_30 nginx]# ifconfig | grep "inet 192.168"                   #查看IP地址
        inet 192.168.11.30  netmask 255.255.255.0  broadcast 192.168.11.255
[iyunv@CentOS7_30 nginx]# curl  #使用GET获取
<h1>Sunshine root</h1>
[iyunv@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>
[iyunv@CentOS7_29 ~]# ifconfig | grep "inet 192.168"                       #查看IP地址
        inet 192.168.11.29  netmask 255.255.255.0  broadcast 192.168.11.255        
[iyunv@CentOS7_29 ~]# curl http://www.sunshine.com/download/index.html     #使用GET获取
<h1>Sunshine root</h1>
[iyunv@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
1
2
3
4
5
6
[iyunv@CentOS7_30 nginx]# grep "server\|listen" nginx.conf                  #查看配置
    server {
        listen       80;
        server_name  
[iyunv@CentOS7_29 ~]# curl http://www.sunshine.com/download/index.html      #访问
<h1>Sunshine root</h1>



7、server {......}
场景:http 定义:虚拟主机
1
2
3
4
[iyunv@CentOS7_30 nginx]# grep "server\|listen" nginx.conf                #查看配置,在http{server}
    server {
        listen       80;
        server_name  www.sunshine.com;



8、server_name name ...;   
场景:server 定义:主机名称
1
2
3
4
[iyunv@CentOS7_30 nginx]# grep "server\|listen" nginx.conf                #查看配置,在server{server_name}
    server {
        listen       80;
        server_name  www.sunshine.com;




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


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-285688-1-1.html 上篇帖子: Nginx 源码编译安装 下篇帖子: Nginx实现二级域名店铺
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表