Nginx研究记录

Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用。  其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、腾讯,另外知名的微网志Plurk也使用nginx

                                                                                                                                   ----摘自开源中国社区

下面是从安装,到配置的整个过程,并对其中的一些参数进行粗浅的解释

1、下载安装

     先去http://nginx.org/en/download.html去下载源代码包nginx-1.1.17.tar.gz。
当前版本为nginx-1.1.17
# tar xvfz nginx-1.1.17.tar.gz
# cd nginx-1.1.17
    在目录里执行configure操作,该操作是为了生成makefile文件的,所以有一些参数需要设置
#./configure --prefix=/usr/local/nginx --with-http_gzip_static_module [--with-debug] [--with-http_image_filter_module]
# make
# sudo make install
安装完成后,进入/usr/local目录,更改nginx目录的owner和grouper,这里可以根据自身需要进行更改,可以是新建一个用户专门用来执行nginx,如:
# groupadd  -g  100  nginx
# useradd   -u  100  -g  100  nginx

或者直接使用目前的用户和用户组。

# sudo chown {owner}:{group} nginx –R

至此,安装过程已经基本结束。

2、配置squid

2.1基本配置

#基本配置
#设置user
#user  nobody; 
#指定worker进程数,可以根据cpu个数设置
worker_processes  8;
#指定log文件位置和错误日志记录级别(debug|info|notice|warn|error),下面是默认路径
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid文件路径,下面是默认路径
#pid        logs/nginx.pid;

#事件设置
events {
    #使用的网络I/O模型,Linux系统推荐采用epoll模型,FreeBSD系统推荐使用kqueue模型
    use epoll;
    #单个工作进程的最大连接数
    worker_connections  51200;
}   

2.2正向代理配置:

http {
    include       mime.types;
  default_type  application/octet-stream;
  #设置使用的字符集
  #charset gb2312;
  # servername的哈希表大小
  server_names_hash_bucket_size 128;
  #设置请求头的buffer大小
  client_header_buffer_size 32k;
  #当上面的buffer不能存放header时,下面指定一个更大的buffer大小来存放
  large_client_header_buffers  4 32k;
  #log格式定义
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
  #log存放路径和使用的格式
  access_log  logs/access.log  main;
  #将sendfile()置为可用/不可用
  sendfile        on;
  #允许/禁止FreeBSD上的TCP_NOPUSH或者Linux的TCP_CORK。只有在sendfile可用的情况下可用
  tcp_nopush     on;
  #keep-alive链接超时时间设置,在超过该时限,服务器将关闭与客户端的链接
  #keepalive_timeout  0;
  keepalive_timeout  65;
  #允许/禁止socket的TCP_NODELAY选项
  tcp_nodelay on;
  
  #gzip压缩
  gzip  on;
  #设置被压缩的最小请求页面,单位是字节,低于这个大小,将不会被压缩。这个大小主要由http协议中的 “Content-Length”决定的。
  gzip_min_length  1k;
  #设置gzip使用的内存
  gzip_buffers  4  16k;
  #设置gzip压缩的级别,一共有9个级别。1是最小程度的压缩,9是最大程度的压缩。
  gzip_comp_level  2;
  #为除“text/html”之外的MIME类型启用压缩,“text/html”总是会被压缩。
  gzip_ types text/plain application/x-javascript text/css application/xml;
  #启用应答头“Vary: Accept-Encoding”,注意,由于一个bug将导致IE 4-6无法缓存内容。
  gzip_vary  on;
  #server设置
  server {
        #配置DNS,设置超时时间
     resolver 8.8.8.8;
     resolver_timeout 5s;
        #设置监听端口
       listen       8080;
     #location设置
     #对jpg图片的处理
     location ~ .*\.(jpeg|jpg)$ {    
          proxy_set_header Host $http_host;
        proxy_set_header X-Real_IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http://backserver;
         image_filter resize 320 -;
         image_filter_jpeg_quality 20;
         #error_page      502  = @fallback;
      }
      location / {
       #设置http头部的host
          proxy_set_header Host $http_host;
           #设置真实的客户端ip
          proxy_set_header X-Real_IP $remote_addr;
            #设置x-forward
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #及其重要的!不改变请求url的任何参数
          proxy_pass $scheme://$http_host$request_uri;
            #配置缓存大小,减少I/O操作
          proxy_buffers 256 4k;
            #关闭磁盘缓存
          proxy_max_temp_file_size 0;
            #设置代理链接超时时间
          proxy_connect_timeout 30;
            对不同cache的缓存时间
          proxy_cache_valid 200 302 10m;
          proxy_cache_valid 301 1h;
          proxy_cache_valid any 1m;
    }
  }
}

2.3反向代理及负载均衡配置:

http {
    include       mime.types;
default_type  application/octet-stream;
# servername的哈希表大小
server_names_hash_bucket_size 128;
#设置请求头的buffer大小
client_header_buffer_size 32k;
#当上面的buffer不能存放header时,下面指定一个更大的buffer大小来存放
large_client_header_buffers  4 32k;
#log格式定义
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
#log存放路径和使用的格式
access_log  logs/access.log  main;
#反向代理配置
include    proxy.conf;
#fastcgi配置
include    fastcgi.conf;
#将sendfile()置为可用/不可用
sendfile        on;
#允许/禁止FreeBSD上的TCP_NOPUSH或者Linux的TCP_CORK。只有在sendfile可用的情况下可用
tcp_nopush     on;
#设置负载均衡服务器的IP,端口和权重等
upstream backServer{
        server 192.168.0.2:8000  weight=2 max_fails=2 fails_timeout=30s;
        server 192.168.0.3:8000  weight=3;
        server 192.168.0.4:8000  backup;
        server 192.168.0.5:8000;
server 192.168.0.6:8000  down;
}
#server设置
server {
        #设置监听端口
    listen       8080;
        #域名,可以配置多个
        server_name   xxx.com www.yyy.com;
        #访问日志,和上面的级别不一样,覆盖上级日志
        access_log  logs/access.log  main;
        #location设置
        #静态文件,nginx自己处理
        location ~^(images|javascript|js|css|flash|media|static)/ {
            #设置文件存放的路径
            root /var/www/…/;
            #过期时间设置
            expires 30d;
        }
        #把请求转发给后端webServer,反向代理和fastcgi的区别是:反向代理后面是webServer,fastcgi后台是fastcgi监听进程,协议不一样;
        location / {
            #设置转发服务器IP和端口,这里也可以进行负载均衡设置
            proxy_pass  http://127.0.0.1:8090;
            #负载均衡,必须与上面upstream同时使用
            #proxy_pass  http://backServer;
        }
}
}

2.4通过hash url负载均衡配置:

需要安装第三方模块ngx_http_upstream_hash_module。

cd nginx-1.1.17
patch -p0 < /path/nginx.patch
./configure时加上参数
--add-module=path/
make; sudo make install

在配置文件的upstream中添加

hash $request_uri;   #也可以hash其它字段,如$http_host,$http_host$request_uri等

另:

如果后端某台squid服务器宕机,可以采取指定到备用服务器或者再次对备份squid服务器进行hash处理。

upstream backup {
        server 10.108.76.61:3128;
    server 10.108.76.62:3128;
        hash $http_host;
}
location / {
#增加对502错误的处理
error_page      502  = @fallback;
}

location @fallback {
        proxy_pass http://backup;
}

前面包含的文件

fastcgi.conf

# fastcgi.conf  

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;  

fastcgi_param  QUERY_STRING       $query_string;  

fastcgi_param  REQUEST_METHOD     $request_method;  

fastcgi_param  CONTENT_TYPE       $content_type;  

fastcgi_param  CONTENT_LENGTH     $content_length;  

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;  

fastcgi_param  REQUEST_URI        $request_uri;  

fastcgi_param  DOCUMENT_URI       $document_uri;  

fastcgi_param  DOCUMENT_ROOT      $document_root;  

fastcgi_param  SERVER_PROTOCOL    $server_protocol;  

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;  

fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;  

fastcgi_param  REMOTE_ADDR        $remote_addr;  

fastcgi_param  REMOTE_PORT        $remote_port;  

fastcgi_param  SERVER_ADDR        $server_addr;  

fastcgi_param  SERVER_PORT        $server_port;  

fastcgi_param  SERVER_NAME        $server_name;  

fastcgi_index  index.php; 

# PHP only, required if PHP was built with --enable-force-cgi-redirect  

fastcgi_param  REDIRECT_STATUS    200;

proxy.conf

# proxy.conf  

proxy_redirect          off;  

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;  

proxy_send_timeout      90;  

proxy_read_timeout      90;  

proxy_buffers           32 4k; 

mine.types

# mime.types   
types {   
    text/html                             html htm shtml;   
    text/css                              css;   
    text/xml                              xml rss;   
    image/gif                             gif;   
    image/jpeg                            jpeg jpg;   
    application/x-javascript              js;   
    text/plain                            txt;   
    text/x-component                      htc;   
    text/mathml                           mml;   
    image/png                             png;   
    image/x-icon                          ico;   
    image/x-jng                           jng;   
    image/vnd.wap.wbmp                    wbmp;   
    application/java-archive              jar war ear;   
    application/mac-binhex40              hqx;   
    application/pdf                       pdf;   
    application/x-cocoa                   cco;   
    application/x-java-archive-diff       jardiff;   
    application/x-java-jnlp-file          jnlp;   
    application/x-makeself                run;   
    application/x-perl                    pl pm;   
    application/x-pilot                   prc pdb;   
    application/x-rar-compressed          rar;   
    application/x-redhat-package-manager  rpm;   
    application/x-sea                     sea;   
    application/x-shockwave-flash         swf;   
    application/x-stuffit                 sit;   
    application/x-tcl                     tcl tk;   
    application/x-x509-ca-cert            der pem crt;   
    application/x-xpinstall               xpi;   
    application/zip                       zip;   
    application/octet-stream              deb;   
    application/octet-stream              bin exe dll;   
    application/octet-stream              dmg;   
    application/octet-stream              eot;   
    application/octet-stream              iso img;   
    application/octet-stream              msi msp msm;   
    audio/mpeg                            mp3;   
    audio/x-realaudio                     ra;   
    video/mpeg                            mpeg mpg;   
    video/quicktime                       mov;   
    video/x-flv                           flv;   
    video/x-msvideo                       avi;   
    video/x-ms-wmv                        wmv;   
    video/x-ms-asf                        asx asf;   
    video/x-mng                           mng;   
}
原文地址:https://www.cnblogs.com/geekma/p/2612951.html