1.通过nginx的安装脚本来讲解nginx的安装
- #!/bin/bash
- #author zhangyifei
- #2013 2.27
- #email zyfforlinux@163.com
- #源码包的下载
- cd /usr/local/src
- wget http://down1.chinaunix.net/distfiles/nginx-1.2.5.tar.gz
- wget http://down1.chinaunix.net/distfiles/pcre-8.30.tar.bz2
- #pcre库的安装以支持nginx的rewiter的正则表达式
- tar jxvf pcre-8.30.tar.bz2
- cd pcre-8.30
- ./configure
- make && make install
- cd ..
- pcredir="pcre-8.30"
- soft="nginx-1.2.5.tar.gz"
- softdir="nginx-1.2.5"
- if [ -f $soft ]; then
- tar zxvf $soft
- else
- echo "$soft not exis"
- exit
- fi
- #询问用户是否安装ssl模块和status模块
- read -p "do you install http_stub_status_module [Y/N]" args1
- echo "$args1"
- while [ "$args1" != "Y" ] && [ "$args1" != "y" ] && [ "$args1" != "N" ] && [ "$args1" != "n" ]
- do
- read -p "please input [Y/N]" args1
- done
- if [ "$args1" == "Y" ] || [ "$args1" == "y" ]; then
- args1="--with-http_stub_status_module"
- else
- args1=""
- fi
- echo "$args1"
- read -p "do you install --with-http_ssl_module [Y/N]" args2
- while [ "$args2" != "Y" ] && [ "$args2" != "y" ] && [ "$args2" != "N" ] && [ "$args2" != "n" ]
- do
- read -p "please input [Y/N]" args2
- done
- echo "$args2"
- if [ "$args2" == "Y" ] || [ "$args2" == "y" ]; then
- args2="--with-http_ssl_module"
- else
- args2=""
- fi
- echo "$args2"
- #创建启动nginx的用户 并且不可登录
- nginxuser="nginx"
- useradd -s /sbin/nolgin $nginxuser
- cd $softdir
- ./configure --prefix=/usr/local/nginx \
- --user=$nginxuser \
- --group=$nginxuser \
- --with-pcre=/usr/local/src/$pcredir \
- $args1 $args2
- make && make install
2.下面讲解nginx的默认配置文件
- #user nobody; 指定启动nginx的用户和用户组
- worker_processes 1; 指定开启nginx的进程数 一般和逻辑cpu的
- #error_log logs/error.log; 错误日志的存放位置 和格式
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #pid logs/nginx.pid; 进程ID文件的存放位置
- events {
- worker_connections 1024; 单个后台worker process进程的最大并发链接数
- use epoll; 使用epoll机制 大大提高nginx的性能
- }
- http {
- include mime.types; 可处理的类型 html的类型威 text/html
- 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 logs/access.log main;
- sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65; keepalive的超时时间
- #gzip on; gzip功能开启
- server {
- listen 80; 监听的端口
- server_name localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- root html; 设定根目录
- index index.html index.htm; 设定索引启动器文件
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html; 定义错误文件重定向
- location = /50x.html {
- root html;
- }
- }
- }