Nginx各项配置的含义

#user nobody; #配置用户或者组,默认为nobody nobody
worker_processes 4; #允许生成的进程数,默认为1
worker_cpu_affinity 00000001 00000010 00000100 00001000; #为每个进程分配一个CPU
worker_rlimit_nofile 102400; #为nginx工作进程改变打开最多文件描述符数目的限制。用来在不重启主进程的情况下增加限制。

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #指定日志路径,级别。这个设置可以放入全局块,http块,server块,级别依次为:debug|info|notice|warn|error|crit|alert|emerg

#pid logs/nginx.pid; #指定nginx进程运行文件存放地址

 

events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
use epoll; #使用epoll(linux2.6的高性能方式)事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 102400; #最大连接数,默认为512
}


http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
lua_package_path "/usr/local/lib/lua/?.lua;;"; #lua库位置
charset utf-8; #字符集

server_names_hash_bucket_size 128; # 保存服务器名字的hash表
client_header_buffer_size 4k; #用来缓存请求头信息的,容量4K,如果header头信息请求超过了且没有配置client_header_buffer_size,nginx会直接返回400错误
large_client_header_buffers 4 32k; #如果large_buffer还是无法容纳,那么就会返回414(处理request_line)/400(处理request_header)错误
client_max_body_size 300m; #允许客户端请求的最大单文件字节数

tcp_nodelay on; #提高数据的实时响应性
client_body_buffer_size 512k; #缓冲区代理缓冲用户端请求的最大字节数(请求多)


proxy_connect_timeout 5s; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 60s; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_send_timeout 5s; #后端服务器数据回传时间(代理发送超时)

proxy_buffer_size 16k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 64k; #该指令设置缓冲区的大小和数量,从被代理的后端服务器取得的响应内容,会放置到这里
proxy_busy_buffers_size 128k; #所有处在busy状态的buffer size加起来不能超过proxy_busy_buffers_size
proxy_temp_file_write_size 128k; #如果response的内容很大的话,Nginx会接收并把他们写入到temp_file里去。busy的buffer传输完了会从temp_file里面接着读数据,直到传输完毕

gzip on; #NGINX可以压缩静态资源
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2; #压缩级别大小,最小1,最大9,值越小,压缩后比例越小,CPU处理更快; 值越大压缩后占用带宽越少。
gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型:text js css xml 都会被压缩
gzip_vary on; #作用是在http响应中增加一行,目的是改变反向代理服务器的缓存策略

#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/access.log main;
#access_log off; #取消服务日志

#日志格式
#    ip 远程用户 当地时间 请求URL 状态 发送的大小 响应的头 客户端使用的浏览器 页面响应的时间
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $request_time $http_x_forwarded_for'; #自定义格式
access_log logs/access.log myFormat; #combined为日志格式的默认值

sendfile on;    #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块
#sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限

#tcp_nopush on;
tcp_nopush on; #防止网络阻塞

#keepalive_timeout 0;
keepalive_timeout 65;    #连接超时时间,默认为75s,可以在http,server,location块

#gzip on;

#上游服务器
upstream mysvr { 
#负载均衡算法,默认为round-robin轮循
ip_hash; #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题,ip_hash不支持weight和backup

server 192.168.5.91:7878 max_fails=2 fail_timeout=10s;
server 192.168.5.92:7878 max_fails=2 fail_timeout=10s;

#server 192.168.5.91:7878 max_fails=2 fail_timeout=10s weight=1;
#server 192.168.5.92:7878 max_fails=2 fail_timeout=10s weight=2; 
#server 192.168.5.90:7878 backup; #热备
}

#error_page 404 https://www.baidu.com; #错误页

server {
keepalive_requests 120; #单连接请求上限次数
listen 9080; #监听端口
server_name localhost;    #监听地址 127.0.0.1
#charset koi8-r;

#access_log logs/host.access.log main;


#location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
# #root path; #根目录
# #index vv.txt; #设置默认页
# proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
# deny 127.0.0.1; #拒绝的ip
# allow 172.18.5.54; #允许的ip 
#} 



location /test {

proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_next_upstream_timeout 10s;
proxy_next_upstream_tries 2;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header upstream_addr $upstream_addr;

proxy_pass http://mysvr;

}

#nginx主页
location / {
root html;
index index.html index.htm;
}

#用lua脚本向reids存值
location /lua/set {
default_type 'text/plain'; 
content_by_lua_file conf/lua/setKeyValue.lua;
}

#用lua脚本从reids取值
location /lua/get {
default_type 'text/plain'; 
content_by_lua_file conf/lua/getKey.lua;
}

#静态资源代理
location ~ .*.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /var/local/static;
expires 30d;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}
原文地址:https://www.cnblogs.com/jpfss/p/10232901.html