nginx常用配置说明

nginx的主配置(nginx.conf)说明

#worker进程数量
worker_processes  1;
#错误日志
error_log logs/error.log;
#进程ID文件
pid logs/nginx.pid;

#事件区块开始
events {
    #worker进程支持的最大连接数
    worker_connections  1024;
}

#http区块开始
http {
    #nginx支持的媒体类型库文件
    include       mime.types;
    #默认的媒体文件
    default_type  application/octet-stream;
    #开启高效传输模式
    sendfile        on;
    #连接超时
    keepalive_timeout  65;

    #一个server区块开始
    server {
        #端口号
        listen       80;
        #服务主机名
        server_name  localhost;
        #编码
        charset utf-8;
        #location区块
        location / {
            #站点根目录
            root   html;
            #默认首页文件
            index  index.html index.htm;
        }
        #出现对应状态码时,访问50x.html
        error_page   500 502 503 504  /50x.html;
        #访问50x.html时指定目录为html
        location = /50x.html {
            root   html;
        }
    }
}

nginx的状态信息功能

location / {
    #打开状态信息开关
    stub_status on;
    access_log off;
    allow 127.0.0.1/24;
    deny all;
}

nginx错误日志配置

关键字 日志文件 错误日志级别[debug|info|notice|warn|error|crit|alert|emerg]

error_log  logs/error.log  notice;

nginx访问日志配置

#定义日志格式

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 客户端地址 211.28.65.253
$remote_user 客户端用户名称 --
$time_local 访问时间和时区 18/Jul/2012:17:00:01 +0800
$request 请求的URI和HTTP协议 "GET /article-10000.html HTTP/1.1"
$http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.it300.com
192.168.100.100
$status HTTP请求状态 200
$upstream_status upstream状态 $body_bytes_sent 发送给客户端文件内容大小 1547
$http_referer url跳转来源 https://www.baidu.com/
$http_user_agent 用户终端浏览器等信息 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
$ssl_protocol SSL协议版本 TLSv1
$ssl_cipher 交换数据中的算法 RC4-SHA
$upstream_addr 后台upstream的地址,即真正提供服务的主机地址 10.10.10.100:80
$request_time 整个请求的总时间 0.205
$upstream_response_time 请求过程中,upstream响应时间 0.002

#访问日志配置

access_log logs/access.log main;

#在高并发的网站下,日志配置可以如下

access_log logs/access.log main gzip buffer=32k flush=5s;


nginx的location作用
location的作用是根据用户请求的URI来执行不同的应用。

location [= | ~ | ~* | ^~] URI {
    ... ...
}
~用于区分大小写
~*用于不区分大小写
^~进行常规字符串匹配检查后,不做正则表达式的检查

例如:

location = / {
    #精确匹配/
}
location / {
    #所有location不能匹配后的默认匹配
}
location /www/ {
    #匹配常规字符串,有正则,优先匹配正则
}
location ^~ /imgs/ {
    #匹配常规字符串,不做正则匹配检查
}
location ~* \.(gif|jpg|jpeg)$ {
    #正则匹配
}

nginx的rewrite配置

rewrite指令语法

rewrite regex replacement [flag];

例如:

rewrite ^/(.*) http://www.baidu.com/$1 permanent;

其中$1表示前面小括号匹配的部分。

flag参数说明:
last 本条规则匹配完成后,继续向下匹配新的规则
break 本条规则匹配完即终止
redirect 返回302临时重定向
permanent 返回301永久重定向

上述,last和break用来实现URL重写,redirect和permanet用来实现URL跳转

例如:

server {
    listen 80;
    server_name book.site.com;
    location / {
        root html/book;
        index index.html index.htm;
    }
    if($http_host ~* "^(.*)\.site\.com$") {
        set $domain $1;
        rewrite ^(.*) http://www.site.com/$domain/test.html break;
    }
}

当我们访问book.site.com时URL重写为www.site.com/book/test.html

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...