标签:nginx php
文章内内容均参考网络上各类文章后自行整理,感谢原文作者的分享.
OS环境CentOS release 6.6
一,安装配置nginx
下载目前nginx最新的稳定版本nginx-1.8.0
# wget http://nginx.org/download/nginx-1.8.0.tar.gz
解压编译安装
# tar -xvf nginx-1.8.0.tar.gz# cd nginx-1.8.0
# ./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module \
--with-http_gzip_static_module --with-http_stub_status_module  --with-pcre
# make && make install
如果提示错误找不到pcre就用yum装一下
# yum install pcre-devel -y
建立nginx用户
# useradd -M -s /sbin/nologin nginx
修改nginx默认配置文件,有一些为新添
# vim /usr/local/nginx/conf/nginx.conf
#运行用户
user nginx;
#运行进程数,一般设置成和cpu的数量相等
worker_processes  1;
#开启全局错误日志及存放路径
error_log  logs/error.log;
#pid文件路径
pid    logs/nginx.pid;
#设定工作模式
events { 
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,仅用于linux2.6以上内核,可大大提高nginx的性能 
use    epoll;
#最大连接数
worker_connections  1024;
}
#设定http服务器
http { 
      #设定mime类型,类型由mime.type文件定义
    include       mime.types;
    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;
    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off
    #以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile        on;
    #设定连接超时时间,单位秒
    keepalive_timeout  65;
    #gzip压缩开关
    #gzip  on;
    #设定负载均衡的服务器列表,weight为权重,数字越大,被分配的几率越大,此处仅设为本地php服务
    upstream php_processes {
        server 127.0.0.1:9000 weight=1;
        server 127.0.0.1:9001 weight=1;
      }
    #虚拟主机设定,一个server代表一个主机
    server {
          #http服务的端口
        listen       8081;
        #设定访问域名
        server_name  localhost;
        #本虚拟主机内的访问日志记录
        access_log  logs/host.access.log;
        #默认主目录和主索引文件
        root /var/www/html;
            index  index.php index.html index.htm;
        #默认http请求根目录,location类似虚拟目录的概念
        location / {
            #网站根目录设定
           root   /var/www/html;
           #定义首页索引文件名,此处添加index.php
           index  index.php index.html index.htm;
           #设定PHP文件的处理对象
           location ~ \.php$ {
                    root           html;    
                    try_files  $uri =404;
                    #fastcgi_pass可写单个IP,亦可用upstream名定义一个集群池
                    fastcgi_pass   php_processes;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                    include        fastcgi_params;
                }
                #设定静态文件处理的配置
                location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
                    expires 30d;
                    log_not_found off;
                    tcp_nodelay off;
                    open_file_cache max=1000 inactive=120s;
                    open_file_cache_valid 45s;
                    open_file_cache_min_uses 2;
                    open_file_cache_errors off;
                }
                #设定一些比较大文件类型的缓存时间
                location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
                    expires 30d;
                    tcp_nodelay off;
                }
        } #最外层location结束
        #定义错误提示页面
        error_page  404 500 502 503 504  /50x.html;
            location = /50x.html {
                root   /root;
                }
       }#一个server配置结束
 }#http结束
待续...
标签:nginx php
原文地址:http://nextsky.blog.51cto.com/279913/1714338