nginx 常用配置

#用户 用户组
user  nobody nobody;
 
#错误日志  
error_log  logs/error.log;
 
#pid文件位置  
pid        logs/nginx.pid;
 
#nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (2个四核的cpu计为8)
worker_processes    8;
 
#为每个进程分配cpu,上例中将8 个进程分配到8 个cpu,当然可以写多个,或者将一个进程分配到多个cpu
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
 
#这个指令是指当一个nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx 进程数相除(具体参见http://chongzai.iteye.com/admin/blogs/1900194)
worker_rlimit_nofile 65535;
 
events {
    #事件模型
    use epoll;
    #每个进程允许的最多连接数, 理论上每台nginx 服务器的最大连接数为worker_processes*worker_connections
    worker_connections  65535;
}
    
http {
    include       mime.types;
    
    #反向代理配置,可以打开proxy.conf看看  
    include    /etc/nginx/proxy.conf;  
 
    #fastcgi配置,可以打开fastcgi.conf看看  
    include    /etc/nginx/fastcgi.conf;
 
    #日志的格式  
    log_format   main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';  
 
    #访问日志  
    access_log   logs/access.log  main;
 
    default_type  application/octet-stream;
    fastcgi_intercept_errors on;
    sendfile        on;
 
    #服务器名字的hash表
    server_names_hash_max_size 32;
    server_names_hash_bucket_size 32;
 
    #客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,
    #一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,
    #所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE 取得。
    client_header_buffer_size 4k;
 
    #这个将为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存
    open_file_cache max=65535 inactive=36000s;
 
    #这个是指多长时间检查一次缓存的有效信息
    open_file_cache_valid 3600s;
 
    #open_file_cache 指令中的inactive 参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除
    open_file_cache_min_uses 1;
 
    #keepalive 超时时间
    keepalive_timeout  65;
 
    #开启或关闭gzip模块(on/off)    
    gzip on;
 
    #设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大
    gzip_min_length 1k;
 
    #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存
    gzip_buffers 16 64k;
 
    #识别http的协议版本
    gzip_http_version 1.1;
 
    #gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
    gzip_comp_level 6;
 
    #匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的
    gzip_types text/plain application/x-javascript text/css application/xml;
 
    #和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩
    gzip_vary on;
 
    #是针对每个IP定义一个存储session状态的容器。这个示例中定义了一个10m的容器,按照32bytes/session,可以处理320000个session
    limit_zone   one  $binary_remote_addr  10m;
 
    #限制每个IP只能发起10个并发连接。
    limit_conn   one 10;
 
    #对每个连接限速300k. 注意,这里是对连接限速,而不是对IP限速。如果一个IP允许10个并发连接,那么这个IP就是限速limit_rate×10
    limit_rate 300k;
 
    #负载均衡 可以添加权重和负载均衡算法配置
    upstream oceanus {   
        server 192.168.168.23:21530;
        server 192.168.168.24:21530;
    }
    upstream wm {   
        server 192.168.168.213:21530;
        server 192.168.168.224:21530;
    }
    server {
        listen       80;
        #server_name  www.*;
        if ( $uri = '/' ){ 
            rewrite .* /index.html break; 
        }
        
        location ~ ^/good(D|Z|X)/([0-9]+)$ {
            rewrite ^/good(D|Z|X)/([0-9]+)$ /productsList.html?category$1=$2;
        }
 
        location ~ ^/article/([0-9]+)$ {
            rewrite ^/article/([0-9]+)$ /articleList.html?categoryID=$1;
        }
        location / {
            #限制上传文件大小
            client_max_body_size 10m;
 
            proxy_redirect off;
 
            #向后台传递真实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_pass http://oceanus;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 
    server {
        listen       80;
        server_name  oceanus.lwl.com;
        if ( $uri = '/' ){ 
            rewrite .* /index.html break; 
        }
        #域名主页跳转
        location ~ "^/([a-zA-Z]{4,50}$)" {
            rewrite "^/([a-zA-Z]{4,30}$)" /index.html?domain=$1;
        }
        #域名预置页跳转
        location ~ /([a-zA-Z]+)/([a-zA-Z]+).html {
            rewrite /([a-zA-Z]+)/([a-zA-Z]+).html /$2.html?domain=$1&$request_uri;
        }
        #域名ajax跳转
        location ~ "^/([a-zA-Z]{4,50})/([a-zA-Z]{4,50})/([a-zA-Z]{4,50}$)" {
            rewrite "^/([a-zA-Z]{4,50})/([a-zA-Z]{4,50})/([a-zA-Z]{4,50}$)"  /$2/$3?domain=$1&$request_uri;
        }
        #ksh 
        location ~ ^/([a-zA-Z]+)/visualization/([a-zA-Z]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+).html {
            rewrite ^/([a-zA-Z]+)/visualization/([a-zA-Z]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+).html  /visualization/$2/$3/$4.html?domain=$1&$request_uri;
        }
        
        location ~ ^/([a-zA-Z]+)/good(D|Z|X)/([0-9]+)$ {
            rewrite ^/([a-zA-Z]+)/good(D|Z|X)/([0-9]+)$ /productsList.html?domain=$1&category$2=$3;
        }
        location ~ ^/([a-zA-Z]+)/article/([0-9]+)$ {
            rewrite ^/([a-zA-Z]+)/article/([0-9]+)$ /articleList.html?domain=$1&categoryID=$2;
        }
        location / {
            proxy_redirect off;
            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://oceanus;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  i.lwl.com;
        if ( $uri = '/' ){ 
            rewrite .* /index.html break; 
        } 
        location / {
            proxy_redirect off;
            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://wm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 
    server {
        listen       80;
        server_name  static.lwl.com;
        #location ~ "^/goods/pre/([a-zA-Z0-9_]{4,128})\.(jpg|gif|png)" {
        #    rewrite "^/goods/pre/([a-zA-Z0-9_]{4,128})\.(jpg|gif|png)" /goods/pre/$1_$arg_m.$2?version=$arg_version;
        #}
        location ~ "^/goods/([0-9]{1,4})/([a-zA-Z0-9]+)/([0-9]{8})/([a-zA-Z0-9]+)\.(jpg|gif|png)" {
            rewrite "^/goods/([0-9]{1,4})/([a-zA-Z0-9]+)/([0-9]{8})/([a-zA-Z0-9]+)\.(jpg|gif|png)" /goods/$1/$2/$3/$4_$arg_m.$5?version=$arg_version;
        }
 
        #禁止访问doc目录        
        location ~ ^/(doc)/ {
            allow   192.168.1.0/24; 
            deny all;
        } 
        location / {
            #防盗链
            valid_referers none blocked oceanus.lwl.com;
            if ($invalid_referer) {
                return 404;
            }
            root /alidata/oceanus/static/html;
            #让浏览器缓存
            expires 24h;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 
    server {
        listen       80;
        server_name  image.lwl.com;
        location / {
            expires 24h;
            root /alidata/oceanus/static/html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值