码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx自学手册(一) upstream及fastcgi

时间:2017-04-12 05:13:41      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:include   服务器   events   决战   舞蹈   

(一)nginx upstream实现负载均衡

   Ngx_http_upstream_module模块可实现七层负载均衡,定义的服务器组可被proxy_pass、fastcgi_pass、uwsgi_pass、scgi_pass和memcached_pass所引用,具体可以参考官方文档:

http://nginx.org/en/docs/http/ngx_http_upstream_module.html

 

  1,当前的环境:

Nginx upstream IP:192.168.180.2

node1 : 192.168.180.2:8080

node2 : 192.168.180.3:8080

目前两台主机的服务都已经再跑如图:

技术分享

技术分享

 2,nginx upstream配置

   (1)只定义http段的定义服务器组

user  ftp;
worker_processes  3;
worker_rlimit_nofile 65535;
events {
    use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #include       proxy.conf;
    log_format yundns_log ‘$server_name $remote_addr [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"‘;
    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;
    access_log on;
    server_tokens off;
    sendfile        on;
    tcp_nopush     on;
    server_names_hash_bucket_size 256;
    client_header_buffer_size 256k;
    #large_client_header_buffers 4 32k;
    large_client_header_buffers 4 256k;
    client_body_buffer_size 256k;
    client_header_timeout     3m;
    client_body_timeout 3m;
    send_timeout             3m;
    client_max_body_size 50m;
    keepalive_timeout  120;
    fastcgi_intercept_errors on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    #fastcgi_buffer_size 64k;
    #fastcgi_buffers 8 64k;
    #fastcgi_busy_buffers_size 128k;
    #fastcgi_temp_file_write_size 128k;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    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;
    #limit_req_zone $anti_SendSms zone=anti_SendSms:30m rate=1r/m;
    limit_req_zone $binary_remote_addr zone=anti_SendSms:30m rate=1r/m;
     upstream guojinbao{
      ip_hash;
      server 192.168.180.2:8080;
      server 192.168.180.3:8080;
       }
    include server/*.conf;
}

    (2)定义虚拟主机段location区段的proxy_pass中服务器组

server {
        listen       443 ssl http2;
        server_name  localhost;
        #ssl                  on;
        ssl_certificate      guojinbao.geotrust.crt;
        ssl_certificate_key  guojinbao.geotrust.key;
        rewrite ^/invitejoin/(.*)\.htm[l]?$  /register.shtml?$1 last;
        index index.jsp index.html;
        root /opt/static/home;                            
  location ~ .* {
                                        proxy_pass http://guojinbao;
                                        proxy_set_header X-Real-IP $remote_addr;
                                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                                        #proxy_set_header X-Forward-For $remote_addr;
                                        proxy_set_header Host $host;
                                }
        }


3,测试:

把其中一台服务关闭先把192.168.180.2:8080服务关闭,看下能不能调到192.168.180.3这台服务器上

如下图:

技术分享

技术分享

可以正常访问说明服务已调到192.168.180.3这台服务器上,负载均衡已起作用。

备注:主要参数说明:

 server 192.168.0.30 weight=2;此处的weight为权重,默认为1;

 主要算法,rr,wrr,ip_hash等,可在upstream中定义;

 server 192.168.0.30 max_files=3 fail_timeout=30s;此处的max_files,fail_timeout为健康状态检测,超过3次未响应,超时30s,就从server列表中移除。

 server 192.168.0.30 backup;标记为备份

 server 192.168.0.30 down;标记为不可用,与ip_hash算法一同使用



二、Nginx fastcgi

 1、fastcgi全称为高速的通用网关接口,是HTTP服务器与动态脚本语言之间通信的接口,其工作模式与反向代理差不多,实现客户端请求的动静分离。


2 配置fastcfi_params

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
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_FILENAME    $document_root$fastcgi_script_name;
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  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;
    server {
         listen       80;
         server_name  192.168.110.4;       ################域名可以有多个,用空格隔
         access_log  /data/nginx/log/nginx.access.log;
        index index.php  index.html;
       # root      /usr/local/nginx/html;
         root    /data/php;
       # root    /data/test/php;
       # root    /data/www/php;
         error_page 500 502 503 /50x.html;                                                                   
         location = /nginx-status {                                                                          
                     stub_status on;                                                                         
                     access_log  off;                                                                        
                     allow 127.0.0.1;                                                                        
                     allow 192.168.180.4;                                                                    
                                  }             
     location ~ \.php$                                                                                       
         {                                                                                                   
            # root   /usr/local/nginx/html/zabbix;                                                           
             expires -1s;                                                                                    
             #index index.php  index.html;                                                                   
             fastcgi_split_path_info ^(.+\.php)(/.+)$;                                                       
             include fastcgi_params;                                                                         
             fastcgi_param PATH_INFO $fastcgi_path_info;                                                     
             fastcgi_index index.php;                                                                        
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;                               
             fastcgi_pass 127.0.0.1:9000;                                                                    
                                                                                                             
           }

本文出自 “清风明月” 博客,请务必保留此出处http://liqingbiao.blog.51cto.com/3044896/1914951

Nginx自学手册(一) upstream及fastcgi

标签:include   服务器   events   决战   舞蹈   

原文地址:http://liqingbiao.blog.51cto.com/3044896/1914951

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!