Nginx配置文件详细说明

在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络.

  1 #运行用户
  2 user www-data;    
  3 #启动进程,通常设置成和cpu的数量相等
  4 worker_processes  1;
  5 
  6 #全局错误日志及PID文件
  7 error_log  /var/log/nginx/error.log;
  8 pid        /var/run/nginx.pid;
  9 
 10 #工作模式及连接数上限
 11 events {
 12     use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
 13     worker_connections  1024;#单个后台worker process进程的最大并发链接数
 14     # multi_accept on; 
 15 }
 16 
 17 #设定http服务器,利用它的反向代理功能提供负载均衡支持
 18 http {
 19      #设定mime类型,类型由mime.type文件定义
 20     include       /etc/nginx/mime.types;
 21     default_type  application/octet-stream;
 22     #设定日志格式
 23     access_log    /var/log/nginx/access.log;
 24 
 25     #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
 26     #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
 27     sendfile        on;
 28     #tcp_nopush     on;
 29 
 30     #连接超时时间
 31     #keepalive_timeout  0;
 32     keepalive_timeout  65;
 33     tcp_nodelay        on;
 34     
 35     #开启gzip压缩
 36     gzip  on;
 37     gzip_disable "MSIE [1-6]\.(?!.*SV1)";
 38 
 39     #设定请求缓冲
 40     client_header_buffer_size    1k;
 41     large_client_header_buffers  4 4k;
 42 
 43     include /etc/nginx/conf.d/*.conf;
 44     include /etc/nginx/sites-enabled/*;
 45 
 46     #设定负载均衡的服务器列表
 47      upstream mysvr {
 48     #weigth参数表示权值,权值越高被分配到的几率越大
 49     #本机上的Squid开启3128端口
 50     server 192.168.8.1:3128 weight=5;
 51     server 192.168.8.2:80  weight=1;
 52     server 192.168.8.3:80  weight=6;
 53     }
 54 
 55 
 56    server {
 57     #侦听80端口
 58         listen       80;
 59         #定义使用www.xx.com访问
 60         server_name  www.xx.com;
 61 
 62         #设定本虚拟主机的访问日志
 63         access_log  logs/www.xx.com.access.log  main;
 64 
 65     #默认请求
 66     location / {
 67           root   /root;      #定义服务器的默认网站根目录位置
 68           index index.php index.html index.htm;   #定义首页索引文件的名称
 69 
 70           fastcgi_pass  www.xx.com;
 71          fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name; 
 72           include /etc/nginx/fastcgi_params;
 73         }
 74 
 75     # 定义错误提示页面
 76     error_page   500 502 503 504 /50x.html;  
 77         location = /50x.html {
 78         root   /root;
 79     }
 80 
 81     #静态文件,nginx自己处理
 82     location ~ ^/(images|javascript|js|css|flash|media|static)/ {
 83         root /var/www/virtual/htdocs;
 84         #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
 85         expires 30d;
 86     }
 87     #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
 88     location ~ \.php$ {
 89         root /root;
 90         fastcgi_pass 127.0.0.1:9000;
 91         fastcgi_index index.php;
 92         fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
 93         include fastcgi_params;
 94     }
 95     #设定查看Nginx状态的地址
 96     location /NginxStatus {
 97         stub_status            on;
 98         access_log              on;
 99         auth_basic              "NginxStatus";
100         auth_basic_user_file  conf/htpasswd;
101     }
102     #禁止访问 .htxxx 文件
103     location ~ /\.ht {
104         deny all;
105     }
106      
107      }
108 }

以上是一些基本的配置,使用Nginx最大的好处就是负载均衡

如果要使用负载均衡的话,可以修改配置http节点如下:

 1 #设定http服务器,利用它的反向代理功能提供负载均衡支持
 2 http {
 3      #设定mime类型,类型由mime.type文件定义
 4     include       /etc/nginx/mime.types;
 5     default_type  application/octet-stream;
 6     #设定日志格式
 7     access_log    /var/log/nginx/access.log;
 8 
 9     #省略上文有的一些配置节点
10 
11     #。。。。。。。。。。
12 
13     #设定负载均衡的服务器列表
14      upstream mysvr {
15     #weigth参数表示权值,权值越高被分配到的几率越大
16     server 192.168.8.1x:3128 weight=5;#本机上的Squid开启3128端口
17     server 192.168.8.2x:80  weight=1;
18     server 192.168.8.3x:80  weight=6;
19     }
20 
21    upstream mysvr2 {
22     #weigth参数表示权值,权值越高被分配到的几率越大
23 
24     server 192.168.8.x:80  weight=1;
25     server 192.168.8.x:80  weight=6;
26     }
27 
28    #第一个虚拟服务器
29    server {
30     #侦听192.168.8.x的80端口
31         listen       80;
32         server_name  192.168.8.x;
33 
34       #对aspx后缀的进行负载均衡请求
35     location ~ .*\.aspx$ {
36 
37          root   /root;      #定义服务器的默认网站根目录位置
38           index index.php index.html index.htm;   #定义首页索引文件的名称
39 
40           proxy_pass  http://mysvr ;#请求转向mysvr 定义的服务器列表
41 
42           #以下是一些反向代理的配置可删除.
43 
44           proxy_redirect off;
45 
46           #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
47           proxy_set_header Host $host;
48           proxy_set_header X-Real-IP $remote_addr;
49           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
50           client_max_body_size 10m;    #允许客户端请求的最大单文件字节数
51           client_body_buffer_size 128k;  #缓冲区代理缓冲用户端请求的最大字节数,
52           proxy_connect_timeout 90;  #nginx跟后端服务器连接超时时间(代理连接超时)
53           proxy_send_timeout 90;        #后端服务器数据回传时间(代理发送超时)
54           proxy_read_timeout 90;         #连接成功后,后端服务器响应时间(代理接收超时)
55           proxy_buffer_size 4k;             #设置代理服务器(nginx)保存用户头信息的缓冲区大小
56           proxy_buffers 4 32k;               #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
57           proxy_busy_buffers_size 64k;    #高负荷下缓冲大小(proxy_buffers*2)
58           proxy_temp_file_write_size 64k;  #设定缓存文件夹大小,大于这个值,将从upstream服务器传
59 
60        }
61 
62      }
63 }

 

posted @ 2018-08-31 08:24  小猪爱上佩奇  阅读(155)  评论(0编辑  收藏  举报