Heim > Artikel > Backend-Entwicklung > ngnix下conf通用设置方法(php fastcgi)
来自: http://my.oschina.net/kmwzjs/blog/611125
核心做法:
涉及倒三类配置文件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’]