@dungan
2018-09-21T09:12:14.000000Z
字数 2030
阅读 73
Nginx
nginx.conf 是nginx的主配置文件,整个配置文件是以区块区分的,每个区块以'{}'来组织,nginx 配置文件主要分为六个区域:
mainevents {....}http {....upstream myproject {.....}server {....location {....}}server {....location {....}}....}
各个区块的参数说明如下
user www www;worker_processes auto;error_log /data/wwwlogs/error_nginx.log crit;pid /var/run/nginx.pid;worker_rlimit_nofile 51200;
user: 指定 Nginx Worker 进程运行用户以及用户组;
worker_processes : 要开启的子进程数, 一般情况下,最好设置成auto,让系统自动检测;从理论上来说,worker_processes的值越大,可以支持的并发处理量就越多,如果是多核CPU,建议指定和CPU的数量一样的进程数即可;
error_log:用来定义全局错误日志文件。日志输出级别有debug、info、notice、warn、error、crit可供选择,其中,debug输出日志最为最详细,而crit输出日志最少;
worker_rlimit_nofile:用于指定一个nginx进程可以打开的最多文件描述符数目,可以使用命令“ulimit -n 数量”来设置;
events {use epoll;worker_connections 51200;multi_accept on;}
use:指定nginx的 IO 模型;
wroker_connections:每个进程能够接收前端的最大请求数,进程的最大连接数受Linux系统进程的最大打开文件数限制;
include mime.types;default_type application/octet-stream;server_names_hash_bucket_size 128;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_max_body_size 1024m;client_body_buffer_size 10m;sendfile on;tcp_nopush on;keepalive_timeout 120;server_tokens off;tcp_nodelay on;
include mime.types:nginx支持的媒体类型;
default_type::默认的媒体类型;
sendfile on:开启高效传输模式;
keepalive-timeout:客户端连接保持活动的超时时间,在超过这个时间之后,服务器会关闭该连接;
listen 80;server_name www.test.com test.com;access_log /data/wwwlogs/access_nginx.log combined;root /data/wwwroot/test;index index.html index.htm index.php;#error_page 404 /404.html;#error_page 502 /502.html;
listen:提供服务的端口;
server_name:提供服务的域名,多个域名之间用空格分开;
root :站点根目录;
index:站点的默认入口文件,还可以在 'location /' 区块中重写;
access_log:访问日志,注意区分 main 区块的 error_log,前者是访问日志,后者是错误日志;
主要实现对 url 使用定好的规则进行匹配,匹配成功后就可以交给后续的处理程序实现 url 重写或重定向等功能!
# 网站根目录的访问都重定向到根目录下 index.php 中去!location / {if( ! -e $request_filename) {rewrite ^(.*)$ /index.php?s=$1 last;break;}}
# nginx 本身不会解析php脚本,它是通过 cgi(Common Gateway Interface 通用网关接口)来调用php 解析器的;# 这里的意思是凡是见到 .php 文件就把它交给php解析器去处理!location ~ [^/]\.php(/|$) {#fastcgi_pass remote_php_ip:9000;fastcgi_pass unix:/dev/shm/php-cgi.sock;fastcgi_index index.php;include fastcgi.conf;}