nginx 配置文件分析以及配置负载均衡器

修改Nginx核心配置文件nginx.conf

# cat /usr/local/nginx/conf/nginx.conf

user  www www;
worker_processes  8;  # 工作进程个数,一般为当前机器总cpu核心数的1到2倍,

error_log  /var/log/nginx_error.log;
#error_log  /var/log/nginx_error.log  notice;
#error_log  /var/log/nginx_error.log  info;

pid        /var/run/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;    # 解除打开文件的资源限制

events {
    use epoll;
    worker_connections  65535;   # 单个进程最大连接数(nginx最大连接数=单个进程连接数*进程数)
}

核心参数设置说明:
worker_processes指明了nginx要开启的进程数,据官方说法,一般开一个就够了,多开几个,可以减少机器io带来的影响。 一般为当前机器总cpu核心数的1到2倍。如,我的机器为双核,那么开4个足够了。
worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。配置完毕后,重启nginx。
worker_connections=65526 单个进程最大连接数,2 的 16 次方是 65526,这是系统端口的极限.
worker_rlimit_nofile 65535; # 解除打开文件的资源限制, 否则会有警告提示。设置了这个后,你修改worker_connections值时,是不能超过worker_rlimit_nofile的这个值。

# 基本设置

server {
    listen       800;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

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

基本参数设置说明:
listen 80 # 可以设置其它端口
server_name localhost www.net.cn 192.168.0.126; # 监听多个域名,域名之间用空格分隔

# 负载均衡基本配置

# 服务器集群
upstream http_cluster_youjia {  # 服务器集群名称
    server 192.168.0.102:92 weight=1 max_fails=2 fail_timeout=60s down;   # 第一台web服务器
    server 192.168.0.103:92 weight=1 max_fails=2 fail_timeout=60s;  # 第二台web服务器
}
server {
    listen       80;
    server_name  youjia.com;
    access_log   logs/youjia.tbkt.cn.log main;
    error_log    logs/youjia.tbkt.cn_error.log;

    location / {
            proxy_cache_key $host$uri$is_args$args;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://http_cluster_youjia;
            #expires      1d;
   }
}

•location / {}:对aspx后缀的进行负载均衡请求,假如我们要对所有的aspx后缀的文件进行负载均衡时,可以这样写:location ~ .*.aspx$ {}
•proxy_pass:请求转向自定义的服务器列表,这里我们将请求都转向标识为http://cuitccol.com的负载均衡服务器列表;
•在负载均衡服务器列表的配置中,weight是权重,可以根据机器配置定义权重(如果某台服务器的硬件配置十分好,可以处理更多的请求,那么可以 为其设置一个比较高的weight;而有一台的服务器的硬件配置比较差,那么可以将前一台的weight配置为weight=2,后一台差的配置为 weight=1)。weigth参数表示权值,权值越高被分配到的几率越大;

#添加Nginx对于静态文件的缓存配置

server {
    listen       80;
    server_name  youjia.com;
    access_log   logs/youjia.tbkt.cn.log main;
    error_log    logs/youjia.tbkt.cn_error.log;

    location = /favicon.ico {
        alias /www/tbkt_web_student/site_media/favicon.ico;
        log_not_found off;  
        expires 30d;   # 过期时效为30天
    }
    location ^~/site_media {
        access_log off;
        alias /www/tbkt_web_student/site_media/;
    }
    location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
      access_log   off; # po co mi logi obrazk¨®w :)
      expires      30d;
    }

    location / {
        uwsgi_connect_timeout 300;
        uwsgi_read_timeout 300;
        uwsgi_send_timeout 300;
        include    uwsgi_params;
        #uwsgi_pass unix:///tmp/uwsgi_www.tbkt.cn.sock;
        uwsgi_pass 127.0.0.1:3333;
    }
}

下面是一段nginx以HTTP形式的反向代理代码

# 编辑nginx.conf文件添加如下代码
upstream http_ziyuan_xueceping_server_pool {
        server 192.168.0.102:92 weight=1 max_fails=2 fail_timeout=60s down;   # 代理到102:92
        server 192.168.0.103:92 weight=1 max_fails=2 fail_timeout=60s;  # 代理到103:92
}


server {
    listen       80;
    server_name  ziyuan.xueceping.cn;
    access_log   logs/www.ziyuan_xueceping93.log main;
    error_log    logs/www.ziyuan_xueceping93_error.log;

    #location / {
    #        # host and port to fastcgi server
    #        #uwsgi_pass 127.0.0.1:6688;  # socket形式
    #        include uwsgi_params;
    #        uwsgi_pass uwsgi_ziyuan_xueceping_server_pool;
    #    }
    location / {
            #proxy_cache cache_one;
            #proxy_cache_valid 200 304 10m;
            #proxy_cache_valid 301 302 1m;
            #proxy_cache_valid any 1m;

            proxy_cache_key $host$uri$is_args$args;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://http_ziyuan_xueceping_server_pool;   # 设置http代理
            #expires      1d;

   }

}

查看安装配置参数/usr/local/nginx/sbin/ngxin -V

[root@iZ25rw6p599Z conf]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.10.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --error-log-path=/var/log/nginx_error.log --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_gzip_static_module --http-log-path=/var/log/nginx_access.log --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --add-module=../ngx_cache_purge-2.3 --with-http_stub_status_module --with-http_flv_module

nginx官网完整的配置文件 https://www.nginx.com/resources/wiki/start/topics/examples/fullexample2/?highlight=worker_processes

.

原文地址:https://www.cnblogs.com/weiok/p/5424414.html