nginx超详细讲解之概述

来源:互联网 发布:知乎 无锡美食 编辑:程序博客网 时间:2024/05/22 00:13
下载地址:http://nginx.org/en/download.html官方文档:http://nginx.org/en/docs/http/ngx_http_core_module.html

一、nginx目录

conf 配置文件
html 网页文件
logs 日志文件 
sbin 主要二进制程序
nginx默认使用80端口启动。

二、nginx配置文件详解

nginx主要配置文件只有一个,就是/conf/nginx.conf

[javascript] view plain copy
  1. #user  nobody;   //nginx用户和组,windows版本不可以指定,例如:user nginx nginx  
  2. //全局区  
  3. worker_processes  1;    // 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数  
  4.  
  5. #error_log  logs/error.log;    //错误日志存放路径  
  6. #error_log  logs/error.log  notice;  
  7. #error_log  logs/error.log  info;  
  8.  
  9. #pid        logs/nginx.pid;    //pid(进程标识符):存放路径。  
  10.   
  11.   
  12. events {  
  13.     use epoll;    //使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定  
  14.   
  15.     // 一般是配置nginx连接的特性  
  16.     // 如1个word能同时允许多少连接  
  17.     worker_connections  1024;       // 这是指 一个子进程最大允许连1024个连接  
  18.     keepalive_timeout 60;       //keepalive超时时间  
  19.   
  20. }  
  21.   
  22.   
  23. http {//这是配置http服务器的主要段  
  24.     include       mime.types;       //设定mime类型,类型由mime.type文件定义  
  25.     default_type  application/octet-stream;  
  26.   
  27.   
  28.     //该段为自定义log输出样式函数。配合下面的access_log使用  
  29.     //日志格式设置:  
  30.     //$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;  
  31.     //$remote_user:用来记录客户端用户名称;  
  32.     //$time_local: 用来记录访问时间与时区;  
  33.     //$request: 用来记录请求的url与http协议;  
  34.     //$status: 用来记录请求状态;成功是200,  
  35.     //$body_bytes_sent :记录发送给客户端文件主体内容大小;  
  36.     //$http_referer:用来记录从那个页面链接访问过来的;  
  37.     //$http_user_agent:记录客户浏览器的相关信息  
  38.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  39.     #                  '$status $body_bytes_sent "$http_referer" '  
  40.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  41.       
  42.   
  43.   
  44.     //开启log输出。输出位置为logs/access.log(相对于nginx安装根路径)  使用自定的哪个输出格式  
  45.     #access_log  logs/access.log  main;  
  46.   
  47.     sendfile        on;  
  48.     #tcp_nopush     on;  
  49.  
  50.     #keepalive_timeout  0;  
  51.     keepalive_timeout  65;  
  52.     //是否开启压缩  
  53.     #gzip  on;  
  54.   
  55.     server {// 这是虚拟主机段  
  56.         listen       80;//监控端口  
  57.         server_name  localhost;//监控域名  
  58.  
  59.         #charset koi8-r;  
  60.  
  61.         #access_log  logs/host.access.log  main;  
  62.   
  63.         location / {//定位,个人理解就是java中的filter。  
  64.             root   html;   //符合条件请求转发路径  
  65.             index  index.html index.htm;      //索引  
  66.         }  
  67.  
  68.         #error_page  404              /404.html;  
  69.  
  70.         # redirect server error pages to the static page /50x.html  
  71.         #  
  72.         error_page   500 502 503 504  /50x.html;        //错误码值和对应请求  
  73.         location = /50x.html {  
  74.             root   html;  
  75.         }  
  76.  
  77.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  78.         #  
  79.         #location ~ \.php$ {  
  80.         #    proxy_pass   http://127.0.0.1;  
  81.         #}  
  82.  
  83.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  84.         #  
  85.         #location ~ \.php$ {  
  86.         #    root           html;  
  87.         #    fastcgi_pass   127.0.0.1:9000;  
  88.         #    fastcgi_index  index.php;  
  89.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  90.         #    include        fastcgi_params;  
  91.         #}  
  92.  
  93.         # deny access to .htaccess files, if Apache's document root  
  94.         # concurs with nginx's one  
  95.         #  
  96.         #location ~ /\.ht {  
  97.         #    deny  all;  
  98.         #}  
  99.     }  
  100.  
  101.  
  102.     # another virtual host using mix of IP-, name-, and port-based configuration  
  103.     #  
  104.     #server {  
  105.     #    listen       8000;  
  106.     #    listen       somename:8080;  
  107.     #    server_name  somename  alias  another.alias;  
  108.  
  109.     #    location / {  
  110.     #        root   html;  
  111.     #        index  index.html index.htm;  
  112.     #    }  
  113.     #}  
  114.  
  115.  
  116.     # HTTPS server  
  117.     #  
  118.     #server {  
  119.     #    listen       443 ssl;  
  120.     #    server_name  localhost;  
  121.  
  122.     #    ssl_certificate      cert.pem;  
  123.     #    ssl_certificate_key  cert.key;  
  124.  
  125.     #    ssl_session_cache    shared:SSL:1m;  
  126.     #    ssl_session_timeout  5m;  
  127.  
  128.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  129.     #    ssl_prefer_server_ciphers  on;  
  130.  
  131.     #    location / {  
  132.     #        root   html;  
  133.     #        index  index.html index.htm;  
  134.     #    }  
  135.     #}  
  136.   
  137. }  

三、详细讲解(点击下面连接跳转到具体详细讲解页面)
1.nginx超详细讲解之概述2.nginx超详细讲解之server,log3.nginx超详细讲解之location,rewrite,反向代理及负载均衡4.nginx超详细讲解之压缩和缓存


*如果有说明毛病和问题请大家提出和指正。