nginx配置文件

前面一篇,我们介绍nginx的安装,这篇介绍下nginx配置文件

 

功能介绍,可以参考这篇博文 http://www.cnblogs.com/skynet/p/4146083.html

可以简单理解:

nginx可以用于stream模块 (tcp),或是http模块反向代理。

当使用http代理的时候,就会有server 节点(可以有多个server),配置不同域名。

在server下,我们常常基于不同的功能模块划分url, 那么就有location节点(也可以有多个),配置不同的匹配规则

当请求到达location后,这里我们一般会区分两种情况,

一种如果是静态资源的话直接访问本地文件,

另外一种就是转接到相应的解析器上,比如http代理就是 proxy_pass  upstream_name, 常用php代理则是 proxy_fastcgi upstream_name

 

这里我贴出了一个实例配置文件,其中每一项设置都是基于理由的

user  www;
worker_processes  auto;    #worker数应该等于cpu核数
worker_cpu_affinity auto;  #每个worker绑定到一个cpu核上

worker_rlimit_nofile 102400;  #worker最大可以打开文件数

error_log  /var/log/nginx/error.log error;  #nginx错误日志路径
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    use epoll;  #使用epoll组件处理文件句柄
    accept_mutex off;
    worker_connections  102400;  #worker最大连接数
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    set_real_ip_from 10.0.0.0/8;
    real_ip_header X-Real-IP;
    server_tokens off;
    server_tag app/1.1.2;

    log_format  log_post '$remote_addr - $remote_user [$time_local] $hostname "$request_method $http_host$request_uri $request_body $s
erver_protocol" '
                      '$status $body_bytes_sent $request_time "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';

    log_format log_uid_post '$remote_addr - $uid $auser - $remote_user [$time_local] "$request_method $http_host$request_uri $request_
body $server_protocol" '
                      '$status $body_bytes_sent $request_time "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /dev/null;

    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;

    keepalive_timeout  15;
    proxy_connect_timeout 15;
    proxy_read_timeout 300;
    proxy_send_timeout 300;

    fastcgi_connect_timeout 15;
    fastcgi_send_timeout 150;
    fastcgi_read_timeout 150;
    fastcgi_buffer_size 32k;
    fastcgi_buffers 4 32k;
    fastcgi_busy_buffers_size 64k;
    fastcgi_temp_file_write_size 64k;

    open_file_cache max=102400 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 64k;
    large_client_header_buffers 4 128k;
    client_max_body_size 8m;
    client_body_timeout 3m;
    client_header_timeout 3m;

    gzip on;
    gzip_disable "MSIE [1-6]\.";
    gzip_comp_level 3;
    gzip_buffers 4 8k;
    gzip_min_length 1000;
    gzip_types text/plain text/css application/x-javascript text/javascript application/json;

    #fastcgi_cache_path /data/webroot/php_cache levels=2:2
    #                   keys_zone=PHP_CACHE:10m
    #                   inactive=60m;
    #fastcgi_temp_path /data/webroot/php_cache_tmp;
    #fastcgi_cache_key "$scheme$request_method$host$uri";
    #fastcgi_cache_use_stale error timeout invalid_header http_500;
    #fastcgi_ignore_headers "Cache-Control" "Expires";

    #upstream ups_php {
    #    server unix:/dev/shm/php-socket max_fails=3 fail_timeout=5;
    #    server unix:/dev/shm/php-socket-2 max_fails=3 fail_timeout=5;
    #}

     include upstream.conf;

    #limit_conn_zone $binary_remote_addr zone=one:10m;
    #limit_req_zone $binary_remote_addr zone=onezone:10m rate=10r/s;

    #lua_package_path '/data/webroot/www/lua_api/lib/?.lua;/data/webroot/www/lua_api/?.lua;';
    #lua_package_cpath '/data/webroot/www/lua_api/lib/?.so;';
    #lua_shared_dict count 1M;
    #lua_shared_dict turl_map 100k;
    #lua_shared_dict turl_hit 100k;
    #lua_shared_dict sys_level 16k;

    include lua.conf;

    include /etc/nginx/server.d/*;

}

 推荐博文  http://os.51cto.com/art/201404/434930.htm 说的很详细

 

后端server配置

server {
    listen       80;
    server_name  *.example.com;

    location / {
            root   /data/webroot/www/project;
            index  index.php index.html;
            include /data/webroot/www/project/.nginxaccess;
    }

    location ~ /hermes/ {
        content_by_lua_file /data/webroot/www/lua_api/index.lua;
    }

    location ~* \.php {
        root /data/webroot/www/project;

        fastcgi_pass    ups_php;
        include         fastcgi_params;
        fastcgi_index                           index.php;
        fastcgi_split_path_info                 ^(.+\.php)(.*)$;
        fastcgi_param SCRIPT_FILENAME           /data/webroot/www/project/$fastcgi_script_name;
        fastcgi_param PATH_INFO                 $fastcgi_path_info;

        access_log "pipe:/mnt/log/access.log" log_uid_post;
    }

    location ~ ^/(image|style|script|static)/  {
        root /data/webroot/www/project;
        rewrite ^/(script|style|static)/([^\-]*)-(\d+)\.(js|css)$ /$1/$2.$4 last;
        rewrite "^/(script|style|static)/([^\-]*)-([a-z0-9]{6,})\.(js|css)$" /$1/$2.$4 last;
        rewrite ^/static/build/(css|js)/.*/([^/]+).(js|css)$ /static/build/$1/$2.$3 last;
        open_file_cache off;
        expires 1d;
    }

    location ^~ /upload/ {
        root    /mnt/nfs-public/;
        expires 30d;
        break;
    }
}

 

posted on 2016-03-05 17:06  p4w110  阅读(251)  评论(0)    收藏  举报

导航