nginx的相关知识

          nginx是通过软件来实现负载均衡,其外部应用都是请求到nginx的前端,其前端与后端进行交互将其结果发送给用户。nginx可以进行很多配置,如对静态内容进行缓存等 ,这里只是写个简单的后面再进补充。

         nginx的启动命令是 /usr/local/nignx/sbin/nginx   (其nginx是安装在/usr/local下面)

          进行重启是 kill -TERM '主进程号'

         当修改了/usr/local/nignx/conf/nginx.conf中的文件后,检查其配置文件是否错误运行以下命令:

          /usr/local/nginx/sbin/nginx -t   

             其配置文件是在/usr/local/nignx/conf/nginx.conf里面,下面简单说下其配置文件

             #使用的用户和组

             user  root root;

             #指定工作衍生进程数(一般等于cpu的总核数或总核数的两倍,如两个cpu是四核的 ,则总核数为8)
             worker_processes  8;

             events {

             #指定网络的io模型,linux系统推荐采用epoll模型,FreeBSD推荐采用kqueque模型
             use epoll;

              #允许的连接数
             worker_connections  65533;
           }

            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;
                 #keepalive_timeout  0;
                 keepalive_timeout  65;

                 #客户端支持gzip压缩
                gzip  on;


              ##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##

               #此处配置实际处理请求的服务和端口
               upstream  ifservice {
                server   127.0.0.1:8081;
                server   127.0.0.1:8080;
              }
                
            server {

          #nginx监听的端口
          listen       80;

           #监听的域名
           server_name  localhost;
        #charset koi8-r;

         #指定日志的输出路径
        access_log  logs/nginx_access.log  main;

        #缓存文件由nginx进行处理,不用从后端进行请求
        location ~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {
              proxy_pass http://ifservice  ;
              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 / {
        #    root   html;
        #    index  index.html index.htm;

          #代为转发的地址
            proxy_pass   http://ifservice;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP  $remote_addr;
        }


        #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }




    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}




    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;


    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_timeout  5m;


    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}

    缓存配置说明

     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天

     下面简单讲解下nginx的安装

       首先去官网下载稳定版本的nginx包,另外去 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,另外需要下载zlib-1.2.5.tar.gz包使用下面命令下载编译和安装 PCRE 包: 

                                      tar zxvf pcre-8.20.tar.gz   进行解压pcre包

                                       tar zxvf  zlib-1.2.5.tar.gz   

                                    以上两个包只用解压就行,不用安装,因为编译时只要指向其依赖的库就行。

                                      tar zxvf nginx-1.2.0.tar.gz   解压nginx包

                                    进入到解压的nginx目录下

                                  ./configure --with-http_stub_status_module --prefix=/usr/local/nginx/ --with-pcre=/root/pcre-8.20 --with-zlib=/root/zlib-1.2.5

                              其中with-http_stub_status_module为了启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当前状态。-prefix是安装目录默认是在usr/local/nginx/,后面的--with-pcre是指pcre路径,这里只用解压就行,然后时行指定,如果安装pcre可能会报错。

                                  然后执行make

                                  最后是make install

                                    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值