Nginx

安装

NGINXVER=1.10.3
#apt-get install -y libpcre3-dev libssl-dev build-essential
wget -c "http://nginx.org/download/nginx-${NGINXVER}.tar.gz"
tar xf nginx-${NGINXVER}.tar.gz
cd nginx-${NGINXVER}
./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --add-module=
make
make install
ln -s /opt/nginx/sbin/nginx /sbin/nginx
#sed -i "s/#user.*nobody;/user www;/" /opt/nginx/conf/nginx.conf
#sed -i "s/worker_processe.*1;/worker_processes 2;/" /opt/nginx/conf/nginx.conf
#sed -i "s/#pid/pid/" /opt/nginx/conf/nginx.conf
#sed -i "s/worker_connections/use epoll;\n    worker_connections/" /opt/nginx/conf/nginx.conf

groupadd www
useradd www -g www

配置文件

nginx.conf

user                    www;
worker_processes        1;
 
error_log   /srv/logs/nginx-error.log;
pid         /srv/logs/nginx.pid;
 
events {
    use                 epoll;
    worker_connections  10240;
}
 
 
http {
    include       mime.types;
    charset utf8;
    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  /srv/logs/nginx-access.log  main;
 
    sendfile                on;
    tcp_nopush              on;
    keepalive_timeout       65;
    client_max_body_size    20m;
 
    client_header_timeout   3m;
    client_body_timeout     3m;
    send_timeout            3m;
 
    gzip                    on;
    gzip_static             on;
    gzip_min_length         1k;
    gzip_buffers            16 128k;
    gzip_http_version       1.1;
    gzip_comp_level         9;
    gzip_types              text/plain application/javascript application/x-javascript text/css application/xml;
    gzip_vary               on;
    gzip_proxied            any;
 
    server {
        listen       80;
        server_name  localhost;
 
        location / {
            root   /srv/;
            index  index.html index.htm index.php;
        }
        location /nginx_status {
            stub_status on;
            access_log off;
        }
        location ~ ^.+\.php {
            root           /srv/;
            include     fastcgi.conf;
        }
        location ~ /\. {
            deny all;
        }
    }
}

common.conf

set $sub_domain "www";
if ($http_host ~ "([^\.]+)\.([^\.]+\..+)$") {
    set $sub_domain $1;
    set $main_domain $2;
}
 
set $root_dir /srv/$main_domain/$sub_domain/;
access_log  /srv/logs/${sub_domain}.${main_domain}.access.log  main;
 
location ~ ^.+\.(ico|gif|bmp|jpg|jpeg|png|swf|js|css) {
    root            $root_dir;
    access_log      off;
    log_not_found   off;
    expires         30d;
}
location ~ ^.+\.php {
    root        $root_dir;
    include     fastcgi.conf;
}
location ~ /\. {
    deny all;
}

fastcgi.conf

fastcgi_pass   127.0.0.1:9000;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
 
fastcgi_param  PATH_INFO          $fastcgi_path_info;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
 
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
 
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;
 
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
 
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
 
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

启动脚本

# 添加启动脚本
$ vim /etc/init.d/nginx

#!/bin/bash
# 启动脚本内容
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start/stop nginx
### END INIT INFO

export NGINX=/opt/nginx/sbin/nginx

case "$1" in
  start)
    # 启动命令行。此处自定义分辨率、控制台号码或其它参数。
    $NGINX &
    echo "Starting nginx..."
    ;;
  stop)
    # 终止命令行。此处控制台号码与启动一致。
    $NGINX -s stop
    echo "nginx stopped"
    ;;
  *)
    echo "Usage: /etc/init.d/nginx {start|stop}"
    exit 1
    ;;
esac
exit 0

# 设置可执行
$ chmod +x /etc/init.d/nginx