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

第 42 章 Nginx

目录

42.1. Installing
42.1.1. Installing by apt-get under the debain/ubuntu
42.1.2. CentOS
42.1.3. installing by source
42.1.4. config test
42.1.5. rotate log
42.1.5.1. log shell
42.1.5.2. /etc/logrotate.d/nginx
42.2. nginx 配置文件
42.2.1. events
42.2.2. http 配置
42.2.2.1. X-Forwarded-For
42.2.2.2. gzip
42.2.2.3. server_tokens
42.2.2.4. ssi
42.2.3. server
42.2.3.1. listen
42.2.3.2. 单域名虚拟主机
42.2.3.3. ssl 虚拟主机
42.2.3.4. 泛解析主机
42.2.3.5. location
42.2.3.6. expires
42.2.3.7. access
42.2.3.8. auth_basic
42.2.3.9. autoindex
42.2.3.10. try_files
42.2.3.11. stub_status
42.2.3.12. add_header
42.2.3.13. valid_referers
42.2.4. rewrite
42.2.4.1. http get 参数处理
42.2.5. fastcgi
42.2.5.1. spawn-fcgi
42.2.5.2. php-fpm
42.2.6. Proxy
42.2.6.1. proxy_cache
42.2.6.2. rewrite + proxy_pass
42.2.6.3. request_filename + proxy_pass
42.2.6.4. $request_uri 与 proxy_pass 联合使用
42.2.6.5. try_files 与 proxy_pass 共用
42.2.6.6. Proxy 与 SSI
42.2.6.7. Host
42.2.6.8. expires
42.2.6.9. X-Sendfile
42.2.6.10. proxy_http_version
42.2.6.11. proxy_set_header
42.2.6.12. timeout 超时时间
42.2.6.13. example
42.2.7. return
42.2.8. ngx_http_flv_module
42.2.9. ngx_http_mp4_module
42.2.10. limit_zone
42.2.11. Nginx 变量
42.2.11.1. http_user_agent
42.2.11.2. http_referer
42.2.11.3. request_filename
42.2.11.4. request_uri
42.2.11.5. remote_addr
42.2.11.6. 自定义变量
42.2.11.7. if 条件判断
42.3. Example
42.3.1. Nginx + Tomcat
42.4. FAQ
42.4.1. 405 Not Allowed?
42.4.2. 502 Bad Gateway?
42.4.3. 413 Request Entity Too Large
42.4.4. nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression
42.4.5. proxy_pass
42.4.6. proxy_pass SESSION 丢失问题
42.4.7. [alert] 55785#0: *11449 socket() failed (24: Too many open files) while connecting to upstream
42.4.8. server_name 与 SSI 注意事项
42.4.9. location 跨 document_root 引用,引用 document_root 之外的资源

42.1. Installing

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

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

42.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
			

42.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'
			

42.1.4. config test

$ sudo service nginx configtest
Testing nginx configuration: nginx.
			

42.1.5. rotate log

42.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 &
				
				

42.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