Nginx 配置与使用

知识点

  1. 配置文件语法格式
  2. 配置第一个静态WEB服务
  3. 配置案例
    a. 动静分离实现
    b. 防盗链
    c. 多域名站点
    d. 下载限速
    e. IP 黑名单
    f. 基于user-agent分流
  4. 日志配置

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

先来看一个简单的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;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /nginx_status {
        stub_status on;
        access_log   off;
  	    }
    }
}

上述配置中的events、http、server、location、upstream等属于配置项块。而worker_processes 、worker_connections、include、listen 属于配置项块中的属性。 /nginx_status 属于配置块的特定参数参数。其中server块嵌套于http块,其可以直接继承访问Http块当中的参数。
配置块 名称开头用大口号包裹其对应属性
属性 基于空格切分属性名与属性值,属性值可能有多个项 都以空格进行切分 如:
access_log logs/host.access.log main
参数 其配置在 块名称与大括号间,其值如果有多个也是通过空格进行拆
注意 如果配置项值中包括语法符号,比如空格符,那么需要使用单引号或双引号括住配置项值,否则Nginx会报语法错误。例如:
log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ time_local] “KaTeX parse error: Double superscript at position 33: … '̲status b o d y b y t e s s e n t " body_bytes_sent " http_referer” ’
‘“ h t t p u s e r a g e n t " " http_user_agent" " http_x_forwarded_for”’;

2、配置第一个静态WEB服务

基础站点演示:
创建站点目录 mkdir -p /usr/www/xiaodao
编写静态文件
配置 nginx.conf
配置server
配置location

基本配置介绍说明:
(1)监听端口
语法:listen address:
默认:listen 80;
配置块:server

(2)主机名称
语法:server_name name[……];
默认:server_name “”;
配置块:server
server_name后可以跟多个主机名称,如server_name www.testweb.comdownload.testweb.com;。 支持通配符与正则

(3)location
语法:location[=|~|~*|^~|@]/uri/{……}
配置块:server

  1. / 基于uri目录匹配
  2. =表示把URI作为字符串,以便与参数中的uri做完全匹配。
  3. ~表示正则匹配URI时是字母大小写敏感的。
  4. ~*表示正则匹配URI时忽略字母大小写问题。
  5. ^~表示正则匹配URI时只需要其前半部分与uri参数匹配即可。

动静分离演示:
创建静态站点
配置 location /static
配置 ~* .(gif|png|css|js)$
基于目录动静分离
server {
listen 80;
server_name .luban.com;
root /usr/www/luban;
location / {
index luban.html;
}
location /static {
alias /usr/www/static;
}
}
基于正则动静分离
location ~
.(gif|jpg|png|css|js)$ {
root /usr/www/static;
}

防盗链配置演示:

加入至指定location 即可实现

valid_referers none blocked *.luban.com;
if ($invalid_referer) {
return 403;
}

下载限速:
location /download {
limit_rate 1m;
limit_rate_after 30m;
}

创建IP黑名单

创建黑名单文件

echo ‘deny 192.168.0.132;’ >> balck.ip
#http 配置块中引入 黑名单文件
include black.ip;

3、日志配置:
日志格式:
log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ time_local] “KaTeX parse error: Double superscript at position 33: … '̲status b o d y b y t e s s e n t " body_bytes_sent " http_referer” ’
‘“ h t t p u s e r a g e n t " " http_user_agent" " http_x_forwarded_for”’;
access_log logs/access.log main;
#基于域名打印日志
access_log logs/$host.access.log main;

error日志的设置
语法:error_log /path/file level;
默认:error_log logs/error.log error;
level是日志的输出级别,取值范围是debug、info、notice、warn、error、crit、alert、emerg,
针对指定的客户端输出debug级别的日志
语法:debug_connection[IP|CIDR]
events {
debug_connection 192.168.0.147;
debug_connection 10.224.57.0/200;
}
具体配置文件查看下篇博客:https://blog.csdn.net/weixin_32885733/article/details/88755389

猜你喜欢

转载自blog.csdn.net/weixin_32885733/article/details/88755380
今日推荐