[Nginx]配置文件详解

#定义Nginx运行的用户和用户组
#user  nobody; 
user  root;

#nginx进程数,建议设置为等于CPU总核心数。
worker_processes  1; 

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#进程文件
#pid        logs/nginx.pid;

#工作模式与连接数上限
events {

#epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,
#如果跑在FreeBSD上面,就用kqueue模型。
use epoll;

#单个进程最大连接数(最大连接数=连接数*进程数)
    worker_connections  2048;
}

#设定http服务器
http {
    #文件扩展名与文件类型映射表
    include       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  logs/access.log  main;

    #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改 成off。
    sendfile        on;

    #防止网络阻塞
    # tcp_nopush     on;

    #长连接超时时间,单位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;

  # gzip压缩功能设置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
    gzip_vary on;
  
  # http_proxy 设置
    client_max_body_size   10m;
    client_body_buffer_size   128k;
    proxy_connect_timeout   75;
    proxy_send_timeout   75;
    proxy_read_timeout   75;
    proxy_buffer_size   4k;
    proxy_buffers   4 32k;
    proxy_busy_buffers_size   64k;
    proxy_temp_file_write_size  64k;
    proxy_temp_path   /usr/local/nginx/proxy_temp 1 2;

  # 设定负载均衡后台服务器列表 
    upstream  backend  { 
              #ip_hash; 
              server   192.168.10.100:8080 max_fails=2 fail_timeout=30s ;  
              server   192.168.10.101:8080 max_fails=2 fail_timeout=30s ;  
    }

  # 很重要的虚拟主机配置
    server {
        listen       80;
        #域名可以有多个,用空格隔开
        #server_name  localhost baidu.com google.com;
        server_name  itoatest.example.com;
        root   /apps/oaapp;

        #默认编码
        charset utf-8;

        #定义本虚拟主机的访问日志
        access_log  logs/host.access.log  main;

        #对 / 所有做负载均衡+反向代理
        location / {
            root   /apps/oaapp;
            index  index.jsp index.html index.htm;

            proxy_pass        http://backend;  
            proxy_redirect off;
            # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            proxy_set_header  Host  $host;
            proxy_set_header  X-Real-IP  $remote_addr;  
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
            
        }

        #对 "/v1" 启用反向代理
        location ^~ /v1/ {
        proxy_pass http://127.0.0.1:20010/v1;;
        proxy_redirect off;
        # pass on real client's IP
        proxy_set_header X-Real-IP $remote_addr;

        #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        #以下是一些反向代理的配置,可选。
        proxy_set_header Host $host;
        #允许客户端请求的最大单文件字节数
        client_max_body_size 10m; 
        #缓冲区代理缓冲用户端请求的最大字节数,
        client_body_buffer_size 128k; 
        #nginx跟后端服务器连接超时时间(代理连接时)
        proxy_connect_timeout 90; 
        #后端服务器数据回传时间(代理发送超时)
        proxy_send_timeout 90; 
        #连接成功后,后端服务器响应时间(代理接收超时)
        proxy_read_timeout 90; 
        #设置代理服务器(nginx)保存用户头信息的缓冲区大小
        proxy_buffer_size 4k; 
        #proxy_buffers缓冲区,网页平均在32k以下的设置
        proxy_buffers 4 32k; 
        #高负荷下缓冲大小(proxy_buffers*2)
        proxy_busy_buffers_size 64k; 
        #设定缓存文件夹大小,大于这个值,将从upstream服务器传
        proxy_temp_file_write_size 64k;
        }

        #静态文件,nginx自己处理,不去backend请求tomcat
        location  ~* /download/ {  
            root /apps/oa/fs;  
            
        }
        location ~ .*.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$   
        {   
            root /apps/oaapp;   
            expires      7d; 
        }
           location /nginx_status {
            stub_status on;
            access_log off;
            allow 192.168.10.0/24;
            deny all;
        }

        location ~ ^/(WEB-INF)/ {   
            deny all;   
        }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }

  ## 其它虚拟主机,server 指令开始
}
原文地址:https://www.cnblogs.com/landv/p/11224167.html