nginx配置文件

默认配置文件位置:/usr/local/nginx/conf/nginx.conf

配置文件分成3大块

  • main 全局配置,对全局生效
  • events 配置影响 Nginx 服务器与用户的网络连接
  • http 配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置
  • server 配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块
  • location 配置虚拟主机内请求地址应该映射到哪里
  • upstream 用于被代理服务器有多个并且需要负载均衡的情况
  • types
配置大全
  • main 全局配置,对全局生效
  • worker_processes_number worker进程的数量,一般1个或者等于cpu线程数量,可以指定为auto
worker_processes  1;
  • 1.
  • user 指定nginx允许worker进程的用户和用户组
#语法:user USERNAME [GROUP]

user nginx lion; # 用户是nginx;组是lion
  • 1.
  • 2.
  • 3.
#nginx.conf 默认配置
#user  nobody;

#默认情况查看进程示 nobody
[root@localhost conf]# ps -ef|grep nginx
root       1328      1  0 16:02 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     1329   1328  0 16:02 ?        00:00:00 nginx: worker process
nobody     1330   1328  0 16:02 ?        00:00:00 nginx: worker process
root       1352   1277  0 16:54 pts/0    00:00:00 grep --color=auto nginx
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • pid 指定进程id存放位置
#ngixn默认pid 存放位置
#pid        logs/nginx.pid;
  • 1.
  • 2.
  • worker_rlimit_nofile_number 指定每个worker子进程最大文件句柄数
    nginx作为静态资源服务器的时候每个连接都会对应一个磁盘文件,这时候worker_rlimit_nofile_number 不应该小于worker_connections的一半,并且这个值不应该大于linux ulimit -n指定的单进程文件限制。
  • worker_rlimit_core 指定 worker 子进程异常终止后的 core 文件,用于记录分析问题。
worker_rlimit_core 100M; # 存放大小限制
working_directory /opt/nginx/tmp; # 存放目录
  • 1.
  • 2.
  • worker_cpu_affinity cpu亲和力,把cpu和worker进程绑定
orker_cpu_affinity 0001 0010 0100 1000; # 4个物理核心,4个worker子进程
  • 1.

将每个 worker 子进程与特定 CPU 物理核心绑定,优势在于,避免同一个 worker 子进程在不同的 CPU 核心上切换,缓存失效,降低性能。但其并不能真正的避免进程切换。

  • worker_priority 设置worker线程优先级偏量-20到+19,越小优先级越高
worker_priority -10; # 120-10=110,110就是linux线程最终的优先级
  • 1.
  • worker_shutdown_timeout 优雅关闭等待时间
worker_shutdown_timeout 5s;
  • 1.
  • timer_resolution worker程和操作系统的对时精度,
    调整时间间隔越大,系统调用越少,有利于性能提升;反之,系统调用越多,性能下降。
timer_resolution 100ms;
  • 1.
  • daemon 指定 Nginx 的运行方式,前台还是后台,一般都是后台
daemon on;
  • 1.
  • events 配置影响 Nginx 服务器与用户的网络连接
  • use Nginx使用何种事件驱动模型
use method; # 不推荐配置它,让nginx自己选择
  • 1.

method 可选值为:select、poll、kqueue、epoll、/dev/poll、eventport

  • worker_connections worker子进程能够处理的最大并发连接数。
    受到 worker_rlimit_nofile_number 和 操作系统 ulimit -n限制
worker_connections 1024 # 每个子进程的最大连接数为1024
  • 1.
  • accept_mutex 是否打开负载均衡互斥锁
accept_mutex on # 默认是off关闭的
  • 1.
  • http 配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置
  • server 配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块
  • server_name 指定虚拟主机域名
#语法:server_name name1 name2 name3

# 示例:
server_name www.nginx.com;
  • 1.
  • 2.
  • 3.
  • 4.

server-name可以使用通配符

域名匹配的四种写法:

  • 精确匹配: server_name  http://www.nginx.com ;
  • 左侧通配: server_name *. http://nginx.com ;
  • 右侧通配: server_name www.nginx.* ;
  • 正则匹配: server_name ~^www.nginx.*$ ; # ~是正则标记,^和$示开始结束标记

匹配优先级:精确匹配 > 左侧通配符匹配 > 右侧通配符匹配 > 正则表达式匹配

  • location 配置虚拟主机内请求地址应该映射到哪里
  • root 指定静态资源目录位置,它可以写在 http 、 server 、 location 等配置中
location /te/ {
            root   html;
            index  index.html index.htm;
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • alias
location /te/ {
            alias   html;
            index  index.html index.htm;
        }
  • 1.
  • 2.
  • 3.
  • 4.

alias 和 root 的区别 在于alias时替换(不会加上匹配部分),root指定根目录(会加上匹配部分)

  • proxy_pass 代理地址
location /log/{
    proxy_pass http://192.168.1.6;   #可以写upstream的地址
}

上下文:location、if、limit_except
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

proxy_pass 地址后面的 斜杆很关键,类似alias 和 root 的区别

  • add_header 添加或者修改响应头
location / {
            alias  html;
            index  index.html index.htm;
            add_header aaaaa 11111;
            proxy_pass http://192.168.1.6;
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

效果

nginx-4.配置大全_服务器

  • return 停止处理请求,直接返回响应码或重定向到其他 URL ;执行 return 指令后, location 中后续指令将不会被执行。
#return code [text];
#return code URL;
#return URL;

#例如:
location / {
	return 404; # 直接返回状态码
}

location / {
	return 404 "pages not found"; # 返回状态码 + 一段文本
}

location / {
	return 302 /bbs ; # 返回状态码 + 重定向地址
}

location / {
	return https://www.baidu.com ; # 返回重定向地址
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • rewrite 根据指定正则表达式匹配规则,重写 URL ,重写适用于复杂的转发场景,如果用到重写可以反过来思考设计是否合理,rewrite可使用ngixn内置变量,文档末尾有总结
#语法:rewrite 正则表达式 要替换的内容 [flag];

#上下文(标签):server、location、if

#示例:rewirte /images/(.*\.jpg)$ /pic/$1; # $1是前面括号(.*\.jpg)的反向引用
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

flag 可选值的含义:

  • last 重写后的 URL 发起新请求,再次进入 server 段,重试 location 的中的匹配;
  • break 直接使用重写后的 URL ,不再匹配其它 location 中语句;
  • redirect 返回302临时重定向;
  • permanent 返回301永久重定向;
rewrite 例子
server{
  listen 80;
  server_name fe.lion.club; # 要在本地hosts文件进行配置
  root html;
  location /search {
  	rewrite ^/(.*) https://www.baidu.com redirect;
  }
  
  location /images {
  	rewrite /images/(.*) /pics/$1;
  }
  
  location /pics {
  	rewrite /pics/(.*) /photos/$1;
  }
  
  location /photos {
  
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • if 指令,if可使用ngixn内置变量,文档末尾有总结
#语法:if (condition) {...}

#上下文:server、location

#示例:
if($http_user_agent ~ Chrome){
  rewrite /(.*)/browser/$1 break;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

condition 判断条件:

  • $variable 仅为变量时,值为空或以0开头字符串都会被当做 false 处理;
  • = 或 != 相等或不等;
  • ~ 正则匹配;
  • ! ~ 非正则匹配;
  • ~* 正则匹配,不区分大小写;
  • -f 或 ! -f 检测文件存在或不存在;
  • -d 或 ! -d 检测目录存在或不存在;
  • -e 或 ! -e 检测文件、目录、符号链接等存在或不存在;
  • -x 或 ! -x 检测文件可以执行或不可执行;
if例子
server {
  listen 8080;
  server_name localhost;
  root html;
  
  location / {
	#满足条件的时候会执行 rewrite
  	if ( $uri = "/images/" ){
    	rewrite (.*) /pics/ break;
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • autoindex 自动生成首页,用于ngxin作为文件服务器(映射指定硬盘目录)
server {
  listen 80;
  server_name fe.lion-test.club;
  
  location /download/ {
    root /opt/download;
    
    autoindex on; # 打开 autoindex,,可选参数有 on | off
    autoindex_exact_size on; # 精确大小,默认on,显示byte。off关闭显示KB,MB,GB
    autoindex_format html; # 以html的方式进行格式化,可选参数有 html | json | xml
    autoindex_localtime off; # 显示的⽂件时间为⽂件的服务器时间。默认为off,显示的⽂件时间为GMT时间
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

效果

nginx-4.配置大全_html_02

  • upstream 用于被代理服务器有多个并且需要负载均衡的情况
  • server 定义上游服务器地址;
语法:server address [parameters]

上下文:upstream
  • 1.
  • 2.
  • 3.

parameters 可选值:

  • weight=number 权重值,默认为1;
  • backup 备份服务器,仅当其他服务器都不可用时才会启用;
  • down 标记服务器长期不可用,离线维护;
  • max_conns=number 上游服务器的最大并发连接数;
  • fail_timeout=time 服务器不可用的判定时间;
  • max_fails=numer 服务器不可用的检查次数,超过一定次数以后nginx会自动剔除这个子服务器
  • keepalive 对上游服务启用长连接;
keepalive 16;
  • 1.
  • keepalive_requests 一个长连接可以有多少个请求 HTTP请求;
#默认值 100
keepalive_requests 100;
  • 1.
  • 2.
  • keepalive_timeout 空闲情形下,一个长连接的超时时长;
#默认值60秒
keepalive_timeout 60s;
  • 1.
  • 2.
  • 指定负载均衡算法可有下面值
  • hash
  • ip_hash
  • least_conn
  • least_time
  • random