NginxWeb服务设置

关于设置示例服务环境:
设置示例目标:实现网站论坛静态化,防盗链,下载并发数和速率限制
第一步: PHP5编译安装(使用FastCGI调用)
#安装配置
[root@save-info.com]# cat in.sh
./configure \
–prefix=/usr/local/php-fcgi \
–enable-fastcgi \
–enable-discard-path \
–enable-force-cgi-redirect \
–with-config-file-path=/usr/local/php-fcgi/etc \
–enable-zend-multibyte \
–with-mysql=/usr/local/mysql \
–with-libxml-dir=/usr/local/libxml2 \
–with-gd=/usr/local/gd2 \
–with-jpeg-dir \
–with-png-dir \
–with-bz2 \
–with-freetype-dir \
–with-iconv-dir \
–with-zlib-dir  \
–with-openssl=/usr/local/openssl \
–with-mcrypt=/usr/local/libmcrypt \
–enable-sysvsem \
–enable-inline-optimization \
–enable-soap \
–enable-gd-native-ttf \
–enable-ftp \
–enable-mbstring \
–enable-exif \
–disable-debug \
–disable-ipv6
#编译并安装
make & make install
#制作php.ini文件
cp php.ini-dist /usr/local/php-fcgi/etc/php.ini
本文介绍Nginx的安装与配置,如想了解其它相关的问题可以在本站(www.save-info.com)找寻其他比如 如何安装gd库,mysql的编译安装
第二步: Nginx编译安装
1.创建nginx运行用户和虚拟主机目录代码:
[root@save-info.com]# groupadd www -g 48
[root@save-info.com]# useradd -u 48 -g www www
[root@save-info.com]# mkdir -p /html/www/wwwroot
[root@save-info.com]# chown -R www:www /html/www/wwwroot
2.安装Lighttpd中附带的spawn-fcgi,用来启动php-cgi
先编译安装lighttpd产生spawn-fcgi二进制文件
[root@save-info.com]# cd /usr/local/src/lighttpd-1.5.10
[root@save-info.com]# cp src/spawn-fcgi /usr/local/php-fcgi/bin/
启动php-cgi进程,监听127.0.0.1的8888端口,进程数为500,如果服务器内存太小,可以只开启5到10个进程,用户为www:
[root@save-info.com]# /usr/local/php-fcgi/bin/spawn-fcgi -a 127.0.0.1 -p 8888 -C 500 -u www -f /usr/local/php-fcgi/bin/php-cgi
3.nginx的安装与配置
安装Nginx所需的pcre库:
[root@save-info.com]#wget http://ftp.dk.debian.org/exim/pcre/pcre-7.3.tar.gz
[root@save-info.com]#tar zxvf pcre-7.2.tar.gz
[root@save-info.com]#cd pcre-7.2/
[root@save-info.com]#./configure
[root@save-info.com]#make & make install
[root@save-info.com]#cd ../
[root@save-info.com]#wget http://sysoev.ru/nginx/nginx-0.5.32.tar.gz
[root@save-info.com]#tar zxvf nginx-0.5.32.tar.gz
[root@save-info.com]#cd  nginx-0.5.32
[root@save-info.com]#./configure –user=www –group=www –prefix=/usr/local/nginx/ –with-http_stub_status_module –with-openssl=/usr/local/openssl
[root@save-info.com]#make & make install
(可以附加的编译参数 –with-http_stub_status_module, 此模块非必须 )
第三步: Nginx配置文件及PHP解释器指定
1.nginx.conf 主配置文件的配置
[root@save-info.com]#cd /usr/local/nginx/conf/
[root@save-info.com]#cp  nginx.conf nginx.conf.cao
[root@save-info.com]#cat /dev/null > nginx.conf
[root@save-info.com]#vi nginx.conf
//主配置文件代码:user  www www;
worker_processes 10;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid        /var/run/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
#maxclient = worker_processes * worker_connections / cpu_number
worker_connections 51200;
}
http
{
include       conf/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  /data/www/logs/access.log  main;
#sendfile on;
tcp_nopush     on;
tcp_nodelay    off;
keepalive_timeout 60;
client_header_timeout  3m;
client_body_timeout    3m;
send_timeout           3m;
connection_pool_size        256;
client_header_buffer_size    1k;
large_client_header_buffers    4 2k;
request_pool_size        4k;
output_buffers   4 32k;
postpone_output  1460;
client_max_body_size       10m;
client_body_buffer_size    256k;
client_body_temp_path /dev/shm/client_body_temp;
proxy_temp_path            /usr/local/nginx/proxy_temp;
fastcgi_temp_path          /usr/local/nginx/fastcgi_temp;
#gzip
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1100;
gzip_buffers 4 8k;
# The following includes are specified for virtual hosts   //虚拟主机配置.
#www.save-info.com
include          conf/vhosts/save-info_com.conf;
}
2.配置PHP对FastCGI的支持
[root@save-info.com]# cat enable_php5.conf
fastcgi_pass  127.0.0.1:8888;
fastcgi_index index.php;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
#new ac upload
#fastcgi_pass_request_body off;
#client_body_in_file_only clean;
#fastcgi_param  REQUEST_BODY_FILE  $request_body_file;
#
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;
1.设置示例采用Discuz!论坛,原rewrite规则不做修改即可全部移植到nginx下.
2.实现防盗链功能,在nginx下实现代码:
#Preventing hot linking of images and other file types
valid_referers none blocked server_names *.save-info.com save-info.com //允许连接的域名;
if ($invalid_referer) {
rewrite   ^/   http://www.redocn.com/images/denylink.gif;  //当认为是盗链时显示一个替代图片.
#return   403;  //或者发送HTTP 403错误
}
3.限制下载的并发数和速率,在nginx下使用 limit_zone(limit_conn) 来限制并发数,limit_rate来限制下载的速率,配置实例:
[root@redocn vhosts]# vi down_redocn_com.conf代码:limit_zone   one  $binary_remote_addr  10m;
server
{
listen       80;
server_name  down.redocn.com;
index index.html index.htm index.php;
root   /html/www/wwwroot/down;
error_page 404 /index.php;
# redirect server error pages to the static page /50x.html
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
#Zone limit
location / {
limit_conn   one  1;
limit_rate  20k;
}
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/  {
root    /html/www/wwwroot/down;
expires 30d;
}
}
4.启动nginx服务脚本:
/usr/local/php-fcgi/bin/spawn-fcgi -a 127.0.0.1 -p 8888 -C 500 -u www -f /usr/local/php-fcgi/bin/php-cgi
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
你可以把上面两条命令制做成系统启动服务脚本,放入 init.d 即可实现开机自动启动

《NginxWeb服务设置》上有2条评论

  1. 说:突然想说一句半题外话,呵呵:我们发现,对于 nginx + php farm 的并发性能影响最大的经常是,当 php 发起到 DB 或者 memcached 或者后端 HTTP srcivee 时,当前 php 进程是阻塞的。因此我们打算试试让 php 进程需要发起二级 IO 请求时,直接由 nginx 的 subrequest 来代理之,这样,php 端的 IO 便可以与 nginx 的性能极好的 event model 融为一体,从而提升单机整体的并发能力。(这对于 Perl/Python/Ruby/Haskell/V8 等其他的以 fastcgi 形式挂在 nginx 后面的 web 应用亦是适用的。)因此我们需要借用 fastcgi 协议去支持 fastcgi 后端直接发起 nginx subreqest,并传递 subrequest 完成后需要打开的“continuation 对象”,并结果和控制权重新交回指定的 fastcgi 后端入口。

发表评论