centos 安装nginx 配置nginx

来源:互联网 发布:mysql bit类型 默认值 编辑:程序博客网 时间:2024/03/29 22:25

1 源代码编译Nginx

wget http://nginx.org/download/nginx-1.3.12.tar.gz
把源码解压缩之后,在终端里运行如下命令:

./configure
make
make install

默认情况下,Nginx 会被安装在 /usr/local/nginx。通过设定编译选项,你可以改变这个设定。

 

 

2 nginx的启动命令是

/usr/local/sbin/nginx -c  /usr/local/nginx/conf/nginx.conf

-c制定配置文件的路径,不加-nginx会自动加载默认路径的配置文件。 

nginx有-s参数可对nginx服务进行管理:
# /usr/local/sbin/nginx -h

-s signal : send signal to a master process: stop, quit, reopen, reload 

# /usr/local/nginx/sbin/nginx -s  reload 
nginx已经重启成功

 

 

3 Nginx配置文件详细说明

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

#运行用户

user www-data;   

#启动进程,通常设置成和cpu的数量相等

worker_processes  1;

#全局错误日志及PID文件

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;


#工作模式及连接数上限

events {

      use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能


}

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

     #设定mime类型,类型由mime.type文件定义

      include       /etc/nginx/mime.types;

    default_type  application/octet-stream;
    #设定日志格式
    access_log    /var/log/nginx/access.log;

    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;
   
    #开启gzip压缩
    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    #设定请求缓冲
    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    #设定负载均衡的服务器列表
     upstream mysvr {
    #weigth参数表示权值,权值越高被分配到的几率越大
    #本机上的Squid开启3128端口
    server 192.168.8.1:3128 weight=5;
    server 192.168.8.2:80  weight=1;
    server 192.168.8.3:80  weight=6;
    }


   server {
    #侦听80端口
        listen       80;
        #定义使用www.xx.com访问
        server_name  www.xx.com;

        #设定本虚拟主机的访问日志
        access_log  logs/www.xx.com.access.log  main;

    #默认请求
    location / {
          root   /root;      #定义服务器的默认网站根目录位置
          index index.php index.html index.htm;   #定义首页索引文件的名称

          fastcgi_pass  www.xx.com;
         fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
          include /etc/nginx/fastcgi_params;
        }

    # 定义错误提示页面
    error_page   500 502 503 504 /50x.html; 
        location = /50x.html {
        root   /root;
    }

    #静态文件,nginx自己处理
    location ~ ^/(images|javascript|js|css|flash|media|static)/ {
        root /var/www/virtual/htdocs;
        #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
        expires 30d;
    }
    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
    location ~ \.php$ {
        root /root;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
        include fastcgi_params;
    }
    #设定查看Nginx状态的地址
    location /NginxStatus {
        stub_status            on;
        access_log              on;
        auth_basic              "NginxStatus";
        auth_basic_user_file  conf/htpasswd;
    }
    #禁止访问 .htxxx 文件
    location ~ /\.ht {
        deny all;
    }
    
     }
}




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

查看Linux 内核版本

 uname -a
Linux server10 2.6.18-274.el5 #1 SMP Fri Jul 22 04:43:29 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux



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

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
     #设定mime类型,类型由mime.type文件定义
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    #设定日志格式
    access_log    /var/log/nginx/access.log;

    #省略上文有的一些配置节点

    #。。。。。。。。。。

    #设定负载均衡的服务器列表
     upstream mysvr {
    #weigth参数表示权值,权值越高被分配到的几率越大
    server 192.168.8.1x:3128 weight=5;#本机上的Squid开启3128端口
    server 192.168.8.2x:80  weight=1;
    server 192.168.8.3x:80  weight=6;
    }

   upstream mysvr2 {
    #weigth参数表示权值,权值越高被分配到的几率越大

    server 192.168.8.x:80  weight=1;
    server 192.168.8.x:80  weight=6;
    }

   #第一个虚拟服务器
   server {
    #侦听192.168.8.x的80端口
        listen       80;
        server_name  192.168.8.x;

      #对aspx后缀的进行负载均衡请求
    location ~ .*\.aspx$ {

         root   /root;      #定义服务器的默认网站根目录位置
          index index.php index.html index.htm;   #定义首页索引文件的名称

          proxy_pass  http://mysvr ;#请求转向mysvr 定义的服务器列表

          #以下是一些反向代理的配置可删除.

          proxy_redirect off;

          #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          client_max_body_size 10m;    #允许客户端请求的最大单文件字节数
          client_body_buffer_size 128k;  #缓冲区代理缓冲用户端请求的最大字节数,
          proxy_connect_timeout 90;  #nginx跟后端服务器连接超时时间(代理连接超时)
          proxy_send_timeout 90;        #后端服务器数据回传时间(代理发送超时)
          proxy_read_timeout 90;         #连接成功后,后端服务器响应时间(代理接收超时)
          proxy_buffer_size 4k;             #设置代理服务器(nginx)保存用户头信息的缓冲区大小
          proxy_buffers 4 32k;               #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
          proxy_busy_buffers_size 64k;    #高负荷下缓冲大小(proxy_buffers*2)
          proxy_temp_file_write_size 64k;  #设定缓存文件夹大小,大于这个值,将从upstream服务器传

       }

     }
}

 






nginx缓存设置


目的: 缓存nginx服务器的静态文件。如css,js,htm,html,jpg,gif,png,flv,swf,这些文件都不是经常更新。便于缓存以减轻服务器的压力。
实现: nginx proxy_cache可以将用户的请缓存到本地一个目录,当下一个请求时可以直接调取缓存文件,就不用去后端服务器去取文件了。
配置: 打开配置文件/etc/nginx/nginx.conf

user  www www;
worker_processes 2;
error_log  /var/log/nginx/nginx_error.log  crit;
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}

http
{
  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 8m;

  sendfile on;
  tcp_nopush     on;
  keepalive_timeout 0;
  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  ##cache##
  proxy_connect_timeout 5;
  proxy_read_timeout 60;
  proxy_send_timeout 5;
  proxy_buffer_size 16k;
  proxy_buffers 4 64k;
  proxy_busy_buffers_size 128k;
  proxy_temp_file_write_size 128k;
  proxy_temp_path /home/temp_dir;
  proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
  ##end##

  gzip    on;
  gzip_min_length   1k;
  gzip_buffers   4 8k;
  gzip_http_version  1.1;
  gzip_types   text/plain application/x-javascript text/css  application/xml;
  gzip_disable "MSIE [1-6]\.";

  log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
  upstream appserver { 
        server 192.168.1.251;
  }
  server {
        listen       80 default;
        server_name blog.slogra.com;
        location ~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {
              proxy_pass http://appserver ;
              proxy_redirect off;
              proxy_set_header Host $host;
              proxy_cache cache_one;
              proxy_cache_valid 200 302 1h;
              proxy_cache_valid 301 1d;
              proxy_cache_valid any 1m;
              expires 30d;
        }
        location ~ .*\.(php)(.*){
             proxy_pass http://appserver ;
             proxy_set_header        Host $host;
             proxy_set_header        X-Real-IP $remote_addr;
             proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for; 
        }
        access_log /var/log/nginx/blog.slogra.com.log;
  }
}
红色部分是配置缓存的参数。
说明:
1、http段设置。
proxy_temp_path /home/temp_dir;设置临时目录
proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;设置缓存目录为二级目录,共享内存区大小,非活动时间,最大容量,注意临时目录要跟缓存目录在同一个分区。
2、server段设置 
请求静态文件设置。
proxy_cache cache_one;设置缓存共享内存区块,也就是keys_zone名称。
proxy_cache_valid 200 302 1h;设置http状态码为200,302缓存时间为1小时。
expires 30d;设置失期时间,为30天
请求动态文件设置。
proxy_pass http://appserver ;不进行缓存,直接转到后端服务器。
测试: 当客户端发起http请求时在服务器上会产一个缓存文件如
 

/home/cache/0/b9/8bd841b1c44ee5b91457eb561e44eb90

OK

本文发布于夜空http://blog.slogra.com/,转载请保留此版权信息!








1. 启动 Nginx

poechant@ubuntu:sudo ./sbin/nginx

2. 停止 Nginx

poechant@ubuntu:sudo ./sbin/nginx -s stoppoechant@ubuntu:sudo ./sbin/nginx -s quit

-s都是采用向 Nginx 发送信号的方式。

3. Nginx 重载配置

poechant@ubuntu:sudo ./sbin/nginx -s reload

上述是采用向 Nginx 发送信号的方式,或者使用:

poechant@ubuntu:service nginx reload

4. 指定配置文件

poechant@ubuntu:sudo ./sbin/nginx -c /usr/local/nginx/conf/nginx.conf

-c表示configuration,指定配置文件。

5. 查看 Nginx 版本

有两种可以查看 Nginx 的版本信息的参数。第一种如下:

poechant@ubuntu:/usr/local/nginx$ ./sbin/nginx -vnginx: nginx version: nginx/1.0.0

另一种显示的是详细的版本信息:

poechant@ubuntu:/usr/local/nginx$ ./sbin/nginx -Vnginx: nginx version: nginx/1.0.0nginx: built by gcc 4.3.3 (Ubuntu 4.3.3-5ubuntu4) nginx: TLS SNI support enablednginx: configure arguments: --with-http_ssl_module --with-openssl=/home/luming/openssl-1.0.0d/

6. 检查配置文件是否正确

poechant@ubuntu:/usr/local/nginx$ ./sbin/nginx -tnginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (13: Permission denied)nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok2012/01/09 16:45:09 [emerg] 23898#0: open() "/usr/local/nginx/logs/nginx.pid" failed (13: Permission denied)nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

如果出现如上的提示信息,表示没有访问错误日志文件和进程,可以sudosuper user do)一下:

poerchant@ubuntu:/usr/local/nginx$ sudo ./sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

如果显示如上,则表示配置文件正确。否则,会有相关提示。

7. 显示帮助信息

poechant@ubuntu:/user/local/nginx$ ./sbin/nginx -h

或者:

poechant@ubuntu:/user/local/nginx$ ./sbin/nginx -?

 

 

[root@centos-1-22 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-1-22 sbin]# ./nginx -h
nginx version: nginx/1.0.15
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

 

以上这些涵盖了 Nginx 日常维护的所有基本操作,另外还有向 master 进程发送信号的相关命令,我们会在后续看到。

体验新版博客

ginx 的 HttpUpstreamModule 提供对后端(backend)服务器的简单负载均衡。一个最简单的 upstream 写法如下:


upstream backend {

    server backend1.example.com;

    server backend2.example.com;

    server.backend3.example.com;

}


server {

    location / {

        proxy_pass http://backend;

    }

}


1、后端服务器


通过 upstream 可以设定后端服务器,指定的方式可以是 IP 地址与端口、域名、UNIX 套接字(socket)。其中如果域名可以被解析为多个地址,则这些地址都作为 backend。下面举例说明:


upstream backend {

    server blog.csdn.net/poechant;

    server 145.223.156.89:8090;

    server unix:/tmp/backend3;

}


第一个 backend 是用域名指定的。第二个 backend 是用 IP 和端口号指定的。第三个 backend 是用 UNIX 套接字指定的。


2、负载均衡策略


Nginx 提供轮询(round robin)、用户 IP 哈希(client IP)和指定权重 3 种方式。


默认情况下,Nginx 会为你提供轮询作为负载均衡策略。但是这并不一定能够让你满意。比如,某一时段内的一连串访问都是由同一个用户 Michael 发起的,那么第一次 Michael 的请求可能是 backend2,而下一次是 backend3,然后是 backend1、backend2、backend3…… 在大多数应用场景中,这样并不高效。当然,也正因如此,Nginx 为你提供了一个按照 Michael、Jason、David 等等这些乱七八糟的用户的 IP 来 hash 的方式,这样每个 client 的访问请求都会被甩给同一个后端服务器。(另外,由于最近发现很多网站以不留原文链接的方式盗取本博博文,所以我就在这插一下本博的地址“http://blog.csdn.net/poechant”)具体的使用方式如下:


upstream backend {

    ip_hash;

    server backend1.example.com;

    server backend2.example.com;

    server.backend3.example.com;

}


这种策略中,用于进行 hash 运算的 key,是 client 的 C 类 IP 地址(C 类 IP 地址就是范围在 192.0.0.0 到 223.255.255.255 之间,前三段号码表示子网,第四段号码为本地主机的 IP 地址类别)。这样的方式保证一个 client 每次请求都将到达同一个 backend。当然,如果所 hash 到的 backend 当前不可用,则请求会被转移到其他 backend。


再介绍一个和 ip_hash 配合使用的关键字:down。当某个一个 server 暂时性的宕机(down)时,你可以使用“down”来标示出来,并且这样被标示的 server 就不会接受请求去处理。具体如下:


upstream backend {

    server blog.csdn.net/poechant down;

    server 145.223.156.89:8090;

    server unix:/tmp/backend3;

}


还可以使用指定权重(weight)的方式,如下:


upstream backend {

    server backend1.example.com;

    server 123.321.123.321:456 weight=4;

}


默认情况下 weight 为 1,对于上面的例子,第一个 server 的权重取默认值 1,第二个是 4,所以相当于第一个 server 接收 20% 的请求,第二接收 80% 的。要注意的是 weight 与 ip_hash 是不能同时使用的,原因很简单,他们是不同且彼此冲突的策略。


3、重试策略


可以为每个 backend 指定最大的重试次数,和重试时间间隔。所使用的关键字是 max_fails 和 fail_timeout。如下所示:


upstream backend {

    server backend1.example.comweight=5;

    server 54.244.56.3:8081max_fails=3 fail_timeout=30s;

}


在上例中,最大失败次数为 3,也就是最多进行 3 次尝试,且超时时间为 30秒。max_fails 的默认值为 1fail_timeout 的默认值是 10s。传输失败的情形,由 proxy_next_upstream 或 fastcgi_next_upstream 指定。而且可以使用 proxy_connect_timeout 和 proxy_read_timeout 控制 upstream 响应时间。


有一种情况需要注意,就是 upstream 中只有一个 server 时,max_fails 和 fail_timeout 参数可能不会起作用。导致的问题就是 nginx 只会尝试一次 upstream 请求,如果失败这个请求就被抛弃了 : ( ……解决的方法,比较取巧,就是在 upstream 中将你这个可怜的唯一 server 多写几次,如下:


upstream backend {

    server backend.example.com max_fails fail_timeout=30s;

    server backend.example.com max_fails fail_timeout=30s;

    server backend.example.com max_fails fail_timeout=30s;

}


4、备机策略


从 Nginx 的 0.6.7 版本开始,可以使用“backup”关键字。当所有的非备机(non-backup)都宕机(down)或者繁忙(busy)的时候,就只使用由 backup 标注的备机。必须要注意的是,backup 不能和 ip_hash 关键字一起使用。举例如下:


upstream backend {

    server backend1.example.com;

    server backend2.example.combackup;

    server backend3.example.com;

}


转载请注明来自“柳大的CSDN博客”:http://blog.csdn.net/poechant

体验新版博客




cd nginx-1.0.4

./configure --sbin-path=/usr/local/sbin --without-http-cache --with-http_stub_status_module --with-http_gzip_static_module --with-pcre=/u02/software/nginx/pcre-8.12 --add-module=/u02/software/nginx/nginx_mod_h264_streaming-2.2.7

我安装时候的./configure --prefix=/home/zq/local/nginx --add-module=../nginx_mod_h264_streaming-2.2.7 --sbin-path=/home/zq/local/nginx/sbin --with-debug

之后make,

会报错【ngx_http_streaming_module.c:158: 错误:‘ngx_http_request_t’ 没有名为 ‘zero_in_uri’ 的成员】之类的错误。这需要修改nginx_mod_h264_streaming-2.2.7的源代码:修改ngx_http_streaming_module.c,注释掉

if (r->zero_in_uri)

  {

    return NGX_DECLINED;

  }

这一段。

之后make clean,之后重新configure,重新make,之后make install。

之后执行命令nginx,启动nginx服务器。访问http://ip:80出现Welcome to nginx! 时证明nginx安装成功。

Nginx安装完成之后安装路径在/usr/local/nginx。打开/usr/local/nginx/conf,修改nginx.conf配置文件,添加MP4支持。在server配置中添加如下配置即可

location ~ \.mp4$ {
mp4;
}

测试的话如下:

基本上已经设置完毕,但是此时我们测试的时候还需要一个支持拖拽播放的flash播放器,开源的JW Player就可以实现这样的功能,我将编译的播放器上传上来,供大家下载:

       下载链接:http://blogimg.chinaunix.net/blog/upfile2/100607142612.rar

       下载播放器后,上传到上面设置的/usr/local/nginx/html/flv_file/目录下,闭关把flv视频文件也放到该目录下!

 

5)、启动nginx后测试:

       http://192.168.1.105/player.swf?type=http&file=test1.mp4

             说明: #我的ip是192.168.1.105

                       #player.swf是我的JW Player播放器

                       #http是表示居于http分发方式

                       #test1.mp4是我的flv视频文件

#Nginx





Nginx配置和内核优化 实现突破十万并发

nginx指令中的优化(配置文件)

worker_processes 8;

  nginx进程数,建议按照cpu数目来指定,一般为它的倍数。

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

  为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个,或者将一个进程分配到多个cpu。

worker_rlimit_nofile 102400;

  这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。

use epoll;

  使用epoll的I/O模型

worker_connections 102400;

  每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。

keepalive_timeout 60;

  keepalive超时时间。

client_header_buffer_size 4k;

  客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。

open_file_cache max=102400 inactive=20s;

  这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。

open_file_cache_valid 30s;

  这个是指多长时间检查一次缓存的有效信息。

open_file_cache_min_uses 1;

  open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。
内核参数的优化

net.ipv4.tcp_max_tw_buckets = 6000

  timewait的数量,默认是180000。

net.ipv4.ip_local_port_range = 1024    65000

  允许系统打开的端口范围。

net.ipv4.tcp_tw_recycle = 1

  启用timewait快速回收。

net.ipv4.tcp_tw_reuse = 1

  开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接。

net.ipv4.tcp_syncookies = 1

  开启SYN Cookies,当出现SYN等待队列溢出时,启用cookies来处理。

net.core.somaxconn = 262144

web应用中listen函数的backlog默认会给我们内核参数的net.core.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG默认为511,所以有必要调整这个值。
也可以在nginx配置文件的linsten 80 后面加上这个backlog参数,但是不能超过内核里面设定的数值,如下:

Linsten 80 backlog=65533;
net.core.netdev_max_backlog = 262144

  每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。

net.ipv4.tcp_max_orphans = 262144

  系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤儿连接将即刻被复位并打印出警告信息。这个限制仅仅是为了防止简单的DoS攻击,不能过分依靠它或者人为地减小这个值,更应该增加这个值(如果增加了内存之后)。

net.ipv4.tcp_max_syn_backlog = 262144

  记录的那些尚未收到客户端确认信息的连接请求的最大值。对于有128M内存的系统而言,缺省值是1024,小内存的系统则是128。

net.ipv4.tcp_timestamps = 0

  时间戳可以避免序列号的卷绕。一个1Gbps的链路肯定会遇到以前用过的序列号。时间戳能够让内核接受这种"异常"的数据包。这里需要将其关掉。

net.ipv4.tcp_synack_retries = 1

  为了打开对端的连接,内核需要发送一个SYN并附带一个回应前面一个SYN的ACK。也就是所谓三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送SYN+ACK包的数量。

net.ipv4.tcp_syn_retries = 1

  在内核放弃建立连接之前发送SYN包的数量。

net.ipv4.tcp_fin_timeout = 1

  如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。对端可以出错并永远不关闭连接,甚至意外当机。缺省值是60秒。2.2 内核的通常值是180秒,你可以按这个设置,但要记住的是,即使你的机器是一个轻载的WEB服务器,也有因为大量的死套接字而内存溢出的风险,FIN- WAIT-2的危险性比FIN-WAIT-1要小,因为它最多只能吃掉1.5K内存,但是它们的生存期长些。

net.ipv4.tcp_keepalive_time = 30

  当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时。
一个完整的内核优化配置

net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296net.ipv4.tcp_max_tw_buckets = 6000net.ipv4.tcp_sack = 1net.ipv4.tcp_window_scaling = 1net.ipv4.tcp_rmem = 4096        87380   4194304net.ipv4.tcp_wmem = 4096        16384   4194304net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.core.netdev_max_backlog = 262144net.core.somaxconn = 262144net.ipv4.tcp_max_orphans = 3276800net.ipv4.tcp_max_syn_backlog = 262144net.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_fin_timeout = 1net.ipv4.tcp_keepalive_time = 30net.ipv4.ip_local_port_range = 1024    65000

一个简单的nginx优化配置文件

user  www www;worker_processes 8;worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000;error_log  /www/log/nginx_error.log  crit;pid        /usr/local/nginx/nginx.pid;worker_rlimit_nofile 204800; events{  use epoll;  worker_connections 204800;} http{  include       mime.types;  default_type  application/octet-stream;   charset  utf-8;   server_names_hash_bucket_size 128;  client_header_buffer_size 2k;  large_client_header_buffers 4 4k;  client_max_body_size 8m;   sendfile on;  tcp_nopush     on;   keepalive_timeout 60;   fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2                keys_zone=TEST:10m                inactive=5m;  fastcgi_connect_timeout 300;  fastcgi_send_timeout 300;  fastcgi_read_timeout 300;  fastcgi_buffer_size 16k;  fastcgi_buffers 16 16k;  fastcgi_busy_buffers_size 16k;  fastcgi_temp_file_write_size 16k;  fastcgi_cache TEST;  fastcgi_cache_valid 200 302 1h;  fastcgi_cache_valid 301 1d;  fastcgi_cache_valid any 1m;  fastcgi_cache_min_uses 1;  fastcgi_cache_use_stale error timeout invalid_header http_500;   open_file_cache max=204800 inactive=20s;  open_file_cache_min_uses 1;  open_file_cache_valid 30s;     tcp_nodelay on;   gzip on;  gzip_min_length  1k;  gzip_buffers     4 16k;  gzip_http_version 1.0;  gzip_comp_level 2;  gzip_types       text/plain application/x-javascript text/css application/xml;  gzip_vary on;    server  {    listen       80 backlog=65533;    server_name  www.linuxyan.com;    index index.php index.htm;    root  /www/html/;     location /status    {        stub_status on;    }     location ~ .*\.(php|php5)?$    {        fastcgi_pass 127.0.0.1:9000;        fastcgi_index index.php;        include fcgi.conf;    }     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$    {      expires      30d;    }     log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '              '$status $body_bytes_sent "$http_referer" '              '"$http_user_agent" $http_x_forwarded_for';    access_log  /www/log/access.log  access;      }}

关于FastCGI的几个指令

fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;

  这个指令为FastCGI缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间。

fastcgi_connect_timeout 300;

  指定连接到后端FastCGI的超时时间。

fastcgi_send_timeout 300;

  向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。

fastcgi_read_timeout 300;

  接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。

fastcgi_buffer_size 16k;

  指定读取FastCGI应答第一部分需要用多大的缓冲区,这里可以设置为fastcgi_buffers指令指定的缓冲区大小,上面的指令指定它将使用1个16k的缓冲区去读取应答的第一部分,即应答头,其实这个应答头一般情况下都很小(不会超过1k),但是你如果在fastcgi_buffers指令中指定了缓冲区的大小,那么它也会分配一个fastcgi_buffers指定的缓冲区大小去缓存。

fastcgi_buffers 16 16k;

  指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答,如上所示,如果一个php脚本所产生的页面大小为256k,则会为其分配16个16k的缓冲区来缓存,如果大于256k,增大于256k的部分会缓存到fastcgi_temp指定的路径中,当然这对服务器负载来说是不明智的方案,因为内存中处理数据速度要快于硬盘,通常这个值的设置应该选择一个你的站点中的php脚本所产生的页面大小的中间值,比如你的站点大部分脚本所产生的页面大小为256k就可以把这个值设置为16 16k,或者4 64k 或者64 4k,但很显然,后两种并不是好的设置方法,因为如果产生的页面只有32k,如果用4 64k它会分配1个64k的缓冲区去缓存,而如果使用64 4k它会分配8个4k的缓冲区去缓存,而如果使用16 16k则它会分配2个16k去缓存页面,这样看起来似乎更加合理。

fastcgi_busy_buffers_size 32k;

  这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers的两倍。

fastcgi_temp_file_write_size 32k;

  在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。

fastcgi_cache TEST

  开启FastCGI缓存并且为其制定一个名称。个人感觉开启缓存非常有用,可以有效降低CPU负载,并且防止502错误。但是这个缓存会引起很多问题,因为它缓存的是动态页面。具体使用还需根据自己的需求。

fastcgi_cache_valid 200 302 1h;fastcgi_cache_valid 301 1d;fastcgi_cache_valid any 1m;

  为指定的应答代码指定缓存时间,如上例中将200,302应答缓存一小时,301应答缓存1天,其他为1分钟。

fastcgi_cache_min_uses 1;

  缓存在fastcgi_cache_path指令inactive参数值时间内的最少使用次数,如上例,如果在5分钟内某文件1次也没有被使用,那么这个文件将被移除。

fastcgi_cache_use_stale error timeout invalid_header http_500;

  不知道这个参数的作用,猜想应该是让nginx知道哪些类型的缓存是没用的。 以上为nginx中FastCGI相关参数,另外,FastCGI自身也有一些配置需要进行优化,如果你使用php-fpm来管理FastCGI,可以修改配置文件中的以下值:

<value name="max_children">60</value>

  同时处理的并发请求数,即它将开启最多60个子线程来处理并发连接。

<value name="rlimit_files">102400</value>

  最多打开文件数。

<value name="max_requests">204800</value>

  每个进程在重置之前能够执行的最多请求数。

http://www.linuxyan.com/page/5

 

 

 

 

 

Apache mod_rewrite

Available Languages:  en  | fr  | tr  |  zh-cn 

mod_rewrite provides a way to modify incoming URL requests, dynamically, based onregular expression rules. This allows you to map arbitrary URLs onto your internal URL structure in any way you like.

It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests: server variables, environment variables, HTTP headers, time stamps, external database lookups, and various other external programs or handlers, can be used to achieve granular URL matching.

Rewrite rules can operate on the full URLs, including the path-info and query string portions, and may be used in per-server context (httpd.conf), per-virtualhost context (<VirtualHost> blocks), or per-directory context (.htaccess files and <Directory> blocks). The rewritten result can lead to further rules, internal sub-processing, external request redirection, or proxy passthrough, depending on whatflags you attach to the rules.

Since mod_rewrite is so powerful, it can indeed be rather complex. This document supplements thereference documentation, and attempts to allay some of that complexity, and provide highly annotated examples of common scenarios that you may handle with mod_rewrite. But we also attempt to show you when you should not use mod_rewrite, and use other standard Apache features instead, thus avoiding this unnecessary complexity.

  • mod_rewrite reference documentation
  • Introduction to regular expressions and mod_rewrite
  • Using mod_rewrite for redirection and remapping of URLs
  • Using mod_rewrite to control access
  • Dynamic virtual hosts with mod_rewrite
  • Dynamic proxying with mod_rewrite
  • Using RewriteMap
  • Advanced techniques and tricks
  • When NOT to use mod_rewrite
  • RewriteRule Flags
  • Technical details

Lighttpd 是一个德国人领导的开源Web服务器软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的web server环境。具有非常低的内存开销、cpu占用率低、效能好以及丰富的模块等特点。

Lighttpd是众多OpenSource轻量级的web server中较为优秀的一个。支持FastCGI,CGI,Auth,输出压缩(output compress),URL重写,Alias等重要功能;而Apache之所以流行,很大程度也是因为功能丰富,在lighttpd上很多功能都有相应的实现了,这点对于apache的用户是非常重要的,因为迁移到lighttpd就必须面对这些问题。[2]
 
 

最近把整站从apache升级到了nginx,客户的站点大概有30台服务器大部分架构位tomcat+apache,只有一个php页面
一下是我升级遇到的几个问题的注意点
1.
当我们去访问服务器上的一个目录时候,他不会自动加上一个/ ,浏览器会给出改页无法打开的错误,这个时候浏览器去取的地址实际上是upstream中所写的地址和端口或如果没有使用upstream时 当使用localhost做servername时候 浏览器会去访问http://127.0.0.1/dir。
解决办法
在每个虚拟主机的server定义中加上
if (-d $request_filename) {
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
注意 root字段的定义也一定要出现在server中 如果server中没有定义root 错误还将存在
例子:
server {
listen 800;
server_name www.1.com;
root /opt/1-index; //这边定义了 就会在目录访问的时候加上/ 如果这边没有定义这个 上面的url重写依然不会生效
include vhost/alias.conf;
include vhost/proxy.conf;
if (-d $request_filename) {
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
error_page 405 =200 @405;
location @405 {
proxy_pass http://PROXY_STATIC;
}
location / {
root /opt/1-index; //只在这边定义是没有用的,这边甚至可以不做定义
rewrite ^/(d+).home$ /index.html?userId=$1 last;
index index.html index.htm;
}

2.url重写的注意事项:
原有的url支持正则 重写的url不支持正则
rewrite ^/(d+).home$ /index.html?userId=$1 last;
这个重写中 ^/(d+).home$ 这部分支持正则
而/index.html?userId=$1
不要用正则 也不匹配正则 /index.html?*userId=$1 这样他就会去找.html?*userId=$1这个url 然后给你个404 not found

3.post方式去访问静态文件
Apache、IIS、Nginx等绝大多数web服务器,都不允许静态文件响应POST请求,否则会返回“HTTP/1.1 405 Method not allowed”错误。(但是之前程序在apache上跑 没问题)
如果有这个需求呢 就要做如下配置了
error_page 405 =200 @405;
location @405 {
proxy_pass http://PROXY_STATIC;
# root /usr/local/nginx/html;
}
把所有405错误重定向成200 然后吧所有405错误的请求全部交给一个代理去执行
或者写上本地路径,因为我的路径比较多 所以重定向请求到一台web服务器上了

4.。关于防盗链
1.com的需求是 不是从本来来的请求给除一个403
因为在虚拟主机里配置毫无作用 可能是我们用的是虚拟目录的缘故
所以我们直接对目录做的防盗链
在alias里
别名配置
location /res/ {
alias /opt/Src/;
valid_referers none blocked server_names *.1.com ;
if ($invalid_referer) {
return 403;
}
}

5,关于动态请求转发
location ~ ^/login/(.*.do)$ {
proxy_pass http://login ;
proxy_set_header X-Real-IP $remote_addr;
}
~ ^/login/(.*.do)$ 这个表示 凡是匹配/login/ 下 .do的都转发到一个upstream池里处理 这里的$符号并不起多大作用 只要是有.do的他会全部转 并不是以.do结尾的才转

6.关于php上传文件大小的问题
只改php里的配置是没有用的
需要更改的地方还有nginx的配置

client_max_body_size 10M;
他的默认值是1M;

以上就是基本的注意点

http://www.inginx.com/apache-to-nginx/
 

 

 

 

Rewrite主要的功能就是实现URL的重写,它的正则表达式是基于Perl语言。可基于服务器级的(httpd.conf)和目录级的(.htaccess)两种方式。如果要想用到rewrite模块,必须先安装或加载rewrite模块。方法有两种一种是编译apache的时候就直接安装rewrite模块,别一种是编译apache时以DSO模式安装apache,然后再利用源码和apxs来安装rewrite模块。

基于服务器级的(httpd.conf)有两种方法,一种是在httpd.conf的全局下直接利用RewriteEngine on来打开rewrite功能;另一种是在局部里利用RewriteEngine on来打开rewrite功能,下面将会举例说明,需要注意的是,必须在每个virtualhost里用RewriteEngine on来打开rewrite功能。否则virtualhost里没有RewriteEngine on它里面的规则也不会生效。

基于目录级的(.htaccess),要注意一点那就是必须打开此目录的FollowSymLinks属性且在.htaccess里要声明RewriteEngine on。

目录

 [隐藏] 
  • 1Rewrite语法
  • 2IIS中使用Rewrite
  • 3Rewrite作用
  • 4相关条目
  • 5参考资料

Rewrite语法

RewriteRule

常用Rewrite规则

IIS中使用Rewrite

我们通过在IIS中安装一个名为 ISAPI_Rewrite 的ISAPI筛选器来实现 Rewrite 功能,您需要做的事情只有一个,就是修改配置文件 httpd.ini ,这里举一个简单的例子来说明它的用法。

假设您要实现这样的 Rewrite 功能:您希望当用户访问 /about.htm 的时候实际访问的是 /index.html (您的空间里可以并不需要存在 about.htm)。

那么,设置方法是:

1、创建一个文本文件,内容为

    [ISAPI_Rewrite]  RewriteRule /about\.htm /index.html

这里,RewriteRule 这一行即为规则行,这一行由三部分组成,三部分由空格隔开,第一部分即 RewriteRule 这几个字,第二部分为用户访问的地址(使用正则表达式),第三部分为实际存在于服务器上的文件路径。

2、将上述文件保存,命名为 httpd.ini 。

3、将这个文件上传到您的网站根目录中,对于我们的虚拟主机,即上传至 /web 文件夹中。

这时,当您访问 about.htm 的时候,看到的就是 index.html 的内容。

Rewrite 是一个功能强大的平台,要真正的使用它,您可能要花费相当长的时间来学习。如果您使用像 Discuz! 论坛等支持伪静态的系统,而仅仅是需要使用伪静态功能,那么您可以不必学习,直接复制论坛开发者提供的配置文件即可,但要注意配置文件必须命名为 httpd.ini ,并且这个文件必须放在网站的根下。

httpd.ini 修改或者上传后一般会立即生效,如果长时间不能生效,请登录主机控制面板将网站停止然后再启动。

Rewrite作用

  • 实现网站伪静态
  • 防盗链
  • 网址规范化
  • 实现 301转向 302转向
  • 禁止某些坏访问者
  • 禁止某些URL被访问

相关条目

伪静态网址规范化ApacheMod_rewriteHtaccess

参考资料

  1. http://doc.zzbaike.com/apache/2-2/rewrite/rewrite_guide.html
  2. http://doc.zzbaike.com/apache/2-2/mod/mod_rewrite.html

原创粉丝点击