超详细nginx配置文件

user    work work;    //运行nginx的用户(组)

worker_processes  auto;   //work进程数1个主进程
pid        /opt/soft/nginx/run/nginx.pid; //pid存放位置
error_log  /opt/log/nginx/localhost/error.log error; //全局nginx运行时 error  info,notice,warn,error,crit,alert,emerg//所有指定的文件,运行nginx进程的用户必须有写权限,否则报错
worker_rlimit_nofile 65535;#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
include  /opt/soft/nginx/conf.d/*.conf;   //引入配置文件
events {                //负责网络连接配置 ,比较影响性能
    accept_mutex   on;   //防止多个进程对连接的争抢  默认开启,(影响性能)
    multi_accept  on;  //配置是否允许一个worker_process一次同时接收多个网络连接   默认off 不允许
    use epoll;   //事件驱动模型  select,poll,kqueue,epoll,rtsig,有些模块需手动安装
    worker_connections  1024; //最大连接数允许每个worker_process同时开启的最大连接数(包括所有可能的连接数),该值不能大于操作系统支持打开的最大文件句柄数
}
http {
    server_tokens off;//隐藏nginx的版本号,并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。
    server_tag off;//隐藏nginx的信息
    include       mime.types; //前端请求的资源类型
    default_type  application/octet-stream;  //处理前端请求默认的类型
    charset utf-8;
    # nginx log format.
    log_format  main  '$http_host $remote_addr - $remote_user [$time_local] '

      '$scheme "$request" $status $bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$gzip_ratio" $request_time '
                      '$upstream_addr $upstream_status $upstream_response_time'; //服务日志的格式
  access_log /opt/log/nginx/localhost/access.log main;  //服务日志路径,格式选择
  error_log  /opt/log/nginx/localhost/error.log error; 
  ssl_session_cache   shared:SSL:10m;
  ssl_session_timeout  10m;
  autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
  sendfile        on;   //开启sendfile高效传输文件的模式  默认关闭
  sendfile_max_chunk  512k;   //设置每个worker_process传输文件的最大值  默认0   无限制
   tcp_nopush     on;//tcp_nopush需要和sendfile一起使用,作用是:让HTTP响应头和sendfile()发送的数据混合为一个数据包发送出去。
   tcp_nodelay    on;//用于覆盖Nagle's算法.必须开启keepalive才能使用这个指令否则不能使用
   keepalive_timeout  15s;    //配置客户端连接超时时间 默认75s
   keepalive_requests  60;  //建立某连接允许发送的请求次数  默认100
   server_names_hash_max_size 512;//服务器名字的hash表大小
   server_names_hash_bucket_size 512;#服务器名字的hash表大小和上个值保持一致
   client_header_buffer_size 4k;#上传文件大小限制
   large_client_header_buffers 4 256k; #设定请求缓
   client_header_timeout  10;//请求头的超时时间
 // allow   192.33.33.3;  设置允许访问nginx的Ip 设置多个Ip使用多次allow   all 代表所有 可配置在http    ,server,location
 //deny  192.33.33.3;  禁止访问的IP   
 //auth_basic   string|off ;  开启密码访问nginx
 //auth_basic_user_file  file;  设置密码访问的密码文件的绝对路径
    gzip on;//开启压缩功能,默认off
    gzip_min_length 1k;//大于1k才压缩
    gzip_buffers     16   8k;//压缩申请的空间个数和大小
    gzip_comp_level 4;//压缩等级 1-9   9代表压缩程度最高
    gzip_http_version 1.0;//1  以上版本使用压缩,默认1.1
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript     image/gif image/png;//根据类型压缩
    gzip_vary on;//告知本不支持gzip压缩的浏览器,数据压缩了
    map_hash_max_size 102400;
    map_hash_bucket_size  128;
    fastcgi_intercept_errors on;//开启错误页面跳转,默认关闭
server {
       listen  80;  //配置网络监听  可监听IP,端口
       #listen 443 ssl ;//配置https
       server_name  uums.58qf.com;   虚拟主机名称,可多个  空格隔开 支持正则
       #add_header Strict-Transport-Security "max-age=31536000; includeSubDomains"; #HSTS强制使用HTTPS
       error_page  500 404 http://m.58qf.com/error.html;  //设置错误页面 /404.html 
      //error_page 404  /404.html;// 指安装路径下的/html/404.html

//下面的配置指根路径下的/usr/html/404.html;                
        //location  /404.html
        //{
        //    root  /usr/html/            
        //}
        //
     access_log /opt/log/nginx/uums.58qf.com/uums.**.com_access.log main;
      error_log /opt/log/nginx/uums.58qf.com/uums.**.com_error.log error;
 location / {   //支持正则
          proxy_next_upstream http_502  error timeout invalid_header;
          proxy_set_header Host $host;//如果客户端发过来的请求的header中有’HOST’这个字段时,$http_host和$host都是原始的’HOST’字段 ,没有使用$host
          poxry_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;  //获取HTTP的请求端真实的IP      
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-Proto  $scheme;
          proxy_pass http://test;
     }
upstream test{
    ip_hash;
    server  172.16.11.120:9014  weight=1 max_fails=1 fail_timeout=10s;
    server  172.16.11.120:9014   weight=2 max_fails=1 fail_timeout=10s;
}

猜你喜欢

转载自blog.csdn.net/ch5057997/article/details/79351416
今日推荐