nginx 配置样例
- nginx 配置样例
# 配置 nginx 用户及组:用户 组,window 下不指定 #user nobody; # 工作进程数目,根据硬件调整,通常等于 CPU 数量或者 2 倍于CPU。 worker_processes 1; # 错误日志:存放路径 日志级别 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; # pid(进程标识符):存放路径,windows 放在 logs/nginx.pid pid /usr/local/nginx/logs/nginx.pid; # 一个进程能打开的文件描述符最大值,理论上该值因该是最多能打开的文件数除以进程数 # 但是由于 nginx 负载并不是完全均衡的,所以这个值最好等于最多能打开的文件数 # Linux 系统可以执行 sysctl -a | grep fs.file 可以看到 Linux 文件描述符 worker_rlimit_nofile 65535; events { # 使用 epoll 的 I/O 模型。Linux 建议 epoll,FreeBSD 建议采用 kqueue,window 下不指定。 use epoll; # 单个进程最大连接数(最大连接数=连接数*进程数) worker_connections 1024; # 客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置, # 一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。 client_header_buffer_size 4k; } http { # 设定 mime 类型, 类型由 mime.type 文件定义 include mime.types; default_type application/octet-stream; # 日志格式设置 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # 用了 log_format 指令设置了日志格式之后,需要用 access_log 指令指定日志文件的存放路径 # 记录了哪些用户,哪些页面以及用户浏览器、ip和其他的访问信息 # 这里的 main 就是上面定义的日志格式名称 access_log logs/host.access.log main; # 服务器名字的 hash 表大小 server_names_hash_bucket_size 128; # 客户端请求头缓冲大小 # nginx 默认会用 client_header_buffer_size 这个 buffer 来读取 header 值 # 如果 header 过大,它会使用 large_client_header_buffers 来读取 # 如果设置过小的 HTTP 头,或 Cookie 过大, 会报400 错误 nginx 400 bad request # 如果超过 buffer,就会报 HTTP 414 错误 (URI Too Long) # nginx 接受最长的 HTTP 头部大小必须比其中一个 buffer 大 # 否则就会报 400 的 HTTP 错误 (Bad Request) client_header_buffer_size 32k; large_client_header_buffers 4 32k; # 隐藏ngnix版本号 server_tokens off; # 忽略不合法的请求头 ignore_invalid_headers on; # 让 nginx 在处理自己内部重定向时不默认使用 server_name 设置中的第一个域名 server_name_in_redirect off; # 客户端请求体的大小 client_body_buffer_size 8m; # 开启文件传输,一般应用都应设置为 on; # 若是有下载的应用,则可以设置成 off 来平衡网络 I/O 和磁盘的 I/O 来降低系统负载 sendfile on; # 告诉 nginx 在一个数据包里发送所有头文件,而不一个接一个的发送 tcp_nopush on; # tcp_nodelay off 会增加通信的延时,但是会提高带宽利用率 # 在高延时、数据量大的通信场景中应该会有不错的效果 # tcp_nodelay on,会增加小包的数量,但是可以提高响应速度 # 在及时性高的通信场景中应该会有不错的效果 tcp_nodelay on; # 长连接超时时间,单位是秒 keepalive_timeout 65; # gzip模块设置,使用 gzip 压缩可以降低网站带宽消耗,同时提升访问速度。 # 开启gzip gzip on; # 最小压缩大小 gzip_min_length 1k; # 压缩缓冲区 gzip_buffers 4 16k; # 压缩版本 gzip_http_version 1.0; # 压缩等级 gzip_comp_level 2; # 压缩类型 gzip_types text/plain text/css text/xml text/javascript application/json application/x-javascript application/xml application/xml+rss; # 负载均衡 # max_fails 为允许请求失败的次数,默认为1 # weight 为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。 upstream myServer{ server 192.168.247.129:8080 max_fails=3 weight=2; server 192.168.247.129:8081 max_fails=3 weight=4; } server { listen 80; # IP/域名可以有多个,用空格隔开 server_name 192.168.247.129; server_name www.test.com; charset koi8-r; access_log logs/host.access.log main; # 反向代理配置, # 将所有请求为 www.test.com 的请求全部转发到 upstream 中定义的目标服务器中。 location / { # 此处配置的域名必须与upstream的域名一致,才能转发。 #proxy_pass http://192.168.247.129:8080; proxy_pass http://myServer; # nginx跟后端服务器连接超时时间(代理连接超时) proxy_connect_timeout 20; # 允许客户端请求的最大单文件字节数 client_max_body_size 10m; # 缓冲区代理缓冲用户端请求的最大字节数 client_body_buffer_size 128k; # 后端服务器数据回传时间(代理发送超时) proxy_send_timeout 300; # 连接成功后,后端服务器响应时间(代理接收超时) proxy_read_timeout 300; # 设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffer_size 4k; # proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_buffers 4 32k; # 高负荷下缓冲大小(proxy_buffers*2) proxy_busy_buffers_size 64k; # 设定缓存文件夹大小,大于这个值,将从upstream服务器传 proxy_temp_file_write_size 64k; root html; # 定义首页索引文件的名称 index index.html index.htm; } # 动静分离静态资源走 linux 动态资源走 tomcat # 注意 /source/image/ 下面寻找资源 location /image/ { root /source/; autoindex on; } # 出现 50x 错误时,使用 /50x.html 页返回给客户端 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # 下面是配置生产环境中既支持 HTTP 又支持 HTTPS, 保证用户在浏览器中输入 HTTP 也能正常访问 # SSL证书 配置 # 加密证书路径 ssl_certificate cert/yphtoy.com.pem; #加密私钥路径 ssl_certificate_key cert/yphtoy.com.key; #加密协议 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #加密访问缓存设置,可以大大提高访问速度 ssl_session_cache shared:SSL:1m; #加密访问缓存过期时间 ssl_session_timeout 10m; #加密算法 ssl_ciphers HIGH:!aNULL:!MD5; #是否由服务器决定采用哪种加密算法 ssl_prefer_server_ciphers on; # 负载均衡 upstream api_upstream { server 127.0.0.1:8080 max_fails=3 weight=1; server 127.0.0.1:8081 max_fails=3 weight=1; } # api 接口(兼容HTTP) server { listen 80; server_name api.test.com; # 301重定向跳转到HTTPS接口 return 301 https://$server_name$request_uri; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #api 接口(兼容HTTPS) server{ listen 443 ssl; server_name api.test.com; location / { root html; index index.html index.htm; proxy_pass http://api_upstream; # 语法:proxy_cookie_path oldpath replacepath; # oldpath就是你要替换的路径 replacepath 就是要替换的值 # 作用:同一个web服务器下面多个应用之间能获取到cookie proxy_cookie_path /api/ /; # 服务端接收的请求头 Cooke 值不变 proxy_set_header Cookie $http_cookie; } } # 管理后台端(兼容HTTP) server { listen 80; server_name manage.test.com; # 301重定向跳转到HTTPS接口 return 301 https://$server_name/$request_uri; error_page 500 502 503 504 /50x.html; location = /50x.html{ root html; } } # 管理后台端(兼容HTTPS) server{ listen 443 ssl; server_name manage.test.com; location / { root /home/test/web/dist index /index.html; # 语法:try_files 【$uri】 【 $uri/】 【参数】 # 当用户请求https://manage.test.com/login时, # 一.如果配置了上面的默认index,会依次请求 # 1./home/test/web/dist/login 查找有没有 login 这个文件,没有的话 # 2./home/test/web/dist/index.html 有就直接返回 # 二.如果没有配置了上面的默认index或者配置了没有找到对应的资源,会依次请求 # 1./home/test/web/dist/login 查找有没有 login 这个文件,没有的话 # 2./home/test/web/dist/login/ 查找有没有 login 这个目录,没有的话 # 3.请求https://manage.test.com/index.html nginx 内部做了一个子请求 # 三.总的来说, index 的优先级比 try_files 高,请求会先去找 index 配置, 这里最后一个参数必须存在 try_files $uri $uri/ /index.html; # 解决跨域问题 # 允许跨域请求地址(*表示全部,但是无法满足带cookie请求,因为cookie只能在当前域请求) add_header Access-Control-Allow-Origin $http_origin; # 允许接收cookie和发送cookie add_header Access-Control-Allow-Credentials 'true'; # 允许请求的方法 add_header Access-Control-Allow-Methods 'GET,POST,DELETE,PUT,OPTIONS'; # 允许请求头(Content-Type:请求数据/媒体类型 # x-requested-with:判断请求是异步还是同步 自定义header 比如 token) add_header Access-Control-Allow-Headers $http_access_control_request_headers; # 浏览器缓存请求头信息,1800秒内,只会有1次请求,不会出现"OPTIONS"预请求,节约资源 add_header Access-Control-Max-Age '1800'; if ($request_method = 'OPTIONS') { return 204; } # 服务端HttpServletRequest可以获得用户的真实ip proxy_set_header X-Real-IP $remote_addr; # 服务端HttpServletRequest可以获得用户的真实ip和经过的每一层代理服务器的ip proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 服务端接收的请求头Host值不变 proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; } } }
events 配置块
-
events 模块中包含了 nginx 中所有处理连接的设置
-
常用的配置项如下:
#user nobody; worker_processes 1; # ... events { use epoll; accept_mutex on; multi_accept on; worker_connections 20000; client_header_buffer_size 4k; open_file_cache max=2000 inactive=60s; open_file_cache_valid 60s; open_file_cache_min_uses 1; }
-
use 指令
用来指定要使用连接处理方法,通常nginx会默认使用最有效的连接方式“use epoll;”
指定使用epoll 的 I/O
模型来处理连接,nginx 针对不同的操作系统,有不同的事件模型- 标准事件模型:Select、poll 属于标准事件模型,如果当前系统不存在更有效的方法,nginx 会选择 select 或 poll
- 高效事件模型:
Kqueue
,Epoll
,/dev/poll
,Eventport
-
accept_mutex指令
:用来设置网路连接序列化,防止惊群现象发生,默认为on- 惊群效应(thundering herd)是指多进程(多线程)在同时阻塞等待同一个事件的时候(休眠状态),如果等待的这个事件发生,那么就会唤醒等待的所有进程(或者线程),但是最终却只能有一个进程(线程)获得这个时间的“控制权”,对该事件进行处理,而其他进程(线程)获取“控制权”失败,只能重新进入休眠状态,这种现象和性能浪费就叫做惊群效应。
-
multi_accept指令
:设置是否允许同时接受多个网络连接,只能在 events 模块设置- Nginx 服务器的每个工作进程可以同时接受多个新的网络连接,但是需要在 nginx.conf 配置文件中配置,此指令默认为关闭(multi_accept on)即默认为一个工作进程只能一次接受一个新的网络连接,,打开后可同时接受多个链接。
-
worker_connections指令
:设置工作进程的最大连接数,理论上每台 Nginx 服务器的最大连接数为worker_processes*worker_connections
,worker_processes
为指定的工作进程数量。 -
open_file_cache指令
:配置缓存,为打开的文件指定缓存,默认是没有启用的max
参数指定缓存最大数量inactive
参数指经过多长时间文件没有被请求(或没有被使用)后删除缓存。- 打开文件最大数量为在 main 配置的
worker_rlimit_nofile
参数,worker_rlimit_nofile
参数用来为工作进程设置文件描述符的限制。
-
open_file_cache_valid指令
:配置多长时间检查一次缓存的有效信息。如果有一个文件在 inactive 时间内一次都没被使用,它将被从缓存移除 -
open_file_cache_min_uses指令
配置缓存中的文件在open_file_cache
指令中的inactive
参数(open_file_cache max=2000 inactive=60s
;)指定的时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的。如果有一个文件在 inactive 时间内一次没被使用,它将被从缓存移除
http 配置块用来配置 http
- http 模块从外到内有 http模块、server模块、location模块,同时各个模块有各自的属性和元素
- http模块:即一个 http 处理模块,可进行 http 的相关参数配置,内可以包含多个 server 块
- server模块:即是一个虚拟主机,需配置域名和端口,也只处理对应主机域名的 http 请求,内可包含多个 location 块;
- location模块:对应具体的路径请求(http请求)
常见的 http 模配置项信息:
#user nobody;
worker_processes 1;
# ...
events {
# ...
}
http {
include mime.types;
default_type application/octet-stream;
# 配置日志
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/host.access.log main;
keepalive_timeout 65;
# 配置负载均衡
upstream myServer{
server 192.168.247.129:8080 max_fails=3 weight=2;
server 192.168.247.129:8081 max_fails=3 weight=4;
}
# 配置一个虚拟主机
server {
listen 80;
server_name 192.168.247.129;
charset koi8-r;
access_log logs/host.access.log main;
# 配置请求地址
location / {
#...
}
# 配置请求地址
location /report/ {
#...
}
error_page 500 502 503 504 /50x.html;
}
# 配置虚拟主机
server {
listen 80;
server_name 192.168.247.130;
#...
}
}
-
include指令
用来将另一个文件或与指定通配符匹配的文件包含到当前配置中,包含的文件应该由语法正确的指令和块组成。用法示例:include mime.types; include vhosts/*.conf;
-
mime.types 文件内定义指定文件头所对应的文件格式,需要新增自定义的文件类型,可以在 mime.types 文件内进行定义。
types { text/html html htm shtml; text/css css; text/xml xml; image/gif gif; # ... }
-
在启动一个 nginx 服务时,可能 nginx 会同时处理多个 server 虚拟主机,为了使多个 server 的配置更加便于管理,一般会在 http 配置块中通过 include 指令引入其他的 server 配置文件,相当于把其他配置文件的内容写入到了当前的配置文件中。用法示例:
- 将下面配置内容保存到
conf/server_demo.conf
文件中server { listen 8888; server_name localhost; location / { root html; index index.html index.htm; } }
- 在
nginx.conf
配置文件中使用 include 指令引用conf/server_demo.conf
文件。http { include mime.types; default_type application/octet-stream; # 引用文件 include server_demo.conf; }
- 将下面配置内容保存到
-
default_type指令
:设置默认文件类型- nginx 会根据 mime 类型定义的对应关系来告诉浏览器如何处理服务器传给浏览器的文件,如:打开文件、下载文件。
- 如果 web 程序没有设置 mime 类型,nginx 也没对应文件的扩展名。此时,使用 nginx.conf 中 default_type 指令定义的默认处理方式。
# nginx默认文件类型 default_type application/octet-stream;
- 在 nginx 中,mime 类型和文件扩展名的对应关系一般放在 conf/mime.types 文件中,然后用 include 指令来加载,mime.types 文件中用 types 指令定义的 mime 和 文件扩展名对应关系
# 引入 conf/mime.types 文件内容 include mime.types;
-
log_format指令
:nginx 的日志包含了两类:一类是 error.log 日志,另一类是 access.log 日志。log_format 指令用来定义 access.log 指令记录的访问日志格式log_format main '$upstream_addr $upstream_status $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main;
- log_format 指令后面的 main 为日志格式名称,在 access_log 中可以通过该名称引用。
-
access_log指令
用来指定 nginx 访问日志文件的路径及使用的何种日志格式记录日志log_format main '$upstream_addr $upstream_status $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main;
-
keepalive_timeout指令
:- HTTP 是一种无状态协议,客户端向服务器发送一个 TCP 请求,服务端响应完毕后断开连接。
- 如果客户端向服务器发送多个请求,每个请求都要建立各自独立的连接以传输数据。
- HTTP 有一个 KeepAlive 模式,它告诉 web 服务器在处理完一个请求后保持这个 TCP 连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。
- KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源,占用过多就会影响性能。
- Nginx 使用 keepalive_timeout 来指定 KeepAlive 的超时时间(timeout),指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为 0,就禁止了 keepalive 连接。
# 可用于 http, server, location 配置块 # 设置 TCP 链接保持 60 秒 keepalive_timeout 60s;
-
upstream指令
:定义一组服务器,这些服务器可以监听不同的端口。而且,监听在 TCP 和 UNIX 域套接字的服务器可以混用upstream myserver { server demo1.hxstrive.com weight=5; server demo2.hxstrive.com max_fails=3 fail_timeout=30s; server unix:/tmp/myserver3; }
-
server指令
: server 配置块是配置虚拟主机的重要参数块,每个 https 全局块可以包含多个 server 块,而每个 server 配置块就相当于一台虚拟主机