Nginx技术研究系列6-配置详解
前两篇文章介绍了Nginx反向代理和动态路由: Ngnix技术研究系列1-通过应用场景看Nginx的反向代理 Ngnix技术研究系列2-基于Redis实现动态路由 随着研究的深入,很重要的一点就是了解Nginx各个配置和作用。整理一下分享给大家。 一、先说Nginx配置文件 Nginx的配置文件是一个普通的纯文本文件,使用了Nginx自定义的一套配置语法,更接近于脚本语言,混合了Shell、Perl和C的部分特性:
Ngnix配置文件的默认位置在: /usr/local/openresty/nginx/conf/nginx.conf 可以复制新建一个配置文件。 ?二、进程配置 进程配置指令不属于任何配置块,只能在全局域(main)配置 worker_directory path; 三、运行日志配置 在Nginx中运行日志分为两种,记录TCP/HTTP访问请求的access_log和记录服务器错误信息的error_log 四、Events配置 Nginx采用事件驱动,利用操作系统内核提供的epoll、kqueue等系统调用来高效地处理网络连接,events配置块就是用来配置Nginx的事件机制。Events配置指令不多,默认配置就饿可以工作的很好 五、Http配置 Nginx使用http块配置HTTP相关的所有功能,包括cache、fastcgi、gzip、server、location、proxy、upsteam等。 http { 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 main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; 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; } # proxy the PHP scripts to Apache listening on 127.0.0.1: # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 9000 .php$ { # root html; # fastcgi_pass ; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files,if Apaches document root # concurs with nginxs one # #location ~ /.ht { # deny all; #} } # another virtual host using mix of IP-,name-,and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} resolver address … [valid=time] [ipv6=on|off] 配置域名解析服务器,否则Nginx将无法正确解析域名的地址,无法访问后端的Web服务 keepalive_timeout timeout; 设置keepalive的超时时间,默认75s,主要用于客户端复用Http长连接,提高服务器的性能,如果希望服务器发送数据后能主动断开连接,可以设置为0 access_log path[format [buffer=size][flush=time][if=condition]]; access_log指令用于配置http的访问日志,日志的格式由log_format决定,为了优化磁盘读写,可以设置buffer和flush选项,指定写磁盘的缓冲区大小和刷新时间。 access_log /var/logs/nginx/access.log buffer=8k flush=1s; ?六、Proxy配置 proxy_connect_timeout time; proxy_cookie_domain off; proxy_send_timeout time ? 周国庆 2017/10/25 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |