nginx配置实例解析

Posted on 2022-12-12 16:43  brad1208  阅读(131)  评论(0编辑  收藏  举报

原配置:(拷贝使用)

user nginx;
worker_processes 64;
worker_rlimit_nofile 100000;

error_log /var/log/nginx/error.log warn;
pid    /var/run/nginx.pid;


events {
  worker_connections 20480;
  multi_accept on;
  use epoll;
}


http {
  include    /etc/nginx/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 /var/log/nginx/access.log main;
  #access_log off;
  error_log /var/log/nginx/error.log crit;
  server_tokens  off;
  sendfile    on;
  client_max_body_size  128M;
  #tcp_nopush   on;
  tcp_nodelay   on;
  keepalive_timeout 60;
  client_header_timeout 10;
  client_body_timeout 10;
  reset_timedout_connection on;
  send_timeout 30;
  proxy_connect_timeout 60;
  proxy_read_timeout 60;
  proxy_send_timeout 60;

  limit_conn_zone $binary_remote_addr zone=addr:8m;
  limit_conn addr 400;
  ssl_session_cache shared:SSL:10m;
  ssl_session_timeout 5m;

  gzip on;
  gzip_disable "msie6";
  # gzip_static on;
  gzip_proxied any;
  gzip_min_length 1000;
  gzip_comp_level 6;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  open_file_cache max=100000 inactive=20s;
  open_file_cache_valid 30s;
  open_file_cache_min_uses 2;
  open_file_cache_errors on;
  upstream teamapi{
server 10.123.123.123:5000;
server 10.223.223.223:5000;
#    ip_hash;
  }
  include /etc/nginx/conf.d/*.conf;
}


参数配置解析:

user www; #运行用户
worker_processes 64; #nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数
worker_rlimit_nofile 100000; #当一个nginx进程打开的最多文件描述符数目,理论值应该是系统的最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。

error_log /var/log/nginx/error.log warn; #错误日志位置和日志级别

#error_log 级别分为 debug, info, notice, warn, error, crit 默认为crit, 该级别在日志名后边定义格式如下:
#error_log /var/log/nginx/error.log crit; 
#crit 记录的日志最少,而debug记录的日志最多。如果你的nginx遇到一些问题,比如502比较频繁出现,但是看默认的error_log并没有看到有意义的信息,那么就可以调一下错误日志的级别,当你调成error级别时,错误日志记录的内容会更加丰富。


pid/var/run/nginx.pid; #指定nginx服务的pid文件所在位置

events {
worker_connections 20480; #每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。

multi_accept on;
#这个数值默认就是on建议采用默认设置, multi_accept的作用是告诉nginx收到一个新连接通知后接受尽可能多的连接,多个worker按串行方式来处理连接,也就是一个连接只有一个worker被唤醒,其他的处于休眠状态
#设置为off后,多个worker按并行方式来处理连接,也就是一个连接会唤醒所有的worker,直到连接分配完毕,没有取得连接的继续休眠。当你的服务器连接数不多时,开启这个参数会让负载有一定的降低,但是当服务器的吞吐量很大时,为了效率,可以关闭这个参数。

use epoll; #使用epoll的I/O模型,用这个模型来高效处理异步事件
}


http {
 include /etc/nginx/mime.types; #媒体类型,include 只是一个在当前文件中包含另一个文件内容的指令。
 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 /var/log/nginx/access.log main; #nginx访问日志,用来指定日志文件的路径及使用何种日志格式记录日志
 #access_log off;
 error_log /var/log/nginx/error.log crit; #nginx错误日志及输出的日志级别
 server_tokens off; #隐藏响应头中的有关操作系统和web server(Nginx)版本号的信息,这样对于安全性是有好处的。
 sendfileon; #可以让sendfile()发挥作用。sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。Pre-sendfile是传送数据之前在用户空间申请数据缓冲区。之后用read()将数据从文件拷贝到这个缓冲区,write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到OS缓存。因为这种拷贝是在内核完成的,sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效(更多有关于sendfile)。
 client_max_body_size 128M;
 #tcp_nopush  on; #告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值
 tcp_nodelay  on; #告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。
 keepalive_timeout 60; #http连接超时时间,默认是60s,功能是使客户端到服务器端的连接在设定的时间内持续有效,当出现对服务器的后继请求时,该功能避免了建立或者重新建立连接。切记这个参数也不能设置过大!否则会导致许多无效的http连接占据着nginx的连接数,终nginx崩溃。
 client_header_timeout 10; #接收客户端header超时, 默认60s, 可以指定其它时间,如果限定时间内没有收到完整的http包头, 返回408
 client_body_timeout 10; #指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒,或其它时间


 reset_timedout_connection on; #不建议配置,详见:https://cloud.tencent.com/developer/article/1842805
 send_timeout 30; #send_timeout默认时间60s,当nginx向客户端发送了响应,但是一直等不到客户端的确认,超过send_timeout的时间后,nginx将关闭这个连接,这个时候就是nginx主动断开的连接


 proxy_connect_timeout 60; #nginx与upstream server的连接超时时间
 proxy_read_timeout 60; #nginx接收upstream server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭
 proxy_send_timeout 60; #nginx发送数据至upstream server超时, 默认60s, 如果连续的60s内没有发送1个字节, 连接关闭

 limit_conn_zone $binary_remote_addr zone=addr:8m; #限制并发连接数以及下载带宽
 limit_conn addr 400;
 ssl_session_cache shared:SSL:10m; #设置存储session参数的缓存的类型和大小
 ssl_session_timeout 5m; #考虑到APP操作习惯及安全性暂定60分钟,这个默认5分钟(5m),一般为30分钟到4小时,如果是网页形式可以时间更长一般不超过24小时,多了有安全隐患

 gzip on; #开启gzip压缩功能
 gzip_disable "msie6";
 # gzip_static on;
 gzip_proxied any;
 gzip_min_length 1000;
 gzip_comp_level 6;
 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

 open_file_cache max=100000 inactive=20s; #这个指令指定缓存是否启用,默认未启用,参数详解:http://t.zoukankan.com/jking10-p-4379407.html
 open_file_cache_valid 30s; #这个是指多长时间检查一次缓存的有效信息。也就是说即使我一直访问这个文件,30s后会检查此文件的更改信息是否变化,发现变化就更新。
 open_file_cache_min_uses 2; #指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件更改信息一直是在缓存中打开的。
 open_file_cache_errors on; #这个指令指定是否在搜索一个文件时记录cache错误

 


#下方为配置服务器负载均衡,可以配多个
 upstream teamapi{
server 10.123.123.123:5000;
server 10.223.223.223:5000;
#ip_hash;
 }

 include /etc/nginx/conf.d/*.conf; #nginx子配置服务存放位置
}

 


项目子配置:

server {
  listen 80;
  server_name www.xxxxx.com;
  rewrite ^/(.*)$ https://www.xxxxx.com/$1 permanent;
  }
server {
  listen 443 ssl;
  server_name www.xxxxx.com;
  ssl off;
  allow 123.123.123.123;
  #deny all;
  ssl_certificate /cert/www.xxxxx.com.pem;
  ssl_certificate_key /cert/www.xxxxx.com.key;
  location / {
    add_header Access-Control-Allow-Origin *;
    #add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    #add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
    proxy_pass http://123.123.123.123:1000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection keep-alive;
    proxy_set_header Host $host;
    proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
    proxy_set_header X-real-ip $remote_addr;
    proxy_cache_bypass $http_upgrade;
  }

}