Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | Email

第 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. nginx 配置文件
3.2.1. events
3.2.2. http 配置
3.2.2.1. X-Forwarded-For
3.2.2.2. gzip
3.2.2.3. server_tokens
3.2.2.4. ssi
3.2.3. server
3.2.3.1. listen
3.2.3.2. 单域名虚拟主机
3.2.3.3. ssl 虚拟主机
3.2.3.4. 泛解析主机
3.2.3.5. location
3.2.3.6. expires
3.2.3.7. access
3.2.3.8. auth_basic
3.2.3.9. autoindex
3.2.3.10. try_files
3.2.3.11. stub_status
3.2.3.12. add_header
3.2.3.13. valid_referers
3.2.4. rewrite
3.2.4.1. http get 参数处理
3.2.5. fastcgi
3.2.5.1. spawn-fcgi
3.2.5.2. php-fpm
3.2.6. Proxy
3.2.6.1. proxy_cache
3.2.6.2. rewrite + proxy_pass
3.2.6.3. request_filename + proxy_pass
3.2.6.4. $request_uri 与 proxy_pass 联合使用
3.2.6.5. try_files 与 proxy_pass 共用
3.2.6.6. Proxy 与 SSI
3.2.6.7. Host
3.2.6.8. expires
3.2.6.9. X-Sendfile
3.2.6.10. proxy_http_version
3.2.6.11. proxy_set_header
3.2.6.12. timeout 超时时间
3.2.6.13. example
3.2.7. return
3.2.8. ngx_http_flv_module
3.2.9. ngx_http_mp4_module
3.2.10. limit_zone
3.2.11. Nginx 变量
3.2.11.1. http_user_agent
3.2.11.2. http_referer
3.2.11.3. request_filename
3.2.11.4. request_uri
3.2.11.5. remote_addr
3.2.11.6. 自定义变量
3.2.11.7. if 条件判断
3.3. Example
3.3.1. Nginx + Tomcat
3.4. FAQ
3.4.1. 405 Not Allowed?
3.4.2. 502 Bad Gateway?
3.4.3. 413 Request Entity Too Large
3.4.4. nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression
3.4.5. proxy_pass
3.4.6. proxy_pass SESSION 丢失问题
3.4.7. [alert] 55785#0: *11449 socket() failed (24: Too many open files) while connecting to upstream
3.4.8. server_name 与 SSI 注意事项
3.4.9. location 跨 document_root 引用,引用 document_root 之外的资源

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