lichaoyue888 发表于 2018-11-10 13:46:05

Nginx之二:负载均衡及高可用

upstream webserver {  
server 192.168.10.2;#默认80端口
  
server 192.168.10.3;
  
}
  
------nginx.conf配置如下:
  
usernginx;   #运行用户
  
worker_processes2;   #启动进程数
  
error_log/var/log/nginx/error.log warn;
  
pid      /var/run/nginx.pid;
  
events {
  
    worker_connections5000;#工作线程连接数5000
  
}
  
http {
  
    include       /etc/nginx/mime.types;
  
    default_typeapplication/octet-stream;
  
    log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  
                      '$status $body_bytes_sent "$http_referer" '
  
                      '"$http_user_agent" "$http_x_forwarded_for"';
  
    access_log/var/log/nginx/access.logmain;
  
    sendfile      on;
  
    #tcp_nopush   on;
  
    keepalive_timeout65;
  
    #gzipon;
  
    upstream webserver {
  
server 192.168.1.102;
  
    server 192.168.1.200;
  
}
  
    include /etc/nginx/conf.d/*.conf;
  
}
  
    在/etc/nginx/conf.d/default.conf在location / 中添加proxy_pass请求全部代理到刚才定义好webserver
  
location / {
  
proxy_pass http://webserver;
  
root html;
  
index index.html index.htm;
  
}
  
-----------default.conf配置如下:-------------------------------------------------
  
server {
  
    listen       80;
  
    server_namelocalhost;
  
    #charset koi8-r;
  
    #access_log/var/log/nginx/log/host.access.logmain;
  
    #location / {
  
    #    root   /usr/share/nginx/html;
  
    #    indexindex.html index.htm;
  
    #}
  
    location / {
  
    proxy_pass http://webserver;
  
    root html;
  
    index index.html index.htm;
  
    }
  
    #error_page404            /404.html;
  
    # redirect server error pages to the static page /50x.html
  
    #
  
    error_page   500 502 503 504/50x.html;
  
    location = /50x.html {
  
      root   /usr/share/nginx/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_indexindex.php;
  
    #    fastcgi_paramSCRIPT_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 {
  
    #    denyall;
  
    #}
  
}
  
# service nginx configtest#测试配置文件
  
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  
nginx: 5000 worker_connections exceed open file resource limit: 1024
  
nginx: configuration file /etc/nginx/nginx.conf test is successful


页: [1]
查看完整版本: Nginx之二:负载均衡及高可用