Nginx基本配置

配置文件: /usr/local/nginx/conf/nginx.conf

基本模块

  • 核心模块 放于配置文件开始位置
daemon on|off   启用或禁用守护进程模式(后台启动)
error_log logs/error.log 
log_not_found on|off    开启或禁用404错误
master_process on|off   一个主进程和worker进程
user username groupname     worker进程使用配置
worker_priority     定义worker优先级,-20~19,默认0
worker_processes 4;     worker进程数量
  • 事件模块
events{
    worker_connections 1024;    //一个worker进程能够同时连接数量
    accept_mutex on;    //使用一个接受互斥锁来打开套接字监听
    accept_mutex_delay 500ms;   //定义一个worker进程尝试再次获取资源之前应该等待时间
}
  • 配置模块
include mime.types
  • http模块,提供web服务
http {
    gzip on;
    server {
        server_name www.mysite.com;
        listen 80;
        location /html/ {
            gzip off;
        }
    }
    server {}
}
  • server : 声明站点
server {
    server_name www.mysite.com mysite.com;
    listen 127.0.0.1:8080;
    #listen [ipv6]:8080;
    root /home/mysite.com/html; #网站根目录,不以/结尾,可放区段 http,server,location,if
    index index.php index.html; #默认页面,可放区段 http,server,location
    error_page 404 /notFound.html; #修改URI所产生的HTTP响应码选择性替换,可放区段 http,server,location,if
    error_page 403 http://www.mysite.com/;
    error_page 500 501 502 503 504 /server_error.html; 
    # error_page 404 =200 /index.html; #出现404错误时候重定向到index.html,并且响应码为200 OK
    # error_page 404 @rename; #跳到命名的location区段
    log_not_found off; #日志记录404错误
    recursive_error_pages off; #error_page提供的页面本身也发生了错误,指令error_page会被再次递归
    direction 5m; #读取的文件大于5m时候,nginx将通过Direct I/O系统机制读取直接放入内存,不用通过复杂的中间缓存处理 可放区段 http,server,location

    keepalive_requests 100; #单个keep-alive提供的最大请求量 可放区段 http,server,location
    keepalive_timeout 60; #定义keep-alive 时长,这个时间内,客户端对服务器的访问不需再次建立连接或重新连接 可放区段 http,server,location
    send_timeout 60; #nginx会关闭超过设置时间的不活动连接 可放区段 http,server,location

    include mime.types; #MIME类型与扩展名直接联系
    location /downloads/ {
        internal; #只允许内部重定向
        types { #可放区段 http,server,location
            text/html html; #重新声明进行覆盖,可使该区块空与default_type配合,强制该目录文件只能下载不能显示
        }
        default_type application/octet-stream; #提供的文件扩展名与任何MIME不匹配时使用 可放区段 http,server,location
    }

    #限制约束
    location /root/ {
        limit_except GET { #只允许192.168.0.1/24客户端通过GET访问到root目录下 可放区段 location
            allow 192.168.0.1/24;
            deny all;
        }
    }

    location /home/ {
        alias /home/mysite.com/static/; #只能放在location区段中,当访问 www.mysite.com/home/ 会路由到/home/mysite.com/static/ 下,尾部以/结尾
    }
    location @rename {
        try_files $uri $uri.php $uri.html @error_location; #只能放在location区段中,试图找到指定文件,失败则添加.php,然后再提供文件服务,最后找不到则用另外location区段处理请求
    }
    location @error_location {
        proxy_pass 127.0.0.1:8888;
    }
}
  • location : 定义配置应用于网站特定目录,比如http全局开启gzip压缩,可以在html目录下关闭gzip压缩
server {
    server_name mysite.com;
    location /html/ {
        #www.mysite.com/html/
    }
}
# 语法 location [= | ~ | ~* | ^~ | @] pattern { ... }
location = /html {
    #精确匹配,不使用正则表达式
    #yes: www.mysite.com/html
    #yes: www.mysite.com/HTML
    #yes: www.mysite.com/html?param1=1&param2=2
    #no:  www.mysite.com/html/
    #no:  www.mysite.com/htmls
    #no:  www.mysite.com/html/demo.php
}
location /html {
    #指定模式开始匹配,不使用正则表达式
    #yes: www.mysite.com/html
    #yes: www.mysite.com/HTML
    #yes: www.mysite.com/html?param1=1&param2=2
    #yes: www.mysite.com/html/
    #yes: www.mysite.com/htmls
    #yes: www.mysite.com/html/demo.php
}
location ~ ^/html$ {
    #区分大小写,使用正则表达式
    #yes: www.mysite.com/html
    #no:  www.mysite.com/HTML
    #yes: www.mysite.com/html?param1=1&param2=2
    #no:  www.mysite.com/html/
    #no:  www.mysite.com/htmls
}
location ~* ^/html$ {
    #不区分大小写,使用正则表达式
    #yes: www.mysite.com/html
    #yes:  www.mysite.com/HTML
    #yes: www.mysite.com/html?param1=1&param2=2
    #no:  www.mysite.com/html/
    #no:  www.mysite.com/htmls
}
location @rename {
    #命名区段,只能内部访问,如try_files.error_page
}

rewrite模块 (rewrite if)

  • 正则表达式
元字符描述量词描述
^开始*0或多次
$结束+1或多次
.任何字符?0或1次
[]{n}n次
[^]否定组{n,}至少n次
()分组{n,m}n~m次
|
\|转义

server {
    server_name www.mysite.com
    listen 80;
    root /home/mysite.com/html;
    location /php/ {
        internal;
        alias /home/mysite.com/php/;
    }
    location /documents/ { #客户端访问 www.mysite.com/documents/index.php
        rewrite ^/documents/(.*)$ /php/$1; #重写为 /php/index.php,重定向到 /php/
    }
}
  • 条件结构
server {
    # if 与 location 功能相近,location支持更多的指令
    if ($string) {}
    if ($request_method = POST) { 
        # =,!=
    }
    if ($request_filename ~ "\.php$") {
        # ~ 正则匹配,大小写敏感
        # ~* 正则匹配,大小写不敏感
        # !~
        # !~*
    }
    if (-f $request_filename) {
        # -f !-f 指定文件是否存在
        # -d !-d 指定目录是否存在
    }
    #其它指令
    if (-f $request_filename) {
        break; #匹配则URI被锁定,不能再被改变
    }
    if (-f $request_filename) {
        return 404; #中断返回状态码
    }
    set $var1 "index .php"
    if ($var1 ~ ^(.*) (.*)$) {
        set var2 $1$2;
        rewrite ^ http://www.mysite.com/$var2;
    }
}

Autoindex 模块

autoindex on; #若找不到文件则会显示该目录列表
autoindex_exact_size on; #显示文件大小
autoindex_localtime on; #显示文件日期

auth_basic模块 认证功能

auto_basic "进入该站点需要帐号密码"; #提示信息
auto_basic_user_file access/password_file; #密码文件位置,格式为 username:password 密码需为crypt()加密

Access模块 语法跟apache一样

location {
    allow 127.0.0.1;
    deny all;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值