`

Nginx安装部署

阅读更多
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。



一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。
1.选定源码目录
选定目录 /usr/local/

cd /usr/local/

2.安装PCRE库
cd /usr/local/
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
tar -zxvf pcre-8.21.tar.gz
cd pcre-8.21
./configure
make
make install

3.安装zlib库
cd /usr/local/ 
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
./configure
make
make install

4.安装ssl

cd /usr/local/
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
./config
make
make install

5.安装nginx

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:

cd /usr/local/
wget http://nginx.org/download/nginx-1.2.8.tar.gz
tar -zxvf nginx-1.2.8.tar.gz
cd nginx-1.2.8  
./configure --prefix=/usr/local/nginx 
make
make install

--with-pcre=/usr/src/pcre-8.21 指的是pcre-8.21 的源码路径。
--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。

6.启动
确保系统的 80 端口没被其他程序占用,
/usr/local/nginx/sbin/nginx

检查是否启动成功:
netstat -ano|grep 80 有结果输入说明启动成功


打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

7.重启
/usr/local/nginx/sbin/nginx –s reload

8.修改配置文件
cd /usr/local/nginx/conf
vi nginx.conf

9.常用配置
#nginx运行用户和组
user    www www;  
#启动进程,通常设置成和cpu的数量相等
worker_processes  4;

#全局错误日志及PID文件
pid /var/run/nginx.pid;
error_log  /var/log/nginx/error.log;

events {
        #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
use epoll;
                   #单个后台worker process进程的最大并发链接数
        worker_connections  10240;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
        #设定mime类型,类型由mime.type文件定义
        #include       /etc/nginx/mime.types;
        include       mime.types;

        default_type  application/octet-stream;

        #设定日志格式
         access_log    /var/log/nginx/access.log;

        error_page 400 403 500 502 503 504  /50x.html;

        index index.html index.shtml

        autoindex off;

         fastcgi_intercept_errors on;

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

        # These are good default values.
        tcp_nopush      on;

        #连接超时时间
         #keepalive_timeout  0;
        keepalive_timeout  65;
        #tcp_nodelay        on;       
        tcp_nodelay     off;


        # output compression saves bandwidth
        gzip  off;
         #gzip_static on;
        #gzip_min_length  1k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_buffers  4 16k;
        gzip_proxied any;
        gzip_disable "MSIE [1-6]\.";
        gzip_types  text/plain text/html text/css application/x-javascript application/xml application/xml+rss text/javascript;
        #gzip_vary on;

        server_name_in_redirect off;

#设定负载均衡的服务器列表
        upstream portals {
                  server 172.16.68.134:8082 max_fails=2 fail_timeout=30s;
                  server 172.16.68.135:8082 max_fails=2 fail_timeout=30s;
                            server 172.16.68.136:8082 max_fails=2 fail_timeout=30s;
                  server 172.16.68.137:8082 max_fails=2 fail_timeout=30s;
        }

        #upstream overflow {
         #       server 10.248.6.34:8090 max_fails=2 fail_timeout=30s;       
         #       server 10.248.6.45:8080 max_fails=2 fail_timeout=30s;       
        #}

        server {
                                     #侦听8080端口
                listen       8080;
                server_name  127.0.0.1;

                   #403、404页面重定向地址
                   error_page  403 = http://www.e100.cn/ebiz/other/217/403.html;
                   error_page  404 = http://www.e100.cn/ebiz/other/218/404.html;
                   proxy_connect_timeout      90;
                   proxy_send_timeout         180;
                   proxy_read_timeout         180;

                   proxy_buffer_size 64k;
                   proxy_buffers 4 128k;
                   proxy_busy_buffers_size 128k;


                   client_header_buffer_size 16k;
                   large_client_header_buffers 4 64k;

                #proxy_send_timeout         3m;
                #proxy_read_timeout         3m;
                #proxy_buffer_size          4k;
                #proxy_buffers              4 32k;

                proxy_set_header Host $http_host;
                proxy_max_temp_file_size 0;
                #proxy_hide_header Set-Cookie;
                  
         #       if ($host != 'www.e100.cn' ) {
         #                rewrite ^/(.*)$ http://www.e100.cn/$1 permanent;
         #       }


               location / {
                       deny all;
               }

                   location ~ ^/resource/res/img/blue/space.gif {
                    proxy_pass http://tecopera;
               }

               location = / {
                   rewrite ^(.*)$  /ebiz/event/517.html last;
               }



                   location = /ebiz/event/517.html {
                    add_header Vary Accept-Encoding;
                    root /data/web/html;
                    expires 10m;
               }




               location = /check.html {
                    root /usr/local/nginx/html/;
                    access_log off;
               }

               location = /50x.html {
                    root /usr/local/nginx/html/;
                    expires 1m;
                    access_log off;
               }


              location = /index.html {
                       add_header Vary Accept-Encoding;
#定义服务器的默认网站根目录位置
                    root /data/web/html/ebiz;
                    expires 10m;
               }
#定义反向代理访问名称
                   location ~ ^/ecps-portal/* {
                   # expires 10m;
#重定向集群名称
                    proxy_pass http://portals;
                    #proxy_pass http://172.16.68.134:8082;
               }

                   location ~ ^/fetionLogin/* {
                   # expires 10m;
                    proxy_pass http://portals;
                    #proxy_pass http://172.16.68.134:8082;
                }

                   #location  ~ ^/business/* {                                                                     
                #   # expires 10m;                                                                                
                #    proxy_pass http://172.16.68.132:8088;                                                                   
                #    #proxy_pass http://172.16.68.134:8082;                                                       
                #}

                   location ~ ^/rsmanager/* {
                    expires 10m;
                    root /data/web/;
                    #proxy_pass http://rsm;
               }
#定义nginx处理的页面后缀
                   location ~* (.*)\.(jpg|gif|htm|html|png|js|css)$  {
                            root /data/web/html/;
#页面缓存时间为10分钟
                         expires 10m;
                   }

#设定查看Nginx状态的地址     
               location ~* ^/NginxStatus/ {
                    stub_status on;
                    access_log off;
                    allow 10.1.252.126;
                    allow 10.248.6.49;
                    allow 127.0.0.1;
                    deny all;
               }
         #       error_page   405 =200 @405;
         #       location @405
         #       {
         #                proxy_pass http://10.248.6.45:8080;
         #       }  

               access_log  /data/logs/nginx/access.log combined;
               error_log   /data/logs/nginx/error.log;
        }
         server {
                listen       8082;

                server_name  _;
               location = /check.html {
                    root /usr/local/nginx/html/;
                    access_log off;
               }
                  
        }
         server {
                   listen       8088;
                   server_name  _;
                   location ~ ^/* {
                   root /data/web/b2bhtml/;
                   access_log off;
         }                
         }
        server {
                listen       9082;
                server_name  _;

        #        location ~ ^/resource/* {
        #            expires 10m;
         #           root /data/web/html/;
         #       }

                location  / {
                     root /data/web/html/sysMaintain/;
                       if (!-f $request_filename) {
                            rewrite ^/(.*)$ /sysMaintain.html last;
                           }
                }
        }

}
10.Nginx配置文件详细说明

如果要使用负载均衡的话,可以修改配置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服务器传

       }

     }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics