Debian系统下为PHP程序配置Nginx服务器的基本教程

2019-10-17 20:00:21王冬梅

  error_log logs/error.log;

    设置pid文件路径,可以使用kill命令发送相关信号

  pid    logs/nginx.pid;

    event模块配置选项,event模块主要控制nginx处理连接的方式

  events {

  #如果在configure时指定的不止一个事件模型,可以通过use告诉nginx要使用哪一个模型:seletc、poll、kqueue、epoll、rtsig、/dev/poll、eventport等

  use epoll;

  #worker_connections和worker_processes可以计算你的理论最大链接数, worker_connections*worker_processes

  worker_connections 1024;

}

    http模块里面主要是对http服务器相关属性进行设置

  

 http {

  #可以用include指令包含一些其他文件,支持通配符,可以使用绝对路径,也可以使用相对路径,相对路径以nginx.conf为根据

  include    mime.types;

  #设置默认的MIME类型

  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拷贝文件在内核态完成,更加高效

  sendfile    on;

  #tcp_nopush   on;

  #可以设置两个值,第一个表示客户端与服务器长连接的超时时间,超过这个时间,服务器将关闭连接。第二个值指定的应答头中keep-alive中timeout的值,让浏览器知道什么时候关闭连接。

  keepalive_timeout 65;

  #开启gzip压缩

  gzip on;

  #在三次握手时,发送给客户端应答后的超时时间,目前还没进入连接状态,只完成了两次握手,如果在规定时间没收到应答包,nginx将关闭链接

  send_timeout 30

  server {

     xxx

  }

}

    server模块嵌在http模块中,主要用来配置虚拟主机

  server {

    #指定server字段中可以被访问到的ip地址及端口

    listen    80;

    #将http请求的主机头与server中的server_name参数进行匹配,并找出第一个结果,如果没有server_name参数匹配上,则第一个出现listen的server将被匹配,多域名用空格分割

    server_name www.nginx.com;

    #设个指令是应答头重的content-type字段使用指定的编码集,off表示不在应答头重添加content-type信息

    charset off;

    #指定www.nginx.com域名的访问日志路径及格式

    access_log logs/host.access.log main;

    #如果在url中没有指定文件,则设置一个默认主页,可以设置多个文件,空格分开,可以在http、server、location中设置

    index index.php index.htm;

    #根据URL的不同需求进行配置,可以使用字符串和正则匹配,最确切的匹配被使用,搜索到第一个后会停止

    # ~* 不区分大小写;~ 区分大小写;^~ 禁止在字符串匹配后检查正则;= 在URL和location之间精确匹配,匹配完成后不做额外搜索。

    location /i/ {

      #请求到达后的文件根目录,在请求中root会把location匹配的值加到root指定的值后面,请求/i/a.php,则会是/html/i/a.php响应

      root  html;

      #在location中设置index

      index index.html index.htm;

    }

    #为错误代码指定相应的错误界面,可以用在http、server、location字段中。

    error_page 404       /404.html;

    # redirect server error pages to the static page /50x.html

    error_page  500 502 503 504 /50x.html;

    #精确匹配50x.html,真实响应是/html/50x.html

    location = /50x.html {

      root  html;

    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80

    location ~ .php$ {

      proxy_pass  http://127.0.0.1;

    }

    #配置php脚本传至fastcgi

    location ~ .php$ {

     root      html;

     fastcgi_pass  127.0.0.1:9000;

     fastcgi_index index.php;

     #/scripts是php脚本所在的目录

      fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

      include    fastcgi_params;

    }

    #拒绝访问.htaccess文件

    location ~ /.ht {

      deny all;

    }

  }