【原创】学习日记2:nginx配置文件2012.01.06

本文承接http://www.cnblogs.com/wangjiafang/archive/2013/01/07/2848850.html 一文

【首先】

nginx.conf 最上面的的usr 改为root

【其次】

看下面最后一行 include 包含的*.conf

新建个文件如 test.conf

内容如下:

server {
    listen       80;
    server_name  www.test.com;

    #charset koi8-r;
    access_log  /var/log/nginx/test.access.log  main;
    error_log  /var/log/nginx/test.error.log;

    location / {
        root   /var/www/html/test/;
        index  index.php;
    }

    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   /usr/share/nginx/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           /var/www/html/test/;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/test$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;
    #}
}

【设置浏览器缓存时间(expires)】

在配置文件server块中,增加以下内容。

   location ~ .*\.(gif|jpg|jpeg|png|bmp|wsf)$ {
      expires 30d;
   }

   location ~ .*\.(js|css)$ {
      expires 1h;
   }

以上设置中,第一个表示所有gif、jpg、jpeg、png、bmp、wsf文件,在访问后的30天后缓存失效;第二个表示所有js、css文件,在访问后的1小时后缓存失效。

【设置反向代理】

例如,将域名下所有php请求转交给apache处理,我们可以在配置文件相应server块中,设置如下内容。

   location ~ \.php$ {
      proxy_pass   http://127.0.0.1:8080;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $remote_addr;
   }

【压缩设置】

在配置文件http块中找到"# gzip on;",将gzip前的#号去掉,并在下一行增加以下内容。

   gzip_min_length 1k;
   gzip_buffers 4 16k;
   gzip_http_version 1.1;
   gzip_comp_level 2;
   gzip_types text/plain application/x-javascript text/css application/xml;
   gzip_vary on;

gzip_types表示启用压缩文能的文件头,以上设置为文本、js、css、xml进行文件压缩。