IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    nginx及其配置文件介绍

    haoflynet发表于 2015-07-17 01:52:34
    love 0

    QQ图片20150716103016

    Nginx用起来比Apache方便简介,也有很多超过Apache的地方。Nginx不仅可以作为http服务器来用,更重要的,它还可以用来做负载均衡和反向代理。

    配置文件详解

    nginx配置文件就是/etc/nginx/nginx.conf

    nginx配置文件里最重要的是http设置,其中包含了四个部分:全局设置、主机设置(server)、上游服务器设置(upstream)、URL设置(特定的URL使用特定的方法)。例如:

    user www-data;           # nginx所属用户
    worker_processes 4;      # 进程数,通常是和CPU数量相等
    pid /var/run/nginx.pid;
    
    events {
            worker_connections 768;   # 单个进程并发的最大连接数
            # multi_accept on;
    }
    
    http {
    
            ##
            # Basic Settings
            ##
    
            sendfile on;  # 开启高效文件传输模式,如果是高IO的应用可设置为off
            tcp_nopush on;
            tcp_nodelay on;
            # server_tokens off;
    
            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;
    
            include /etc/nginx/mime.types;
            default_type application/octet-stream;
    
            ##
            # Logging Settings
            ##
    
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
    
            ##
            # Gzip Settings   Gzip压缩功能,可减少网络传输
            ##
    
            gzip on;
            gzip_disable "msie6";
    
            # gzip_vary on;
            # gzip_proxied any;
            # gzip_comp_level 6;
            # gzip_buffers 16 8k;
            # gzip_http_version 1.1;
    
            ##
            # nginx-naxsi config
            ##
            # Uncomment it if you installed nginx-naxsi
            ##
    
            #include /etc/nginx/naxsi_core.rules;
    
            ##
            # nginx-passenger config
            ##
            # Uncomment it if you installed nginx-passenger
            ##
    
            #passenger_root /usr;
            #passenger_ruby /usr/bin/ruby;
    
            ##
            # Virtual Host Configs
            ##
    
            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-enabled/*;
    
            # 设置负载均衡后台服务器列表,命名为name
            # nginx有三种负载均衡策略:round-robin(轮询)、least-connected(选择连接数最小的那个服务器)、ip-hash(使用IP的hash值来进行选择)、weight-load(根据权值)
            upstream name{        # 设置负载均衡后台服务器列表,命名为name
                    server 192.168.0.1:8000;
                    server 192.168.0.1:8001; 
            }
            
            server {      # 设置虚拟主机
                    listen 80;
                    server_name haofly.net;
                    root /var/www/haofly;
          
                    charset utf-8
                    access_log /var/log/nginx/80_access.log;
                    error_log /var/log/nginx/80_error.log;
    
                    location / {    # 对/下面的所有url做负载均衡
                            root /var/www/haofly;
                            index index.html;
    
                            allow 192.168.0.10;
                            allow 192.168.1.0/24;
                            deny all;   # 可设置多个allow和deny,表示允许或禁止某个IP或IP段访问
    
                            proxy_pass http://name;  # 上面设置的负载均衡服务器列表的名字
                            proxy_connect_timeout 60; # nginx到后台服务器连接超时时间
                            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;
                    }
    
                    location /static {    # 静态文件由nginx自己处理
                        root /var/www/haofly
                    }
            }
    }
    
    
    #mail {
    #       # See sample authentication script at:
    #       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
    #
    #       # auth_http localhost/auth.php;
    #       # pop3_capabilities "TOP" "USER";
    #       # imap_capabilities "IMAP4rev1" "UIDPLUS";
    #
    #       server {
    #               listen     localhost:110;
    #               protocol   pop3;
    #               proxy      on;
    #       }
    #
    #       server {
    #               listen     localhost:143;
    #               protocol   imap;
    #               proxy      on;
    #       }
    #}

     

    查看负载均衡状态

    nginx提供了默认的模块可以查看负载均衡的统计信息等,只需要在某个server里面添加:

     

    location /nginx {    
        stub_status on;  
        access_log   on;  
        auth_basic   "NginxStatus";  
        auth_basic_user_file   /etc/nginx/htpasswd;  
    }

    其中htpasswd是保存用户名和密码的文件,可以自定义位置,每一行一个用户username:password这样子保存的,使用crypt3(BASE64)加密,可以用一个PHP文件来生成

    $password = 'hehe'; 
    $password = crypt($password, base64_encode($password)); 
    echo $password;

    最后访问 http://…../nginx 即可看到如下的负载均衡信息:

    Active connections: 1 
    server accepts handled requests
     2 2 37 
    Reading: 0 Writing: 1 Waiting: 0



沪ICP备19023445号-2号
友情链接