Nginx完整配置说明

http://blog.csdn.net/marising/article/details/3979493

可以参考如下的完整例子

http://wiki.codemongers.com/NginxFullExample

[php] view plaincopy

  1. #用户 用户组
  2. user       www www;
  3. #工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点
  4. worker_processes  5;
  5. #错误日志
  6. error_log  logs/error.log;
  7. #pid文件位置
  8. pid        logs/nginx.pid;
  9. worker_rlimit_nofile 8192;
  10. events {
  11. #工作进程的最大连接数量,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行
  12. worker_connections  4096;
  13. }
  14. http {
  15. include    conf/mime.types;
  16. #反向代理配置,可以打开proxy.conf看看
  17. include    /etc/nginx/proxy.conf;
  18. #fastcgi配置,可以打开fastcgi.conf看看
  19. include    /etc/nginx/fastcgi.conf;
  20. default_type application/octet-stream;
  21. #日志的格式
  22. log_format   main ‘$remote_addr - $remote_user [$time_local] $status ‘
  23. ‘"$request" $body_bytes_sent "$http_referer" ‘
  24. ‘"$http_user_agent" "$http_x_forwarded_for"‘;
  25. #访问日志
  26. access_log   logs/access.log  main;
  27. sendfile     on;
  28. tcp_nopush   on;
  29. #根据实际情况调整,如果server很多,就调大一点
  30. server_names_hash_bucket_size 128; # this seems to be required for some vhosts
  31. #这个例子是fastcgi的例子,如果用fastcgi就要仔细看
  32. server { # php/fastcgi
  33. listen       80;
  34. #域名,可以有多个
  35. server_name  domain1.com www.domain1.com;
  36. #访问日志,和上面的级别不一样,应该是下级的覆盖上级的
  37. access_log   logs/domain1.access.log  main;
  38. root         html;
  39. location / {
  40. index    index.html index.htm index.php;
  41. }
  42. #所有php后缀的,都通过fastcgi发送到1025端口上
  43. #上面include的fastcgi.conf在此应该是有作用,如果你不include,那么就把fastcgi.conf的配置项放在这个下面。
  44. location ~ /.php$ {
  45. fastcgi_pass   127.0.0.1:1025;
  46. }
  47. }
  48. #这个是反向代理的例子
  49. server { # simple reverse-proxy
  50. listen       80;
  51. server_name  domain2.com www.domain2.com;
  52. access_log   logs/domain2.access.log  main;
  53. #静态文件,nginx自己处理
  54. location ~ ^/(images|javascript|js|css|flash|media|static)/  {
  55. root    /var/www/virtual/big.server.com/htdocs;
  56. #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
  57. expires 30d;
  58. }
  59. #把请求转发给后台web服务器,反向代理和fastcgi的区别是,反向代理后面是web服务器,fastcgi后台是fasstcgi监听进程,当然,协议也不一样。
  60. location / {
  61. proxy_pass      http://127.0.0.1:8080;
  62. }
  63. }
  64. #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。据说nginx可以根据后台响应时间调整。后台需要多个web服务器。
  65. upstream big_server_com {
  66. server 127.0.0.3:8000 weight=5;
  67. server 127.0.0.3:8001 weight=5;
  68. server 192.168.0.1:8000;
  69. server 192.168.0.1:8001;
  70. }
  71. server {
  72. listen          80;
  73. server_name     big.server.com;
  74. access_log      logs/big.server.access.log main;
  75. location / {
  76. proxy_pass      http://big_server_com;
  77. }
  78. }
  79. }

上面说的include的几个文件,都没有必要改,用的时候include一下就可以。

fastcgi.conf

[c-sharp] view plaincopy

  1. # fastcgi.conf
  2. fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  3. fastcgi_param  QUERY_STRING       $query_string;
  4. fastcgi_param  REQUEST_METHOD     $request_method;
  5. fastcgi_param  CONTENT_TYPE       $content_type;
  6. fastcgi_param  CONTENT_LENGTH     $content_length;
  7. fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
  8. fastcgi_param  REQUEST_URI        $request_uri;
  9. fastcgi_param  DOCUMENT_URI       $document_uri;
  10. fastcgi_param  DOCUMENT_ROOT      $document_root;
  11. fastcgi_param  SERVER_PROTOCOL    $server_protocol;
  12. fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
  13. fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
  14. fastcgi_param  REMOTE_ADDR        $remote_addr;
  15. fastcgi_param  REMOTE_PORT        $remote_port;
  16. fastcgi_param  SERVER_ADDR        $server_addr;
  17. fastcgi_param  SERVER_PORT        $server_port;
  18. fastcgi_param  SERVER_NAME        $server_name;
  19. fastcgi_index  index.php;
  20. # PHP only, required if PHP was built with --enable-force-cgi-redirect
  21. fastcgi_param  REDIRECT_STATUS    200;

proxy.conf

[c-sharp] view plaincopy

  1. # proxy.conf
  2. proxy_redirect          off;
  3. proxy_set_header        Host            $host;
  4. proxy_set_header        X-Real-IP       $remote_addr;
  5. proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
  6. client_max_body_size    10m;
  7. client_body_buffer_size 128k;
  8. proxy_connect_timeout   90;
  9. proxy_send_timeout      90;
  10. proxy_read_timeout      90;
  11. proxy_buffers           32 4k;

mine.types

[c-sharp] view plaincopy

  1. # mime.types
  2. types {
  3. text/html                             html htm shtml;
  4. text/css                              css;
  5. text/xml                              xml rss;
  6. image/gif                             gif;
  7. image/jpeg                            jpeg jpg;
  8. application/x-javascript              js;
  9. text/plain                            txt;
  10. text/x-component                      htc;
  11. text/mathml                           mml;
  12. image/png                             png;
  13. image/x-icon                          ico;
  14. image/x-jng                           jng;
  15. image/vnd.wap.wbmp                    wbmp;
  16. application/java-archive              jar war ear;
  17. application/mac-binhex40              hqx;
  18. application/pdf                       pdf;
  19. application/x-cocoa                   cco;
  20. application/x-java-archive-diff       jardiff;
  21. application/x-java-jnlp-file          jnlp;
  22. application/x-makeself                run;
  23. application/x-perl                    pl pm;
  24. application/x-pilot                   prc pdb;
  25. application/x-rar-compressed          rar;
  26. application/x-redhat-package-manager  rpm;
  27. application/x-sea                     sea;
  28. application/x-shockwave-flash         swf;
  29. application/x-stuffit                 sit;
  30. application/x-tcl                     tcl tk;
  31. application/x-x509-ca-cert            der pem crt;
  32. application/x-xpinstall               xpi;
  33. application/zip                       zip;
  34. application/octet-stream              deb;
  35. application/octet-stream              bin exe dll;
  36. application/octet-stream              dmg;
  37. application/octet-stream              eot;
  38. application/octet-stream              iso img;
  39. application/octet-stream              msi msp msm;
  40. audio/mpeg                            mp3;
  41. audio/x-realaudio                     ra;
  42. video/mpeg                            mpeg mpg;
  43. video/quicktime                       mov;
  44. video/x-flv                           flv;
  45. video/x-msvideo                       avi;
  46. video/x-ms-wmv                        wmv;
  47. video/x-ms-asf                        asx asf;
  48. video/x-mng                           mng;
  49. }

更多的,每个项的具体含义和配置选择,可以参考如下:

http://wiki.codemongers.com/NginxModules

可以依次从MainModule,Events Module,Http Core Module,Http UpStream Module的顺序看。

关于配置和模块的关系是,装了什么模块,就可以用到什么配置,因此配置是灵活多变的。

时间: 2024-08-06 13:48:31

Nginx完整配置说明的相关文章

Nginx 完整配置说明

#用户 用户组 user www www; #工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点 worker_processes 5: #错误日志 error_log logs/error.log; #pid文件位置 pid logs/nginx.pid; worker_rlimit_nofile 8192; events { #工作进程的最大连接数量,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行 worker_connections 409

深刻理解Nginx之Nginx完整安装

1.   Nginx安装 1.1预先准备 CentOS系统下,安装Nginx的库包依赖.安装命令如下: sudo yum groupinstall "DevelopmentTools" sudo yum install pcre pcre-devel sudo yum install zlib zlib-devel yum install perl-ExtUtils-Embed sudo yum install openssl openssl-devel 1.2 安装 最重要的特性和基

Quartz CronTrigger最完整配置说明

Quartz CronTrigger最完整配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年]  序号 说明   是否必填  允许填写的值 允许的通配符   1  秒  是  0-59    , - * /  2  分  是  0-59    , - * /  3 小时  是  0-23   , - * /  4  日  是  1-31   , - * ? / L W  5  月  是  1-12 or JAN-DEC   , - * /

nginx常用配置说明

nginx的主配置(nginx.conf)说明 #worker进程数量 worker_processes 1; #错误日志 error_log logs/error.log; #进程ID文件 pid logs/nginx.pid; #事件区块开始 events { #worker进程支持的最大连接数 worker_connections 1024; } #http区块开始 http { #nginx支持的媒体类型库文件 include mime.types; #默认的媒体文件 default_t

Nginx完整配置配置样例

nginx.conf user www www; ## Default: nobody worker_processes 5; ## Default: 1 error_log logs/error.log; pid logs/nginx.pid; worker_rlimit_nofile 8192; events { worker_connections 4096; ## Default: 1024 } http { include conf/mime.types; include /etc/n

nginx.conf配置说明

#定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]error_log ar/loginx/error.log info; #进程文件pid ar/runinx.pid; #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与ngi

Nginx常见配置说明以及案例图

worker_processes 8;#nginx进程数,建议设置为等于CPU总核心数worker_connections 65535;#单个进程最大连接数(最大连接数=连接数*进程数)client_header_buffer_size 32k; #上传文件大小限制large_client_header_buffers 4 64k; #设定请求缓client_max_body_size 8m; #设定请求缓autoindex on; #开启目录列表访问,合适下载服务器,默认关闭.tcp_nopu

nginx的配置说明

#定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]error_log /var/log/nginx/error.log info; #进程文件pid /var/run/nginx.pid; #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulim

Nginx ServerName 配置说明(转)

Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活,如果你要做多用户博客,那么每个用户拥有自己的二级域名也就很容易实现了. 下面我就来说说server_name的使用吧: server_name的匹配顺序 Nginx中的server_name指令主要用于配置基于名称虚拟主机,server_name指令在接到请求后的匹配顺序分别为: 1.准确的server_name匹配,例如: ? 1 ? 1 2 3 4 5 server { listen 80; server_name