8.//为nginx增加fastcgi_params 文件
#vi /etc/nginx/fastcgi.conf //修改后保存
// 将里面内容替换为
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
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_FILENAME $document_root$fastcgi_script_name;
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 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-redirect
fastcgi_param REDIRECT_STATUS 200;
##########################################
更新说明:2011-11-23
// 什么原因就不解释了,不能归咎与nginx ,因PHP PATH_INFO问题!
// 只采用方便的方式
// 在 fcgi.conf文件开头,添加一下内容,便于多个虚拟主机引用!
if ($request_filename ~* (.*)\.php) {
set $php_url $1;
}
if (!-e $php_url.php) {
return 403;
}
##########################################
9.为nginx 优化linux内核参数
vi /etc/sysctl.conf // 增加一下内容,并是修改生效
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw.reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.ip_local_port_range = 1024 65535
##########################################
#sysctl -p //使其及时生效
--------------------------------------------------
10.编写SystemV风格的启动脚本(可在本文附件下载后直接使用)
//如果需要请参考: 有关nginxd脚本的编写
//使用说明,保存nginxd脚本,赋予执行权限,添加服务和开机启动
#chmod +x /etc/init.d/nginxd
#chkconfig --add nginxd
// 如果不支持,则添加一下2行即可
// # chkconfig: - 85 15 #为必须字符
// # description: nginx is a World Wide Web server. It is used to serve
#chkconfig --level 2345 nginxd on
11.第一次启动nginx
#service nginxd start //没问题的话就显示,也会看到Welcome to nginx!
Starting nginx: [ OK ]
在XP客户端访问一下 //服务器地址
--------------------------------------------------
12.优化配置nginx.conf 文件
user nginx nginx;
worker_processes 8; //我的服务器为双核双线程
pid /usr/local/nginx/nginx.pid; //PID 存在位置,和启用nginx.pid管理
events {
use epoll; //使用的网络IO模型,CeotOs或REAL推荐使用epoll
worker_connections 1024; //允许的连接数
}
http {
include mime.types;
default_type application/octet-stream;
log_format mylog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
//mylog为自定义log_format的name,log_format用于定义log的格式
sendfile on;
tcp_nopush on;
keepalive_timeout 60; //连接保持时间
gzip on; //开启gzip压缩,为客户端访问节省更大的带宽,提高访问速度
gzip_min_length 1k;
gzip_buffers 4 8k; //安装原数据8K为单位的4倍申请内存
gzip_http_version 1.1; //http版本信息,目前为1.0和1.1
gzip_comp_level 4; //gzip 压缩比(与CPU处理速度相反)
gzip_types text/plain application/x-Javascript text/css application/xml;
gzip_vary on;
fastcgi_intercept_errors 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;
server {
listen 80;
server_name ;
access_log logs/abc.access.log mylog; //用到了mylog,将日志定义为log_format格式
location / {
root /usr/local/nginx/html/abc;
index index.php index.html index.htm;
}
error_page 404 = /404.html; //当访问不存在的页面时,提示用户重新访问主页
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /usr/local/nginx/50x/50x.html; //各种错误页面
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php; //fastcgi 索引页格式
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name ;
access_log logs/abcd.access.log mylog;
location / {
root /usr/local/nginx/html/abcd;
index index.php index.html index.htm;
error_page 404 = /404.html;
error_page 500 502 503 504 /usr/local/nginx/50x/50x.html;
location = /50x.html {
root html;
location ~ \.php$ {
root /usr/local/nginx/html/abcd/wb;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
}
}
}
############### This is nginx.conf ###############