Nginx的一些常用配置

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

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

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
#worker_rlimit_nofile 65535;

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

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

    #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
    #use epoll;
}

#设定http服务器
http {
    #文件扩展名与文件类型映射表
    include       mime.types;

    #默认文件类型
    default_type  application/octet-stream;

    #默认编码
    #charset utf-8; 

    #服务器名字的hash表大小
    #server_names_hash_bucket_size 128; 

    #上传文件大小限制
    #client_header_buffer_size 32k; 

    #读取大型客户端请求头的缓冲区的最大数量和大小
    #large_client_header_buffers 4 64k; 

    #此指令设置nginx能处理的最大请求主体大小。 如果请求大于指定的大小,则nginx发回HTTP 413(Request Entity too large)错误。#如果服务器处理大文件上传,则该指令非常重要。
    #client_max_body_size 8m; 

    #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
    #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;

    #开启目录列表访问,合适下载服务器,默认关闭。
    #autoindex on;

    #防止网络阻塞,tcp_nopush 配置和 tcp_nodelay "互斥"。它可以配置一次发送数据的包大小。也就是说,它不是按时间累计  0.2 秒后#发送包,而是当包累计到一定大小后就发送。在 nginx 中,tcp_nopush 必须和 sendfile 搭配使用。
    #tcp_nopush     on;

    #防止网络阻塞 重要。。。
    #tcp_nodelay on; 

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


    #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。

    #为FastCGI缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间。
    #fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;

    #指定连接到后端FastCGI的超时时间。
    #fastcgi_connect_timeout 300;

    #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
    #fastcgi_send_timeout 300;

    #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。
    #fastcgi_read_timeout 300;

    #指定读取FastCGI应答第一部分 需要用多大的缓冲区
    #fastcgi_buffer_size 64k;

    #指定本地需要用多少和多大的缓冲区来 缓冲FastCGI的应答
    #fastcgi_buffers 4 64k;

    #默认值是fastcgi_buffers的两倍。
    #fastcgi_busy_buffers_size 128k;

    #在写入fastcgi_temp_path时将用多大的数据块
    #fastcgi_temp_file_write_size 128k;

    #gzip模块设置

    #开启gzip压缩输出
    #gzip  on;

    #最小压缩文件大小
    #gzip_min_length 1k;

    #压缩缓冲区
    #gzip_buffers 4 16k;

    #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    #gzip_http_version 1.0; 

    #压缩等级
    #gzip_comp_level 2;

    #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    #gzip_types text/plain application/x-javascript text/css application/xml;

    #用与设置Gzip功能是否发送带有“Vary:Accept-Encoding”头域的响应头部
    #gzip_vary on;

    #开启限制IP连接数的时候需要使用
    #limit_zone crawler $binary_remote_addr 10m; 

    #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
    # upstream coreqi.cn {
    # server 192.168.80.121:80 weight=3;
    # server 192.168.80.122:80 weight=2;
    # server 192.168.80.123:80 weight=3;
    # }

    #虚拟主机的配置
    server {

        #监听端口
        listen       80;

        #域名可以有多个,用空格隔开
        server_name  localhost;

        #应该是设置首页
        #index index.html index.htm index.php;

        #指定项目的根目录
        # root /data/www/ha97;

        # 配置php相关设置
        # location ~ .*.(php|php5)?$
        # {
        # fastcgi_pass 127.0.0.1:9000;
        # fastcgi_index index.php;
        # include fastcgi.conf;
        # }

        #图片缓存时间设置
        # location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
        # {
        # expires 10d;
        # }

        #JS和CSS缓存时间设置
        # location ~ .*.(js|css)?$
        # {
        # expires 1h;
        # }


        #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
        # log_format access '$remote_addr - $remote_user [$time_local] "$request" '

        #                   '$status $body_bytes_sent "$http_referer" '

        #                   '"$http_user_agent" $http_x_forwarded_for';

        #默认编码
        #charset koi8-r;

        #用来指定本虚拟主机访问日志文件的路径及使用的何种日志格式记录日志
        #access_log  logs/host.access.log  main;

        #对 "/" 启用反向代理(案例一)
        location / {
            root   html;
            index  index.html index.htm;
        }
        
        #对 "/" 启用反向代理(案例二)
        location / {

        #配置代理转发,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。
        proxy_pass http://127.0.0.1:88;

        #Nginx做反向代理,如果在header设置了Host参数,同时如果有协议和二级目录有不一致的情况的时候,
        #当后端服务做302、301跳转的时候,需要用proxy_redirect将后端设置在response header中的Location做转换.
        proxy_redirect off;

        #
        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状态的地址
        location /NginxStatus {
        stub_status on;
        access_log on;
        auth_basic "NginxStatus";
        auth_basic_user_file confpasswd;
        #htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
        }

        #本地动静分离反向代理配置
        #所有jsp的页面均交由tomcat或resin处理
        location ~ .(jsp|jspx|do)?$ {
        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_pass http://127.0.0.1:8080;
        }

        #所有静态文件由nginx直接读取不经过tomcat或resin
        location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { 
        expires 15d; 
        }
        
        location ~ .*.(js|css)?${ 
        expires 1h; 
        }

        #错误页面配置
        #error_page  404              /404.html;

        # 将服务器错误页重定向到静态页/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
        # 将PHP脚本代理到Apache侦听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
        # 将php脚本传递给fastcgi服务器,侦听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
        # 如果apache的文档根目录与nginx的文档根目录一致,则拒绝访问.htaccess文件
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    # 使用基于IP、名称和端口的配置组合的另一个虚拟主机
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    # HTTPS服务器
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

猜你喜欢

转载自www.cnblogs.com/fanqisoft/p/11244860.html