012009

我的VPS是256M的内存,CPU是四核心的,所以更多的我会在乎内存。而在我调试服务器的时候通常会遇到Nginx502 bad gateway和504 Gateway Time-out的错误。分析nginx.conf我发现server和fastcgi的buffers过多,导致fastcgi请求的数量过大,php-fpm无法及时处理而出错。循此思路我们可以再总体buffers不变的情况下减少请求数量,具体的ningx.conf改动细节如下:

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 1 128k; 4 32k
client_max_body_size 8m;

sendfile on;
tcp_nopush     on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 2 256k;8 128
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

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

另外,php-fpm的默认静态处理方式会使得php-cgi的进程长期占用内存而无法释放,这也是导致nginx出错的原因之一,因此可以将php-fpm的处理方式改成apache模式。

<value name=”style”>apache-like</value>

从更改完毕到现在的测试表明上述方式的效果还是很明显的,并没有发现一次Nginx502 bad gateway或504 Gateway Time-out错误。当然,如果你的VPS或者服务器的性能足够好可以根据具体情况不必做无谓的改动。

转载自:http://www.thismail.org/bbs/thread-3321-1-1.html

Posted by admin Tagged with: ,
012009

#user nobody;

worker_processes 10;多少进程

error_log logs/error.log info;记录什么级别的错误日志

pid        logs/nginx.pid; 进程文件

events {
worker_connections 10000; 最大连接就是进程*这个
}

http {
include       mime.types;
default_type application/octet-stream;
log_format   main '$remote_addr - $remote_user [$time_local] $status ' 这里是日志
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;
client_header_timeout 3m; 客户端连接上来以后3秒还不发送请求 就断开
client_body_timeout    3m;    同上
send_timeout           3m;
sendfile                on;
tcp_nopush              on;
tcp_nodelay             on;

gzip on;
gzip_min_length 1100; 小于这个就不压了
gzip_buffers     4 8k;
gzip_types       text/* application/x-javascript; 压缩的类型
#output_buffers   1 32k;
output_buffers   1 512k;
gzip_comp_level 9;
postpone_output 1460;

upstream mysvr{#这里定义负载均衡服务器
server 127.0.0.1:8080 weight=1;
}

server {
listen       80;
server_name localhost;

#server_name 192.168.0.253;#写成本机ip就可以

charset utf-8;字符集

access_log logs/host.access.log main;日志

下面一段用于设置nginx的proxy_store:

To be clear proxy_store is not a cache, it's rather mirror on demand.

To be clear proxy_store is not a cache, it's rather mirror on demand. 因为没有过期头(expire的概念)

location ~* \.(jpg|gif|png|css|swf|html|htm)$ {

root /var/html/$host;远端被缓存的文件都会被放到这里

proxy_store on;
proxy_set_header Host $host;
proxy_temp_path /web/html/tmp;缓存的tmp目录
proxy_set_header Accept-Encoding '';
proxy_store_access user:rw group:rw all:rw;权限
if ( !-f $request_filename ) {
proxy_pass http://mysvr;这样只有不存在才回去后端拉
}
}

#      location ~ \ * {
location / {
proxy_pass http://mysvr;
proxy_redirect off;
proxy_store off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

}
}

location / {
proxy_pass         http://mysvr;   #前面定义的upstream
#        proxy_redirect     off;

proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;     重写http头,没写会导致访问不了

client_max_body_size       10m; #客户端发送的body,在上传附件的时候可能比较大
client_body_buffer_size    128k;

proxy_connect_timeout      90;
proxy_send_timeout         90;
proxy_read_timeout         90;
#        proxy_send_lowat           12000;
proxy_buffer_size          4k;
proxy_buffers              4 32k;
proxy_busy_buffers_size    64k;
proxy_temp_file_write_size 64k;

}
location ~ \.(gif|js|css)$ { #图片,css本地解析
root    /usr/local/tomcat6.bak/webapps/ROOT;
expires 24h;#过期时间
}
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$
{
access_log off;#图片和css不记录日志

}

location /status {
stub_status            on;
access_log      logs/access-status-ip.log;
auth_basic      "status";
#auth_basic_user_file conf/user;     设置管理员查看连接情况目录
allow   192.168.0.87;
#         allow   10.1.1.0/16;
deny    all;

}
error_page   500 502 503 504 /50x.html;
location = /50x.html {
root   html;
}
}

}

参考文章:

http://wiki.codemongers.com/NginxFullExample2 威客

http://www.chinaunix.net/jh/13/1319835.html         chinaunix

NginxStatus 显示的内容意思如下:

* active connections – 当前 Nginx 正处理的活动连接数。
* server accepts handled requests -- 总共处理了 14553819 个连接 ,

成功创建 14553819 次握手 ( 证明中间没有失败的 ), 总共处理了 19239266 个请求 ( 平均每次握手处理了 1.3 个数据请求 )。
* reading -- nginx 读取到客户端的 Header 信息数。
* writing -- nginx 返回给客户端的 Header 信息数。
* waiting -- 开启 keep-alive 的情况下,这个值等于 active - (reading + writing),

意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。
* 另外转剑心的关于解析泛域名 说不定哪天用上:
* ++++++++++++++++++++++++++++++++++++++++++++++
*
nginx支持泛域名解析的方法
*

http://bbs.bsdlover.cn/thread-2194-1-1.html

要使用Nginx下的泛域名支持,必须在编译 Nginx的时候加上
--with-http_sub_module
freebsd下ports安装的时候有提示的,选上即可。

方法我google了半天,网上的好多我照做都是不行的,例如这个:
listen    80;
server_name  www.yourdomain.com *.yourdomain.com;
这个会提示:
# nginx -t
2009/01/04 13:22:56 [emerg] 63944#0: conflicting parameter "*.bsdlover.cn" in www.conf:14
2009/01/04 13:22:56 [emerg] 63944#0: the configuration file nginx.conf test failed

还有些文章里面说的是:
server_name   .yourdomain.com;
这个也是不行的,经过我的实验,正确的做法是:
listen    80;
server_name   _;
这样就可以了,留个笔记,呵呵
* ++++++++++++++++++++++++++++++++++++++
* 二级目录自动加 /
* if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
*

*

——————————————————————————————————————

nginx的日志管理:

——————————————————————————————————————

Nginx 支持下表中的信号:

信号名 作用描述
TERM, INT 快速关闭程序,中止当前正在处理的请求
QUIT 处理完当前请求后,关闭程序
HUP 重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求
USR1 重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件
USR2 平滑升级可执行程序
WINCH 从容关闭工作进程

用logrotate来管理;cat /etc/logrotate.d/nginx:

/usr/local/nginx/logs/*.log {
daily每天滚动
rotate 7保留7份
nocompress不压缩
postrotate在执行完滚动后:
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
fi
endscript
}

生效:logrotate -f 这个文件

Posted by admin Tagged with: ,
272009

已经遇到过两次了,打开一片空白,没有任何提示。

1次是Discuz!首页打开时一片空白,但UCenter又可以打开,登录进后台stat index.php,发现文件的修改日期有问题,被修改日期是6月16日,论坛改版做好后,很久没有修改过这些文件了。估计是被人入侵了!网上有说是编码问题。难道别人把我文件的编码修改了?查看了下备份目录,刚好在6月13日有个备份,把它解压缩出来,覆盖掉原来的,一切恢复正常。

2次是朋友的一个很简单的站,打开是一个空白页面,对比过配置文件,发现跟其他站点的配置一样,怀疑会不会是程序不能在二级目录下运行,就把该主机的根目录设置为那二级目录,结果打开还是不行,又改回来,查看了Nginx的日志,没有错。最后去程序官方,重新下载了一个,直接在服务器上解压缩,再访问,一切正常。注意啊!估计是从Windows传文件上去时,文件出现的问题。无论是SFTP还是FTP,都记得要用binary(二进制)方式上传。这样是最保险的!

Posted by admin Tagged with: ,
252009

NGINX 502 Bad Gateway错误是FastCGI有问题,造成NGINX 502错误的可能性比较多。将网上找到的一些和502 Bad Gateway错误有关的问题和排查方法列一下,先从FastCGI配置入手:

1.查看FastCGI进程是否已经启动
NGINX 502错误的含义是sock、端口没被监听造成的。我们先检查fastcgi是否在运行

2.检查系统Fastcgi进程运行情况
除了第一种情况,fastcgi进程数不够用、php执行时间长、或者是php-cgi进程死掉也可能造成nginx的502错误
运行以下命令判断是否接近FastCGI进程,如果fastcgi进程数接近配置文件中设置的数值,表明worker进程数设置太少
netstat -anpo | grep "php-cgi" | wc -l

3.FastCGI执行时间过长
根据实际情况调高以下参数值
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

4.头部太大
nginx和apache一样,有前端缓冲限制,可以调整缓冲参数
fastcgi_buffer_size 32k;
fastcgi_buffers 8 32k;
如果你使用的是nginx的负载均衡Proxying,调整
proxy_buffer_size  16k;
proxy_buffers      4 16k;
参见:http://www.ruby-forum.com/topic/169040

5.https转发配置错误
正确的配置方法
server_name www.mydomain.com;
location /myproj/repos {
set $fixed_destination $http_destination;
if ( $http_destination ~* ^https(.*)$ )
{
set $fixed_destination http$1;
}
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Destination $fixed_destination;
proxy_pass http://subversion_hosts;
}
参见:http://www.ruby-forum.com/topic/169040

当然,还要看你后端用的是哪种类型的FastCGI,我用过的有php-fpm,流量约为单台机器40万PV(动态页面), 现在基本上没有碰到502。

转载自:http://www.jefflei.com/post/1049.html

Posted by admin Tagged with:
242009

鉴于Nginx对高并发的优良性能,故配了个Nginx+php-fpm来跑在线代理程序,是按照张宴文章配的,刚配置好时运行正常,但运行一段时间后,网站打开很慢,打开网站后,在输入框输入要访问的网站,也慢得不行。在网站打开慢时,在SSH终端上输入命令也慢,怀疑是机房网速问题,但在ssh上输入

w3m www.example.com

这个打开也慢,基本可以排除机房的网速问题。

当打开网站慢时,把服务器重启后,就会快起来,后来发现,用

/usr/local/webserver/php/sbin/php-fpm restart

把fastcgi重启下也会快起来,最把它加入计划任务,每小时重启下,基本保证网站不会慢,但终究不是办法。

查看了nginx.log和php-fpm.log,根据里面的错误,找了以上转载的几篇文章,总算是把问题解决了,主要修改了两个地方
1、
问题:
发现/usr/local/webserver/php/etc/php-fpm.conf文件里定义的打开文件描述符的限制数量是
<value name="rlimit_files">51200</value>
但用 命令ulimit -n查看,发现只有1024

我已在/etc/rc.local里添加了
ulimit -SHn 51200

竟然没生效

解决:
vi  /etc/security/limits.conf

文件最后加上
*        soft    nofile  51200
*        hard    nofile  51200

2、
问题:
用命令

netstat -np | grep 127.0.0.1:9000 |wc -l

发现只有100多

解决:
根据服务器内存情况,可以把PHP FastCGI子进程数调到100或以上,在4G内存的服务器上200就可以
服务器上内存为8G,我把PHP FastCGI子进程数调整到300

vi /usr/local/webserver/php/etc/php-fpm.conf
将max_children修改为300
<value name="max_children">300</value>

重启服务器,这样,网站打开速度快,而且稳定了。

Posted by admin Tagged with: ,