nginx配置文件nginx.conf 不包括server节点

时间:2023-12-06 14:38:32

整理的一个nginx.conf的配置,不包括server节点介绍

原文请查看,Nginx配置文件(nginx.conf)配置详解

# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/ #nginx用户及组,window下不指定。user用户 组 ;
user nginx;
#nginx的工作进程数,数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid; #指定进程可以打开的最大描述符,我这里设置int最大值,可不设置
#worker_rlimit_nofile 65535; # Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; events {
# 每个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。
# 每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。worker_processes*worker_connections
worker_connections 1024;
# 使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
# 不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的
use epoll;
# 告诉nginx收到一个新连接通知后接受尽可能多的连接。
multi_accept on;
#keepalive超时时间。
keepalive_timeout 60; #客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
#分页大小可以用命令getconf PAGESIZE 取得。client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。
client_header_buffer_size 4k; # 为打开文件指定缓存,默认是没有启用的,
# max指定缓存数量,建议和打开文件数一致,
# inactive是指经过多长时间文件没被请求后删除缓存。
open_file_cache max=65535 inactive=60s; # 多长时间检查一次缓存的有效信息。
open_file_cache_valid 80s;
# open_file_cache指令中的inactive参数时间内文件的最少使用次数,
# 如果超过这个数字,文件描述符一直是在缓存中打开的,
# 如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除
open_file_cache_min_uses 1;
} # 设定http服务器,利用它的反向代理功能提供负载均衡支持
http { #设置日志记录格式,
# 远程访问地址-访问主机名[访问时间] url和http协议
# 请求状态 requestBody内容大小 那个页面链接访问过来
# 客户浏览器的相关信息 http forward头部ip地址
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 /var/log/nginx/access.log main;
# 保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的
# 如果需要增大hash max size 或 hash bucket size,我们需要增大server_names_hash_max_size的值
server_names_hash_bucket_size 128; # 客户端请求头部的缓冲区大小
client_header_buffer_size 4k;
# 客户端请求头的缓冲区的最大数量和大小
large_client_header_buffers 8 128k; open_file_cache max=102400 inactive=20s;
#设定通过nginx上传文件的大小
client_max_body_size 300m; #指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件。普通应用,必须设为on
#用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime
sendfile on;
#此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048; #代理服务器连接的超时时间_发起握手等候响应超时时间
proxy_connect_timeout 90;
#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
proxy_read_timeout 180;
#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
proxy_send_timeout 180; # 设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头。
# 默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小
proxy_buffer_size 256k; # 设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小
proxy_buffers 4 256k; #设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_temp_path /data0/proxy_temp_dir; # proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
# 设置内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
# 如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常
# 默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,再提交256k图片则会报500异常
client_body_buffer_size 512k; # 默认为off , 表示使nginx阻止HTTP应答代码为400或者更高的应答。指定的error_page错误页面才能生效
proxy_intercept_errors on # 设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
#这是一个无效的域名,默认访问http://localhost时,如果找不到匹配的域名,则会访问第一个server节点,可以取任意匹配不到的字符串
#比如可以设置server_name s; ,,这个也可以使用localhost正确访问到nginx欢迎页面
root /usr/share/nginx/html; # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location / {
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
} # Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# } }

2018.12.18增加额外的gzip模块

gzip模块压缩指定格式文件,以减小传输带宽

作用域:http,server,Location

可以在http节点下配置,或server节点下配置,nginx的配置是内层高于外层,

如果gzip模块在http下关闭gzip off, 而server开启的gzip on ,最后是由内层的server决定生效,开启gzip模块

gzip配置参数:

http {
# gzip默认开启,默认作用于text/html格式的文件(后缀html htm shtml默认压缩),可以自定义gzip_types设置想要压缩的文件格式
gzip on;
# 设置压缩响应所需的最小http协议版本1.1或1.0,默认值是1.1
gzip_http_version 1.1;
# 指定允许压缩的文件类型,文本内容css,xml,json,javascript可以进行压缩
gzip_types text/plain text/css application/xml application/json text/javascript application/javascript application/x-javascript application/xml+rss;
# nginx作为反向代理时候设置哪个条件启动gzip
gzip_proxied expired no-cache no-store private auth;
# gzip_disable的设置是禁用IE6的gzip压缩
gzip_disable "MSIE [1-6]\.";
# 指定压缩的文件最小尺寸,单位 bytes 字节,低于该值的不压缩,超过该值的将被压缩
gzip_min_length 1k;
# 以16k为单位,按照原始数据大小以16k为单位的4倍申请内存,默认值是申请跟原始数据相同大小的内存空间去存储gzip压缩结果。
gzip_buffers 4 16k;
# 指定压缩等级,其值从1到9,数字越大,压缩率越高,越消耗CPU,负载也越高
# 一般使用1-4等级
gzip_comp_level 2;
# 增加响应头Vary:Accept-Encoding,可以根据头信息返回压缩和非压缩副本
gzip_vary on; }

额外补充

gzip_proxied   expired no-cache no-store private auth;

指定当nginx作为代理服务器时,哪些条件下启动gzip

# Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含"Via"的 header头。
# off - 关闭所有的代理结果数据的压缩
# expired - 启用压缩,如果header头中包含 "Expires" 头信息
# no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息
# no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息
# private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息
# no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息
# no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息
# auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息
# any - 无条件启用压缩

via请求头是用来通知中间网关或代理服务器地址,Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)

via响应头告知代理客户端响应是通过哪里发送的,Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)

gzip_disable “MSIE [1-6]\.”
指定那个在哪个浏览器上不使用gzip压缩,MSIE是IE1-6的浏览器发送请求时,User-Agent中携带的浏览器标识,指定ie6及以下浏览器不使用gzip压缩

gzip_vary on

启用应答头"Vary: Accept-Encoding"告诉代理服务器缓存两种版本的资源:压缩和非压缩,这有助于一些公共代理不能正确检测到 content-encoding:gzip 响应头的信息

正常的请求响应逻辑

nginx配置文件nginx.conf 不包括server节点

客户端和服务器之间直接请求响应过程。因为上面nginx服务器使用gzip压缩的最小支持是http1.1,所以这个请求响应过程没有问题

而如果客户端和服务器之间有代理服务器,代理服务器到服务器使用的是http/1.0协议,结果服务器返回的是非压缩副本资源到代理服务器,代理服务器再转到客户端。

结果是这个gzip功能没有生效。过程如下图

nginx配置文件nginx.conf 不包括server节点

所以,如果在有代理服务器,网关的情况下,要使用gzip压缩,则需要将gzip_http_version改为1.0