nginx配置文件-详解
基本模块配置
1.main:用于进行nginx全局信息的配置
2.events:用于nginx工作模式的配置
3.http:用于进行http协议信息的一些配置
4.server:用于进行服务器访问信息的配置
5.location:用于进行访问路由的配置(请求转发)
6.upstream:用于进行负载均衡的配置
#注释
通过location指定不同的后缀名实现不同的请求转发
nginx是通过轮询来转发的,查找对应配置的服务再转发。
配置文件详解
#定义nginx运行的用户和用户组
user nginx name;
worker_processes auto;
#worker_processes 8; #启动进程,通常设置成和cpu的数量相等
#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; #为每个进程分配cpu,下面将8个进程分配到8个cpu,也可以写多个,也可以将一个进程分配到多个cpu中。
#全局错误日志及pid
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
#错误日志定义等级:[debug | info | notice | warn | error | crit]
#error_log /var/log/nginx/error.log warn; warn错误日志的等级
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
#工作模式及连接数上线
events {
use epoll; #多路复用IO,可以大大提高nginx的性能
worker_connections 102400;
multi_accept on ; #尽可能多的接受请求
}
# worker_connections 102400;
#单个后台worker_process进程的最大并发连接数(最大连接数=连接数*进程数,这里就是102400*8//worker_connections 102400*worker_processes 8)
#设定http服务器,利用它的反向代理功能提供负载均衡
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 /var/log/nginx/access.log main;
#指令指定nginx是否调用sendfile函数(zerocopy方式)来传输文件,普遍应用都是设置成on,如果nginx主要是用来下载,就用off,关闭,可以平衡磁盘与网络I/O处理速度,降低系统的uptime,相当于降低系统I/O
sendfile on;
tcp_nopush on; #防止网络阻塞
tcp_nodelay on; #提高数据的实时响应性
keepalive_timeout 65; #超时时间,客户连接到服务器端的连接持续有效时间, keepalive_timeout 可避免建立/重新建立连接(保持连接的时间)
types_hash_max_size 2048; #混淆数据,影响三列冲突率,值越大消耗内存越多,散列key冲突率会降低,检索速度更快;值越小key,占用内存较少,冲突率越高,检索速度变慢
#引用mime类型
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;
upstream testproxys{
server 172.20.22.36:50001;
server 172.20.21.45:50001;
}
server {
listen 8887;
location / {
proxy_pass http://testproxys;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 2637 default backlog=4096;
server_name postranstest.jlpay.com;
charset utf-8;
proxy_ignore_client_abort on;
#set $request_trace_id set $request_trace_id trace-id-$pid-$connection-$bytes_sent-$msec;
location /request_trace_id_validate {
return 200 " without request_trace_id";
proxy_redirect off;
#proxy_set_header X-Request-Id $request_trace_id;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Real-IP $remote_addr;
}
location /nginx_status {
stub_status on;
}
}
#https需要的配置
# 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 {
# }
# }
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步