Nginx负载均衡搭建胜过Apache十倍(3)
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data1/logs/access.log access;
}
server
{
listen 80;
server_name www.s135.com;
index index.html index.htm index.php;
root /data0/htdocs/www;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
log_format wwwlogs '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data1/logs/wwwlogs.log wwwlogs;
}
server
{
listen 80;
server_name status.blog.s135.com;
location / {
stub_status on;
access_log off;
}
}
}
②、在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件:
| vi /usr/local/webserver/nginx/conf/fcgi.conf |
输入以下内容:
| fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param REMOTE_ADDR $remote_addr; # PHP only, required if PHP was built with --enable-force-cgi-redirect |
5、启动Nginx
| ulimit -SHn 51200 /usr/local/webserver/nginx/sbin/nginx |
四、配置开机自动启动Nginx + PHP
| vi /etc/rc.local |
在末尾增加以下内容:
| ulimit -SHn 51200 /usr/local/webserver/php/sbin/php-fpm start /usr/local/webserver/nginx/sbin/nginx |
五、优化Linux内核参数
| vi /etc/sysctl.conf |
在末尾增加以下内容:
| net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 300 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.ip_local_port_range = 5000 65000 |
使配置立即生效:
| /sbin/sysctl -p |
六、在不停止Nginx服务的情况下平滑变更Nginx配置
(1)、修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
| /usr/local/webserver/nginx/sbin/nginx -t |
如果屏幕显示以下两行信息,说明配置文件正确:
| the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully |
(2)、这时,输入以下命令查看Nginx主进程号:
| ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}' |
屏幕显示的即为Nginx主进程号,例如:
| 6302 |
这时,执行以下命令即可使修改过的Nginx配置文件生效:
| kill -HUP 6302 |
或者无需这么麻烦,找到Nginx的Pid文件:
| kill -HUP `cat /usr/local/webserver/nginx/nginx.pid` |
- 最新评论
