核心做法:
涉及倒三类配置文件1. 基础文件 conf/nginx.conf# 说明:nginx默认会引用该文件,该文件会做最通用的参数设置2. fastcgi参数文件 conf/nginx.conf# 说明 fastcgi会设置所有站点都通用的参数3. 站点文件 如:conf/vhost/a.com 可以有多个,放在vhost文件夹下彼此引用说明在基础文件conf/nginx.conf中插入include *.conf 引用站点文件站点文件中合适位置插入include fastcgi.conf 设置fastcgi,如果要覆盖参数,可以在引用再赋值一次即可
nginx配置文件里指令的继承关系:nginx配置文件分为好多块,常见的从外到内依次是「http」、「server」、「location」等等,缺省的继承关系是从外到内,也就是说内层块会自动获取外层块的值作为缺省值
值得参考的文章:http://huoding.com/2013/10/23/290
详细说明:
nginx.conf中设置基础参数,内容如下:
#用户及用户组设置user www www;worker_processes 1;#错误级别的日志设置error_log logs/error.log;#其他级别的日志设置#error_log logs/error.log notice;#error_log logs/error.log info;pid logs/nginx.pid;events { use epoll; worker_connections 1024;}http { #fastcgi_intercept_errors on; #error_page 404 ; 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; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; #keepalive_timeout 0; keepalive_timeout 60; tcp_nodelay on; server_tokens off; gzip on; 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; #未绑定域名返回404 server { listen 80 default; return 404; } #加载每个站点conf文件 include vhost/*.conf;}
单个站点conf文件放在conf/vhost文件夹下,比如建立一个a.com.conf,内容见下方:
server { #监听端口 listen 80; #绑定域名,用空格分开多个域名 server_name a.com www.a.com; #默认首页 index index.php index.html index.htm; #站点目录 set $root_path '/data/wwwroot/a.com/web'; root $root_path; #访问日志 #access_log logs/a.com_access.log main; #错误日志 #error_log logs/$host_error.log; #伪静态 location / { #ci框架 #try_files $uri $uri/ /index.php?$query_string; #phalcon框架 #try_files $uri $uri/ /index.php?_url=$uri&$args; } #php文件采用fastcgi解析并设置参数 location ~ \.php { try_files $uri = 404; fastcgi_index /index.php; fastcgi_pass 127.0.0.1:9000; #加载fastcgi.conf文件中的参数 include fastcgi.conf; #设置有权限目录,fastcgi.conf中默认设置就是站点目录$document_root,如果要改变就需要重新赋值 #phalcon举例 #fastcgi_param php_admin_value open_basedir=$document_root/../:/data/tmp/php/upload/:/proc/; } #禁止下载伪静态文件 location ~ /\.ht { deny all; }}
conf/fastcgi.conf内容:
fastcgi_param script_filename $document_root$fastcgi_script_name;fastcgi_param query_string $query_string;fastcgi_param request_method $request_method;fastcgi_param content_type $content_type;fastcgi_param content_length $content_length;fastcgi_param script_name $fastcgi_script_name;fastcgi_param request_uri $request_uri;fastcgi_param document_uri $document_uri;fastcgi_param document_root $document_root;fastcgi_param server_protocol $server_protocol;fastcgi_param https $https if_not_empty;fastcgi_param gateway_interface cgi/1.1;fastcgi_param server_software nginx/$nginx_version;fastcgi_param remote_addr $remote_addr;fastcgi_param remote_port $remote_port;fastcgi_param server_addr $server_addr;fastcgi_param server_port $server_port;fastcgi_param server_name $server_name;# php only, required if php was built with --enable-force-cgi-redirectfastcgi_param redirect_status 200;# 用一个文件或状态码(=404)作为最后一个参数,如果是最后一个参数是文件,那么这个文件必须存在try_files $fastcgi_script_name = 404;#可以自定义值,比如区分开发(dev)和生成环境(product),在php中用getenv('my_env')或$_server['my_env']获取fastcgi_param my_env product;#防跨站设置fastcgi_param php_admin_value open_basedir=$document_root/:/data/tmp/php/upload/:/proc/;
需要说明的:nginx有两份fastcgi配置文件,分别是旧的「fastcgi_params」和新的「fastcgi.conf」,它们没有太大的差异,唯一的区别是后者比前者多了一行「script_filename」的定义,只需要引用一份新的配置文件fastcgi.conf即可
fastcgi参数说明,参考:http://www.ivpeng.com/pblog/cgi-arg.html
fastcgi_param script_filename $document_root$fastcgi_script_name;#脚本文件请求的路径fastcgi_param query_string $query_string; #请求的参数;如?app=123fastcgi_param request_method $request_method; #请求的动作(get,post)fastcgi_param content_type $content_type; #请求头中的content-type字段fastcgi_param content_length $content_length; #请求头中的content-length字段。fastcgi_param script_name $fastcgi_script_name; #脚本名称fastcgi_param request_uri $request_uri; #请求的地址不带参数fastcgi_param document_uri $document_uri; #与$uri相同。fastcgi_param document_root $document_root; #网站的根目录。在server配置中root指令中指定的值fastcgi_param server_protocol $server_protocol; #请求使用的协议,通常是http/1.0或http/1.1。fastcgi_param gateway_interface cgi/1.1;#cgi 版本fastcgi_param server_software nginx/$nginx_version;#nginx 版本号,可修改、隐藏fastcgi_param remote_addr $remote_addr; #客户端ipfastcgi_param remote_port $remote_port; #客户端端口fastcgi_param server_addr $server_addr; #服务器ip地址fastcgi_param server_port $server_port; #服务器端口fastcgi_param server_name $server_name; #服务器名,域名在server配置中指定的server_name#fastcgi_param path_info $path_info;#可自定义变量# php only, required if php was built with –enable-force-cgi-redirect#fastcgi_param redirect_status 200;在php可打印出上面的服务环境变量如:echo $_server[‘remote_addr’]