nginx 基本配置

#user  nobody;
worker_processes  1; #子进程,nginx启动会有1个master进程和若干个子进程

#error_log  logs/error.log;   [ debug | info | notice | warn | error | crit ] 日志级别
#error_log  logs/error.log  notice; 
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}


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        on;
    #tcp_nopush     on;#防止网络阻塞
    #tcp_nodelay on; #防止网络阻塞
    #keepalive_timeout  0;
    keepalive_timeout  65; #长连接超时时间,单位是秒

	#文件压缩
	#WEB 服务器接收到请求后判断浏览器是否支持压缩,如果支持就传送压缩后的响应内容,否则传送不经过压缩的内容
    #大大节省服务器的网络带宽,提升页面的浏览速度,浏览器获取响应内容后,判断内容是否被压缩,如果是则解压缩,然后显示响应页面的内容
	
    gzip  on;  
	#开启gzip压缩输出,开启后可以在开发者工具中看到响应头中 content-Encodin:gzip,文件减小
	
	gzip_comp_level 4;
	# 默认值:1(建议选择为4)
	# gzip压缩比/压缩级别,压缩级别 1-9,级别越高压缩率越大,当然压缩时间也就越长(传输快但比较消耗cpu)。
	
	gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif ;
	#gzip_types mime-type [mime-type ...]
	# 默认值: gzip_types text/html (默认不对js/css文件进行压缩),JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就行了
	# 压缩类型,匹配MIME类型进行压缩
	# 不能用通配符 text/*
	# (无论是否指定)text/html默认已经压缩 
	# 设置哪压缩种文本文件可参考 conf/mime.types
	
	gzip_min_length  1k;
	# 默认值: 0 ,不管页面多大都压缩
	# 设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。
	# 建议设置成大于1k的字节数,小于1k可能会越压越大。 即: gzip_min_length 1024
	
	gzip_vary on;
	# 和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩

	gzip_disable "MSIE [1-6].";
	# 禁用IE6的gzip压缩,又是因为杯具的IE6。当然,IE6目前依然广泛的存在,所以这里你也可以设置为“MSIE [1-5].”
	# IE6的某些版本对gzip的压缩支持很不好,会造成页面的假死,今天产品的同学就测试出了这个问题
	#后来调试后,发现是对img进行gzip后造成IE6的假死,把对img的gzip压缩去掉后就正常了
	#为了确保其它的IE6版本不出问题,所以建议加上gzip_disable的设置
	
	    ##cache##
	    #proxy_cache_path /nginx/nginx_cache/cache_one levels=1:2 keys_zone=cache_one:200m inactive=5s max_size=200g; #设置web缓存区名为cache_one,内存缓存空间大小为12000M,自动清除超过15天没有被访问过的缓存数据,硬盘缓存空间大小200g
		#inactive 在指定时间内没人访问则被删除,keys_zone 设置缓存名字和共享内存大小,evels 设置缓存文件目录层次;levels=1:2 表示两级目录,max_size 最大缓存空间,如果缓存空间满,默认覆盖掉缓存时间最长的资源。
	
	upstream imagesever {
		server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s ;
		server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s backup;		
		#1.down 表示单前的server暂时不参与负载
		#2.weight 默认为1.weight越大,负载的权重就越大。
		#3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
		#4.fail_timeout:max_fails次失败后,暂停的时间。
		#5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。当其他服务器挂了才会启用这台服务器
		
		#负载均衡策略
		#1.RR(轮询,默认)                             根据请求顺序进行分发请求
		#2.加权轮询                                         设置weight,权重越大,访问率越高,用于后端服务器性能不均的情况。
		#3.ip_hash (写在upstream中)              每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
	
	}
	
    server {
        listen       8181; #默认是80端口
        server_name  aaa.com bbb.com;	#如果是在本地,该域名须在hosts配置一下,如果是80端口,直接用aaa.com就可以访问,如果是其他例如81端口,则需要aaa.com:81才能访问,可配置多个域名 
        #charset koi8-r;
        charset utf-8;        		
        #access_log  logs/host.access.log  main; #日志输出位置 以及使用的日志格式
	
		#location / {
		#	root /var/www/;
		#	index index.htm index.html;
        #}
        #这样,当用户请求 / 地址时,Nginx 就会自动在 root 配置指令指定的文件系统目录下依次寻找 index.htm 和index.html 这两个文件。
		#如果 index.htm 文件存在,则直接发起“内部跳转”到 /index.htm 这个新的地址;而如果 index.htm 文件不存在,则继续检查 index.html 是否存在。
		#如果存在,同样发起“内部跳转”到/index.html;如果 index.html 文件仍然不存在,则放弃处理权给 content 阶段的下一个模块。
		
		
		#location /images {
		#	root /var/www/; #主要 /var/www/ 和 var/www/不同,前者是绝对路径,对应盘符,后者是相对路径,相对于nginx.exe目录
        #}                  #root 当匹配到/images时,在 /var/www/images 下寻找文件
		
		#location会首先匹配最长路径, 所以 / 通常是最后匹配到的
		#【=】模式: location = path,此种模式优先级最高(但要全路径匹配) 
		#【^~】模式:location ^~ path,此种模式优先级第二高于正则;特别告诉 Nginx 本条普通 location 一旦匹配上,则不需要继续正则匹配。(即后面location有适合的正则匹配也不管)  
		#【~ or ~*】模式:location ~ path,正则模式,优先级第三,【~】正则匹配区分大小写,【~*】正则匹配不区分大小写; 
		#【path】模式: location path,中间什么都不加,直接跟路径表达式;
		####################################匹配例子####################################
		#location = / {
		#	[ configuration A ]
		#}
		#location / {
		#	[ configuration B ]
		#}
		#location /documents/ {
		#	[ configuration C ]
		#}
		#location ^~ /images/ {
		#	[ configuration D ]
		#}
		#location ~* \.(gif|jpg|jpeg)$ { 
		#	[ configuration E ] 
		#}
		#请求“/”匹配配置A, 请求“/index.html”匹配配置B, 请求“/documents/document.html”匹配配置C, 请求“/images/1.gif”匹配配置D, 请求“/documents/1.jpg”匹配配置E
		#两种情况下,不需要继续匹配正则 location : 
		#( 1 ) 当普通 location 前面指定了“ ^~ ”,特别告诉 Nginx 本条普 通 location 一旦匹配上,则不需要继续正则匹配。 
		#( 2 ) 当普通location 恰好严格匹配上 ,不是最大前缀匹配,则不再继续匹配正则
		

        # 把请求反向代理到另一个服务器 
		location / {       
                proxy_pass http://localhost:8080;  
        }   

        #location ~*\.(gif|png|jpg)$ {       
        #        root data/images;   
        #}

		# 利用多台服务器进行负载均衡,将请求分发到不同服务器
		#其实负载均衡可以说就是反向代理,不过反向代理一般对应特定服务器,负载均衡利用多台服务器
		#location ~ .*\.(jpg|png|gif|bmp)$ {
		#  proxy_pass http://imagesever; #imagesever 是上面配置的服务器集群
		#}	 
		

		#对某个接口进行缓存#对某个接口进行缓存
		#location /servie {
		#	proxy_pass	https://imagesever/user/detail?id=10;
		#	proxy_cache cache_one;
        #    proxy_cache_valid 200 10m;
        #}

		location /download/{ #下载图片,不要再写匹配img、gif等后缀的location
		     alias  C:/zywk/ ; //alias 当匹配后会直接使用C:/zywk/ +文件路径,和root不一样,root会把匹配路径进行拼接 C:/zywk/ download/+文件路径
			 access_log E:/nginx-1.14.0/logs/log_download.log;	#日志路径
			 default_type  'application/octet-stream';  #加上 'application/octet-stream' Content-disposition "attachment"
			 add_header Content-disposition "attachment";
		}
		
		location /file/{  #查看图片
		     alias  C:/zywk/ ;
			 access_log E:/nginx-1.14.0/logs/log_file.log;	#日志路径
		}
		
		
        #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;
        #}
    }


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

}

nginx最目前最主要的功能时反向代理和负载均衡,但在我看来,这两个概念本质上是一样的,无需区分得太清楚。
以上配置,主要展示了nginx以下几个关键:
gizp :将文件进行压缩,节约网络传输带宽,提高网页加载速度
cache:对某些请求和资源进行缓存,加快访问资源响应速度
upstream: 负载均衡服务器配置

学习nginx,初学者的话,需要注意以下几点:
1.注意 location root和alias 的区别
2.注意 location的匹配顺序
3.负载均衡配置及其策略
4.作为静态资源服务器的配置(以上 /file/ 和 /download/ 是访问静态资源常用配置)

和https相关的配置可以看看以下链接
[1]: https://www.cnblogs.com/reghao/p/8504719.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值