Skip to content
On this page

配置

其他选项

  • worker_processes: 默认为1,表示开启一个业务进程
  • worker_connections: 单个业务进程可接受连接数
  • include mime.types; 引入http mime类型
  • default_type application/octet-stream; 如果mime类型没匹配上,默认使用二进制流的方式传输。
  • sendfile on; 使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝
  • keepalive_timeout:控制一个 TCP协议最大连接时间,单位 s

server_name

使用 server_name 配置主机域名匹配规则

nginx
# 匹配域名 www.briver.com
server {
    listen 80;
    server_name  www.briver.com;
    root   /usr/share/nginx/html/www;
}
nginx
# 匹配任意二级域名 *.briver.com
server {
    listen 80;
    server_name  *.briver.com;
    root   /usr/share/nginx/html/briver;
}
nginx
# www.briver.com -> /usr/share/nginx/html/www
# 同时存在两个虚拟主机的时候,精准 > 模糊
server {
    listen 80;
    server_name  www.briver.com;
    root   /usr/share/nginx/html/www;
}

server {
    listen 80;
    server_name  *.briver.com;
    root   /usr/share/nginx/html/briver;
}
nginx
#正则匹配 [数字].briver.com
server {
    listen 80;
    server_name  ~^[0-9]+\.briver\.com$;
    root   /usr/share/nginx/html/user;
}

location

  • / 通用匹配,任何请求都会匹配到
  • = 精准匹配
  • ~ 正则匹配,区分大小写
  • ~* 正则匹配,不区分大小写
  • ^~ 正则匹配,不区分大小写

location 匹配顺序

  • 多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配
  • 普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)
  • 当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配
  • 所有类型location存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配)

rewrite

使用 rewrite 重写访问 url,可以实现伪静态

nginx
# 将 123.html -> /index.jsp?p=123
server {
    listen 80;
    server_name  www.briver.com;
    
    location / {
        rewrite ^/([0-9]+).html$ /index.jsp?p=$1 break;
    }
}

proxy_pass

使用 proxy_pass 进行反向代理、负载均衡、动静分离。

反向代理

nginx
# 访问 /baidu,反向代理到 http://baidu.com
server {
    listen 80;
    server_name  *.briver.com;

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

负载均衡

负载均衡默认使用轮询的方式。

  • weight: 权重
  • down: 当前主机不参与负载
  • backup: 其他主机请求有问题时,备用请求主机

其他负载方式

  • ip_hash: 根据客户端的ip地址转发同一台服务器,可以保持回话
  • least_conn: 最少连接访问
  • url_hash: 根据用户访问的url定向转发请求
  • fair: 根据后端服务器响应时间转发请求

使用 upstream 来指定配置负载均衡的主机

nginx
http {
    upstream httpd {
        server 127.0.0.1:8050 weight=10 down;
        server 127.0.0.1:8060 weight=1;
        server 127.0.0.1:8060 weight=1 backup;
    }

    server {
        listen 80;
        server_name  *.briver.com;

        location / {
            proxy_pass http://httpd;
        }
    }
}

动静分离

nginx
# 配置多个 localtion 进行动静分离
server {
    listen 80;
    server_name  *.briver.com;

    location / {
        proxy_pass http://www.briver.com;
    }

    location /css {
        root /usr/local/nginx/static;
        index index.html index.htm;
    }

    location /images {
        root /usr/local/nginx/static;
        index index.html index.htm;
    }

    location /js {
        root /usr/local/nginx/static;
        index index.html index.htm;
    }
}
nginx
# 使用正则通过一个 localtion 进行动静分离
server {
    listen 80;
    server_name  *.briver.com;

    location / {
        proxy_pass http://www.briver.com;
    }

    location ~*/(css|images|js) {
        root /usr/local/nginx/static;
        index index.html index.htm;
    }
}