Home | Mirror | SearchITEYE 博客 | OSChina 博客 | 51CTO 博客

第 3 章 Nginx

目錄

3.1. Installing
3.1.1. Installing by apt-get under the debain/ubuntu
3.1.2. CentOS
3.1.3. installing by source
3.1.4. config test
3.1.5. rotate log
3.1.5.1. log shell
3.1.5.2. /etc/logrotate.d/nginx
3.2. fastcgi
3.2.1. spawn-fcgi
3.2.2. php-fpm
3.2.2.1. php5-fpm
3.2.2.2. 編譯 php-fpm
3.3. nginx 配置檔案
3.3.1. events
3.3.2. http 配置
3.3.2.1. X-Forwarded-For
3.3.2.2. gzip
3.3.2.3. server_tokens
3.3.2.4. ssi
3.3.3. server
3.3.3.1. 單域名虛擬主機
3.3.3.2. ssl 虛擬主機
3.3.3.3. 泛解析主機
3.3.3.4. location
3.3.3.5. expires
3.3.3.6. access
3.3.3.7. auth_basic
3.3.3.8. autoindex
3.3.3.9. rewrite
3.3.3.10. Cache
3.3.3.11. stub_status
3.3.3.12. add_header
3.3.3.13. valid_referers
3.3.4. Proxy
3.3.4.1. request_filename + proxy_pass
3.3.4.2. proxy_cache
3.3.4.3. expires
3.3.4.4. $request_uri 與 proxy_pass 聯合使用
3.3.4.5. X-Sendfile
3.3.5. limit_zone
3.3.6. Nginx 變數
3.3.6.1. http_user_agent
3.3.6.2. http_referer
3.3.6.3. request_filename
3.3.6.4. request_uri
3.3.6.5. remote_addr
3.3.6.6. 自定義變數
3.4. FAQ
3.4.1. proxy_pass

3.1. Installing

3.1.1. Installing by apt-get under the debain/ubuntu

			
$ sudo apt-get install nginx
			
			
			
sudo /etc/init.d/nginx start
			
			

3.1.2. CentOS

http://nginx.org/packages/centos/$releasever/$basearch/

$releasever 是版本號

$basearch 處理器架構

http://nginx.org/packages/centos/6/x86_64/

			
cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/x86_64/
gpgcheck=0
enabled=1
EOF
			
			

i386

			
cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/5/i386/
gpgcheck=0
enabled=1
EOF
			
			
yum search nginx
============================================= Matched: nginx =============================================
nginx.x86_64 : high performance web server

yum install -y nginx
chkconfig nginx on
service nginx start
			

3.1.3. installing by source

			
cd /usr/local/src/
wget http://www.nginx.org/download/nginx-1.0.6.tar.gz

./configure --prefix=/usr/local/server/nginx \
--with-openssl=/usr/include \
--with-pcre=/usr/include/pcre/ \
--with-http_stub_status_module \
--without-http_memcached_module \
--without-http_fastcgi_module \
--without-http_rewrite_module \
--without-http_map_module \
--without-http_geo_module \
--without-http_autoindex_module
			
			

rpm 所使用的編譯參數

nginx -V
nginx: nginx version: nginx/1.0.6
nginx: built by gcc 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC)
nginx: TLS SNI support enabled
nginx: configure arguments: --prefix=/etc/nginx/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwcgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6
			
# nginx -V
nginx version: nginx/1.2.3
built by gcc 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g'
			

3.1.4. config test

$ sudo service nginx configtest
Testing nginx configuration: nginx.
			

3.1.5. rotate log

3.1.5.1. log shell

				
# cat /srv/bin/rotatelog.sh

#!/bin/bash
# run this script at 0:00

#Nginx Log Path
log_dir="/var/log/nginx"
date_dir=`date +%Y/%m/%d/%H`

mkdir -p ${log_dir}/${date_dir} > /dev/null 2>&1
mv ${log_dir}/access.log ${log_dir}/${date_dir}/access.log
mv ${log_dir}/error.log ${log_dir}/${date_dir}/error.log

kill -USR1 `cat /var/run/nginx.pid`

gzip ${log_dir}/${date_dir}/access.log &
gzip ${log_dir}/${date_dir}/error.log &
				
				

3.1.5.2. /etc/logrotate.d/nginx

				
# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
        endscript
}
				
				
comments powered by Disqus