【安装与配置】--Nginx

来源:互联网 发布:淘宝吉光片羽家怎么样 编辑:程序博客网 时间:2024/05/01 21:55

#安装pcre 

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
[root@svr3 ~]# tar -xjf pcre-8.02.tar.bz2 -C/usr/src/ 

[root@svr3 ~]# cd /usr/src/pcre-8.02/ 
[root@svr3 pcre-8.02]# ./configure &&make && make install  


#安装nginx 

http://nginx.org/en/download.html
[root@svr3 ~]# useradd -M -s /sbin/nologinnginx 

[root@svr3 ~]# tar -xzf nginx-1.0.5.tar.gz -C/usr/src/ 
[root@svr3 ~]# cd /usr/src/nginx-1.0.5/ 
[root@svr3 nginx-1.0.5]# ./configure--prefix=/usr/local/nginx --user=nginx --group=nginx  
[root@svr3 nginx-1.0.5]# make && makeinstall  

 

报错:

libtool: compile: unrecognized option `-DHAVE_CONFIG_H'

libtool: compile: Try `libtool --help' for more information.

make[1]: *** [pcrecpp.lo] Error 1

解决: yum install gcc-c++

 

报错:

./configure: error: the HTTP gzip module requires the zliblibrary.

解决下载zlib;


#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的性能

    worker_connections  1024;#单个后台worker process进程的最大并发链接数

    # multi_accept on;

}

 

#设定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:3128weight=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.htmlindex.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_paramSCRIPT_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负载均衡

nginx负载均衡需重新编译nginx 添加upstream_hash模块

为了管理方便 ,将使用3个文件来配置

nginx.conf

普通nginx.conf 配置即可,以下为示例

#user  nobody;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    use epoll;
    worker_connections  102400;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;


    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;
    keepalive_timeout  65;
    
    client_header_buffer_size 4k;
    open_file_cache max=102400 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;


    client_max_body_size 64m;
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css application/xml application/json;
    gzip_vary on;


    ##cache##
    proxy_connect_timeout 5;
    proxy_read_timeout 60;
    proxy_send_timeout 5;
    proxy_buffering on;
    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##


    include vhosts/upstream.conf;  #负载均衡服务器url与ip映射文件
    include vhosts/service1.conf; # service1的配置文件  


}

vhosts/upstream.conf

负载均衡服务器url与ip映射文件
在nginx.conf目录下创建vhost目录(来保存负载均衡服务器的配置文件)
nginx 的 upstream目前支持 4 种方式的分配 
1)、轮询(默认) 
      每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 
2)、weight 
      指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 
2)、ip_hash 
      每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。  
3)、fair(第三方) 
      按后端服务器的响应时间来分配请求,响应时间短的优先分配。  
4)、url_hash(第三方)

upstreamservice1{   #service名字 与 具体的server配置文件中的proxy_pass 对应
server 192.168.0.221:8086; #service对应的ip+端口
server 192.168.0.222:8086; 
 ip_hash;
}


vhosts/service1.conf

service1的配置文件  

server {
        listen       80;
        server_name  myservice.cn ;

        location / {
           #index  index.html index.htm;
 
proxy_pass http://service1; #必须与upstream 中的 service名字对应
           proxy_set_header Host $host:$server_port;
           #proxy_set_header SERVER_PORT $server_port;
           proxy_set_header   X-Real-IP        $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

}

Nginx常用命令

1. 启动 Nginx
root@localhost:sudo ./sbin/nginx
2. 停止 Nginx
root@localhost:sudo ./sbin/nginx -s stop
root@localhost:sudo ./sbin/nginx -s quit
-s都是采用向 Nginx 发送信号的方式。


3. Nginx 重载配置
root@localhost:sudo ./sbin/nginx -s reload
上述是采用向 Nginx 发送信号的方式,或者使用:


root@localhost:service nginx reload
4. 指定配置文件
root@localhost:sudo ./sbin/nginx -c /usr/local/nginx/conf/nginx.conf
-c表示configuration,指定配置文件。


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


root@localhost:/usr/local/nginx$ ./sbin/nginx -v
nginx: nginx version: nginx/1.0.0
另一种显示的是详细的版本信息:


root@localhost:/usr/local/nginx$ ./sbin/nginx -V
nginx: nginx version: nginx/1.0.0
nginx: built by gcc 4.3.3 (Ubuntu 4.3.3-5ubuntu4) 
nginx: TLS SNI support enabled
nginx: configure arguments: --with-http_ssl_module --with-openssl=/home/luming/openssl-1.0.0d/
6. 检查配置文件是否正确
root@localhost:/usr/local/nginx$ ./sbin/nginx -t
nginx: [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 ok
2012/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
如果出现如上的提示信息,表示没有访问错误日志文件和进程,可以sudo(super user do)一下:


poerchant@ubuntu:/usr/local/nginx$ sudo ./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
如果显示如上,则表示配置文件正确。否则,会有相关提示。


7. 显示帮助信息
root@localhost:/user/local/nginx$ ./sbin/nginx -h
或者:


root@localhost:/user/local/nginx$ ./sbin/nginx -?


原创粉丝点击