一般編譯nginx時,都要先安裝pcre、zlib等外部支持程序,然後編譯安裝nginx時指定這些外部支持程序的位置,這樣nginx在每次啓動的時候,就會去動態加載這些東西了。
下面介紹的是另一種方式,即將這些程序編譯到nginx裏面去,這樣nginx啓動時就不會採用動態加載的方式去load。從古譜中可獲知,這種方式會比動態加載有更高的效率。
需要下載的東西:
1. wget http://www.openssl.org/source/openssl-1.0.0a.tar.gz
2. wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.10.tar.bz2
3. wget http://www.zlib.net/zlib-1.2.5.tar.bz2
4. wget http://nginx.org/download/nginx-0.8.52.tar.gz
把這些都解壓縮後,就會有:
1. openssl-0.9.8l
2. pcre-8.00
3. zlib-1.2.3
4. nginx-0.8.30
這幾個目錄,我把它們都放在/homr/software/裏,按原先的方式,需要進openssl、pcre、zlib目錄裏去編譯安裝它們,現在不用了,直接進nginx目錄。
安裝build.sh
#set nginx root path
HOME=/home/raycloud/
NGINX_HOME=$HOME/nginx
NGINX_SOURCE=$HOME/soft/nginx
#install path
install=$NGINX_HOME
#pcre source
pcre=$NGINX_SOURCE/pcre
#zlib source path
zlib=$NGINX_SOURCE/zlib
#openssl source path
openssl=$NGINX_SOURCE/openssl
#cpu type
cpu=pentium4
#user
user=`whoami`
#group
group=anygroup
if [ ! -d $install ] ; then
mkdir -p $install
fi
./configure --user=$user --group=$group --prefix=$install --with-http_gzip_static_module --with-pcre=$pcre --with-zlib=$zlib --with-http_ssl_module --with-openssl=$openssl
把上面的shell拷貝成sh可安裝完成,這種方式安裝的時間會較長,因爲需要先編譯外部程序,值得注意的是,make時不能加-j多進程方式,只能用單進程make,不然沒法通過。如果你的應用不需要openssl,那麼可以不下載openssl並在configure時將其去掉。另外,nginx的 google_perftools_module還不能用這種方式編譯進去,所以還是要先在外部安裝google_perftools。
nginx.conf配置
user nobody;
worker_processes 4;
error_log /home/raycloud/nginx/error/nginx_error.log crit;
pid /home/raycloud/nginx/error/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 90;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name yourdoamin;
charset utf-8;
#access_log logs/host.access.log main;
access_log off;
location ~ (\.shtm)$ {
proxy_pass http://127.0.0.1:8088;
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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 1800; //這裏是瀏覽器和NGINX通訊的超時時間。如果請求比較長可以稍微調大一些。
proxy_send_timeout 1800;
proxy_read_timeout 1800;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
pcre編譯進去支持正則,如下
rewrite ^/img/freetemplate/template([0-9]+)/([0-9]+)/(.+)$ /img/freetemplate/template$1/$3 last;
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|html|xml|cfm|cfc|afp|asp|lasso|pl|py|txt|fla|swf)$ {
root /test/WebRoot;
expires 1d;
}
location ~* ^.+.(js|css)$ {
root /test/WebRoot;
expires 2h;
}
}
}