nginx 配置文件详细解释

最近在项目中有用到 nginx,于是对 nginx.conf 的配置文件详细了解一翻,在conf文件中加了一些注释,在此记录,方便以后使用,如果有读者觉得有用的话,可以直接复制/粘贴~~

#nginx 用户及组:用户/组
#user  nobody;
#工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU数量
worker_processes  1;

#错误日志:存放路径
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# pid(进程标识符) 存放路径
#pid        logs/nginx.pid;

#指定进程可以打开的最大描述符:数目
#这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit-n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。

#现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。

#这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。
#worker_rlimit_nofile 204800;

events {
	#使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
	#use epoll;
	#每个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。
	#每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。worker_processes*worker_connections
    worker_connections  1024;
	#keepalive 超时时间
	keepalive_timeout 60;
	#客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
	#分页大小可以用命令getconf PAGESIZE 取得。
	#但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。
	client_header_buffer_size 4k;
	#这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
	open_file_cache max=65535 inactive=60s;
	#这个是指多长时间检查一次缓存的有效信息。
	open_file_cache_valid 80s;	#open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。
	open_file_cache_min_uses 1;
}

##设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
	#设定mime类型,类型由mime.type文件定义
    include       mime.types;
	
    default_type  application/octet-stream;
	#日志格式设置。
	#$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
	#$remote_user:用来记录客户端用户名称;
	#$time_local: 用来记录访问时间与时区;
	#$request: 用来记录请求的url与http协议;
	#$status: 用来记录请求状态;成功是200;
	#$body_bytes_sent :记录发送给客户端文件主体内容大小;
	#$http_referer:用来记录从那个页面链接访问过来的;
	#$http_user_agent:记录客户浏览器的相关信息;
	
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
	
	#用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径;
    #access_log  logs/access.log  main;

	#设定通过nginx上传文件的大小
	client_max_body_size 300m;
	
	#sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
    sendfile        on;
	#此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

	# 压缩打包返回到浏览器
    gzip  on;
	
	#nginx的upstream目前支持4种方式的分配

	#1、轮询(默认)

	#每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

	#2、weight
	#指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
	#例如:
	#upstream blog{
	#server 192.168.0.14 weight=10;
	#server 192.168.0.15 weight=10;
	#}

	#2、ip_hash
	#每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
	#例如:
	#upstream blog{
	#ip_hash;
	#server 192.168.0.14:88;
	#server 192.168.0.15:80;
	#}

	#3、fair(第三方)
	#按后端服务器的响应时间来分配请求,响应时间短的优先分配。
	#upstream blog{
	#server server1;
	#server server2;
	#fair;
	#}

	#4、url_hash(第三方)

	#按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

	#例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

	#upstream blog {
	##server squid1:3128;
	#server squid2:3128;
	#hash $request_uri;
	#hash_method crc32;
	#}

	#tips:

	##upstream blog{#定义负载均衡设备的Ip及设备状态}{
	#ip_hash;
	#server 127.0.0.1:9090 down;
	#server 127.0.0.1:8080 weight=2;
	#server 127.0.0.1:6060;
	#server 127.0.0.1:7070 backup;
	}
	#在需要使用负载均衡的server中增加
	#proxy_pass http://blog/;

	#每个设备的状态设置为:
	#1.down表示单前的server暂时不参与负载
	#2.weight为weight越大,负载的权重就越大。
	#3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
	#4.fail_timeout:max_fails次失败后,暂停的时间。
	#5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

	#nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

	#client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
	#client_body_temp_path设置记录文件的目录 可以设置最多3层目录

	#location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
	
	upstream blog {
		# weight 权重 被命中的概率 max_fails 熔断机制 连接超时次数超过 max_fails tomcat会被踢掉
		server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
		server 127.0.0.1:9090 weight=1 max_fails=2 fail_timeout=30s;
	}

	# -------http请求入口-------
    server {
		# 监听端口号 80
        listen       80;
		# 服务器域名
        server_name  www.clearlove7.com;
		# nginx 首页
		index index.jsp index.html idex.htm;
		# nginx 资源文件路径
		root /usr/local/nginx/html;
		
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            # index  index.html index.htm;
			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://blog;
        }
		
		location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
		{	
			# 动静分离
			# 静态资源访问路径
			root /usr/local/nginx/html;
			# 静态资源缓存到浏览器30天
			expires 30d;
		}

        #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 ~/group([0-9])/M00 {
            root  /data/fastdfs/data;
	    ngx_fastdfs_module;
        }
    }
    
    server {
	listen 81;
	server_name haida.test.com;
	location / {
		root html;
		index index.html index.htm;
	}	
    }

    server {
        listen 82;
        server_name nginx.test2.com;
        location / {
                root html;
                index index.html index.htm;
        }
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值