### 常用设置 > nginx默认配置说明:\[[http://www.nginx.cn/76.html](http://www.nginx.cn/76.html)\] ~~~ groupadd www useradd -g www www chown -R www:www /bigdata2/ chmod -R 777 /bigdata2/cache chmod -R 777 /bigdata2/public ~~~ ### 【http】开启Gzip压缩 ~~~ #开启Gzip,默认值:off gzip on; #不压缩临界值,大于1K的才压缩,默认值是0,不管页面多大都压缩,建议1K gzip_min_length 1k; #表示按照原始数据大小以 16k 为单位的4倍申请内存。 gzip_buffers 4 16k; #用了反向代理的话,末端通信是HTTP/1.0,有这句的话注释了就行了,默认是HTTP/1.1 #gzip_http_version 1.0; #压缩级别,1-10,数字越大压缩的越好,时间也越长,默认值: 1 gzip_comp_level 3; #进行压缩的文件类型,缺啥补啥就行了,JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就行了 gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; # 是否在http header中添加Vary: Accept-Encoding,建议开启 gzip_vary on; #IE6对Gzip不怎么友好,不给它Gzip了 gzip_disable "MSIE [1-6]\."; ~~~ ### 每个站点单独配置文件 ~~~ 打开nginx.conf文件 sudo vi /usr/local/nginx/conf/nginx.conf 将虚拟目录的配置文件加入到”http {}”部分的末尾 http { ... include /usr/local/nginx/conf/vhost/*.conf; } 在Nginx配置目录下,创建一个”vhost”目录。配置目录在”/usr/local/nginx/conf/” $ sudo mkdir /usr/local/nginx/conf/vhost 创建siteA的配置文件,填入常用配置 $ sudo vi /usr/local/nginx/conf/vhost/vhost_siteA.conf 输入以下配置信息 server { listen 80; # 监听端口 server_name www.siteA.com siteA.com; # 站点域名 root /home/user/www/blog; # 站点根目录 charset utf-8; #默认请求 location / { index index.html index.htm index.php; # 默认导航页 } #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置. location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } } ~~~ ### 【server,location】rewrite域名IP,url重写 ~~~ #重写url,将index.php隐藏掉 #默认请求 location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } } ~~~ ### 【server,location】跨域访问 ~~~ #默认请求 location / { #授权从那个域名的请求可跨域,“*”表示所有域名 add_header 'Access-Control-Allow-Origin' '*'; #当该标志为真时,响应于该请求是否可以被暴露 add_header 'Access-Control-Allow-Credentials' 'true'; #指定请求的方法,可以是GET,POST等 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; } ~~~ ### 【server,location】FastCGI模块配置 > 文档参考:[http://blog.csdn.net/bytxl/article/details/18841695](http://blog.csdn.net/bytxl/article/details/18841695) ~~~ #设置缓存的关键字,默认:none fastcgi_cache_key $request_method:#$host$request_uri; fastcgi_cache_path /data/cache/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m; #指定同FastCGI服务器的连接超时时间,这个值不能超过75秒,默认60。 fastcgi_connect_timeout 75; #指令指定请求服务器的超时时间,指完成了2次握手的连接,而不是完整的连接,如果在这期间客户端没有进行数据传递,那么服务器将关闭这个连接。 fastcgi_send_timeout 30000; #前端FastCGI服务器的响应超时时间,如果有一些直到它们运行完才有输出的长时间运行的FastCGI进程,或者在错误日志中出现前端服务器响应超时错误,可能需要调整这个值。 fastcgi_read_timeout 30000; #这个参数指定将用多大的缓冲区来读取从FastCGI进程到来应答头。 fastcgi_buffer_size 16k; #这个参数指定了从FastCGI进程到来的应答,本地将用多少和多大的缓冲区读取。 fastcgi_buffers 16 16k; fastcgi_busy_buffers_size 16k; fastcgi_temp_file_write_size 16k; #为缓存实际使用的共享内存指定一个区域,相同的区域可以用在不同的地方。 fastcgi_cache TEST; #将响应状态码为200和302缓存1小时,301缓存1天,任何一个缓存1分钟。 fastcgi_cache_valid 200 302 1h; fastcgi_cache_valid 301 1d; fastcgi_cache_valid any 1m; #指令指定了经过多少次请求的相同URL将被缓存。 fastcgi_cache_min_uses 1; #在某些网关错误、超时的情况下,nginx都将传送过期的缓存数据。 fastcgi_cache_use_stale error timeout invalid_header http_500; ~~~ ### 【server,location】静态文件缓存 ~~~ #max指定缓存数量 inactive是指经过多长时间文件没被请求后删除缓存。 open_file_cache max=204800 inactive=20s; #在上述时间中没有使用到这个配置的如下次数的话就删除 open_file_cache_min_uses 1; #多少时间检查一次,如果发现inactive时间内没有用过一次的删除 open_file_cache_valid 30s; ~~~ ### IP直接访问 ~~~ server { listen 10.1.1.11:80 default_server; #server_name www.eq.com ; root /bigdata2/public; location / { index index.html index.htm index.php; #autoindex on; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } } ~~~