性能优化-Nginx-04-配置与使用

一、配置文件的语法格式:

    (1)Nginx默认配置nginx.conf,修改配置文件时注意编码与语言格式

worker_processes  1; # 允许的工作进程数, 默认进程为1

events {
    worker_connections  1024; # 最大连接数,默认1024
}

http { # http请求配置快,只能配置一个
    include       mime.types;  # 文件扩展名与文件类型映射表,常用的有html->text/html,                
                               # jpeg- >image/jpeg 等
    default_type  application/octet-stream; # 文件类型,默认为text/plain
    sendfile        on; # 允许sendfile方式传输文件,默认为off,可以在http块,server块,                
                        # location块。数据直接从内核态拷贝到网卡,不经过Nginx
    keepalive_timeout  65; #连接超时时间,默认为75s,可以在http,server,location块。

    server { # 站点配置快,可以配置多个
        listen       80; #监听端口
        server_name  localhost; # 站点名称,类似域名,基于最大匹配原则,按照最前面的匹配

        location / { # 请求的url过滤,可以配置多个
            root   html; # 根目录 
                         # html(NGINX根目录下的html目录),/html(linux根目录下的html目录)
            index  index.html index.htm; # 默认页
        }

	location = /basic_status { # /basic_status 请求url配置,
                               # /basic_status类似方法参数 
    	    stub_status; # nginx方法
	}


        error_page   500 502 503 504  /50x.html; # 错误页面设置
        location = /50x.html { # 返回具体错误页面
            root   html; # NGINX根目录下的html目录的错误页面
        }
    }
}

上述配置中的events、http、server、location等属于配置项块。

worker_processes 、worker_connections、include、listen  属于配置项块中的属性。  

/basic_status   属于配置块的特定参数。其中server块嵌套于http块,其可以直接继承访问Http块当中的参数。

配置块

名称开头用大口号包裹其对应属性

属性

基于空格切分属性名与属性值,属性值可能有多个项 都以空格进行切分 如:

access_log  logs/bert.access.log  main

参数

其配置在 块名称与大括号间,其值如果有多个也是通过空格进行拆

 

如果配置项值中包括语法符号,比如空格符,那么需要使用单引号或双引号括住配置项值。

例如:

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                     '$status $body_bytes_sent "$http_referer" '

                     '"$http_user_agent" "$http_x_forwarded_for"';

$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址; 

$remote_user :用来记录客户端用户名称; 

$time_local : 用来记录访问时间与时区;

$request : 用来记录请求的url与http协议;

$status : 用来记录请求状态;成功是200;

$body_bytes_s ent :记录发送给客户端文件主体内容大小;

$http_referer :用来记录从那个页面链接访问过来的; 

$http_user_agent :记录客户端浏览器的相关信息;

 

二、基本配置说明

    (1)创建站点目录 mkdir /www/bert

    (2)基本配置介绍说明:

             2.1、监听端口与主机名称:

                       server {
                           listen       80;

                           server_name  localhost;

                       }

                      server_name后可以跟多个主机名称,如server_name test1.com test2.com test*.com;。 支持通配符与正则

             2.2、location

                     语法:location[=|~|~*|^~|@]/uri/{……}

                     =表示把URI作为字符串,以便与参数中的uri做完全匹配。          

                            location = /baidu {
                                  proxy_pass http://www.baidu.com/;
                            }

                            curl 127.0.0.1/baidu

                     / 基于uri目录匹配

                    ~表示正则匹配URI时是字母大小写敏感的。

                    ~*表示正则匹配URI时忽略字母大小写问题。

                    ^~表示正则匹配URI时只需要其前半部分与uri参数匹配即可。

                    优先规则:=,正则,前缀匹配,靠前

(3)root 指定站点根目录,绝对匹配

         可配置在 server与location中,location高于server,基于ROOT路径+URL中路径去寻找指定文件。

         location /aaa {
             root   html/bert/;
             index  index.html index.htm;
         }

         curl http://127.0.0.1/aaa  ,这种会去找 html/bert/aaa 下的index.html

(4)alias 指定站点别名,相对匹配

         只能配置location 中。基于alias 路径+ URL移除location  前缀后的路径来寻找文件。

         如下示例:

         location /bert { # 请求的url过滤,可以配置多个
             alias   /www/bert/; # (linux根目录下的www/bert目录)
             index  index.html index.htm; # 默认页
         }

         alias   /www/bert/  需要在最好加/, 不然location请求url也没加/就会叠加。

         curl http://127.0.0.1/bert/ , 会去找 /www/bert/ 下的index.html

 

三、高级配置说名

在/etc/hosts文件中配置ip与域名的映射 : 127.0.0.1 aaa.bert.com

(1)基于目录动静分离

   server {

        listen 8080;

        server_name *.bert.com;

        root /www/bert;

        location / {

            index bert.html;

        }

        location /static {

            alias /www/static;

        }

 }

curl aaa.bert.com:8080/

curl aaa.bert.com:8080/static/

 

(2)基于正则动静分离

location ~* \.(gif|jpg|png|css|js)$ {

      root /www/static;

}       

curl aaa.bert.com:8080/index.css

 

(3)防盗链配置

加入至对应location 即可实现

valid_referers none blocked aaa.bert.com;

 if ($invalid_referer) {

       return 403;

}

在/etc/hosts文件中配置ip与域名的映射 : 127.0.0.1 bbb.bert.com

在/www/static下添加jpg文件

修改/www/bert/bert.html,引入jpg文件

访问  http://aaa.bert.com:8080/bert.html,页面内容正常返回,jpg正常返回

访问  http://bbb.bert.com:8080/bert.html,页面内容正常返回,jpg报403 如下图

(4)下载限速

location /download {

    alias   /www/download/;

    limit_rate 10k; #限制每S下载速度10K

    limit_rate_after 300k; # 超过300k 之 后再下载就限速

}

访问 http://aaa.bert.com:8080/download/bert.docx

(5)创建IP黑名单

location /download {

    alias   /www/download/;

    deny 192.168.0.1;

}

#封禁指定IP

deny 192.168.0.1;

allow 192.168.0.1;

#开放指定IP 段

allow 192.168.0.0/24;

#封禁所有

deny    all;

#开放所有

allow    all;

# 创建黑名单文件

echo 'deny 192.168.0.132;' >> balck.ip

#http 配置块中引入 黑名单文件

include       black.ip;

 

四、测试Nginx文件及相关目录截图

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types; 
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

		root   html;
		
        location / {
			root /www/bert;
			index index.html;
        }
		
		location /abc {
            alias   /www/bert;
			index  bert.html;
        }

		location = /basic_status {
    	    stub_status;
		}

		location = /baidu {
            proxy_pass http://www.baidu.com/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

	server {
        listen       8080;
        server_name  *.bert.com;

        root   /www/bert;
		
        location / {
            index  index.html index.htm;
        }
		
		location /static {
            alias   /www/static/;
            index  index.css;
        }
		
		location ~* \.(gif|jpg|png|css|js)$ {		
            root /www/static;
			valid_referers none blocked aaa.bert.com;
			if ($invalid_referer) {
				return 403;
			}
        }
		
		location /download {
			#deny 192.168.0.1;
			alias   /www/download/;
			limit_rate 10k;
			limit_rate_after 20k;
		}
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值