nginx负载均衡

一.nginx代理服务器部署

   1.在nginx代理服务器上装上nginx软件(下载地址:http://nginx.org/en/download.html

   2.在linux系统中下载nginx安装包

      切换目录:cd /usr/local

     执行如下命令:wget  http://nginx.org/download/nginx-1.10.3.tar.gz

   3.解压nginx压缩包并重命名

    tar -zxvf nginx-1.10.3.tar.gz       ps:解压缩包

    rm nginx-1.10.3.tar.gz               ps:删除下载的压缩包

    mv nginx-1.10.3.tar.gz nginx     ps:重命名

   4.进入nginx目录

    编译安装nginx

    执行命令:.cd nginx                                                ps:切换到nginx软件目录

                      ./configure --prefix=/usr/local/nginx       ps:配置nginx软件

                      make                                                     ps:编译nginx软件

                      make install                                           ps:安装nginx软件

    5.新建日志目录及文件

      在nginx目录下新建logs

      mkdir logs      新建logs目录

      cd logs

      vi error.log    新建error.log文件

      vi access.log  新建access.log文件

   6.修改nginx/conf下面的nginx.conf文件配置代理,及负载均衡配置

#user nobody;
worker_processes 1; #工作进程数 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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 on; #配置负载均衡 域名localhost后面的也要保持一致 #通过ip哈希来实现负载均衡 upstream localhost { #ip_hash server 192.168.33.11 weight=2; #敷在服务器1 weight权重越大访问的几率越高 server 192.168.33.12:80;#敷在服务器2 可以指定端口 server 192.168.33.13;#敷在服务器3 } server { listen 80; server_name localhost; #与upstream后面的保持一致 #charset koi8-r; #access_log logs/host.access.log main; location / { #设置主机头和客户端真实地址,以便服务器获取客户端真实IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #禁止缓存 proxy_buffering off; #设置反向代理地址同时与上面的域名保持一致 proxy_pass http://localhost; } #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; } } }

6.启动nginx代理

./sbin/nginx        ps:启动nginx代理服务

./sbin/nginx -s reload 重启nginx代理服务

可能遇到的问题:

    80端口被占用:

   netstat -an |grep 80 查看端口

   ps -ef |grep nginx 查看服务进程

  kill -9 进程号         删死进程

二,负载服务器部署(以192.168.33.13服务器为例,其它服务器一样

   1.在nginx代理服务器上装上nginx软件(下载地址:http://nginx.org/en/download.html

   2.在linux系统中下载nginx安装包

      切换目录:cd /usr/local

     执行如下命令:wget  http://nginx.org/download/nginx-1.10.3.tar.gz

   3.解压nginx压缩包并重命名

    tar -zxvf nginx-1.10.3.tar.gz       ps:解压缩包

    rm nginx-1.10.3.tar.gz               ps:删除下载的压缩包

    mv nginx-1.10.3.tar.gz nginx     ps:重命名

   4.进入nginx目录

    编译安装nginx

    执行命令:.cd nginx                                                ps:切换到nginx软件目录

                      ./configure --prefix=/usr/local/nginx       ps:配置nginx软件

                      make                                                     ps:编译nginx软件

                      make install                                           ps:安装nginx软件

   

   5.新建日志目录及文件

      在nginx目录下新建logs

      mkdir logs      新建logs目录

      cd logs

      vi error.log    新建error.log文件

      vi access.log  新建access.log文件

  7.配置nginx.conf文件

   

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
    location / {
           index  index.html index.htm index.php;
           root   html;
       }
       

        #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;#指定php文件存放的目录
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; # $document_root 代表当前请求在root指令中指定的值
            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 ssl;
    #    server_name  localhost;

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

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

  8.启动nginx代理

 ./sbin/nginx        ps:启动nginx代理服务

   ./sbin/nginx -s reload 重启nginx代理服务

 9.安装 php和php-fpm管理工具

     yum install php   安装php

     yum install php-fpm安装fpm管理工具

 10.启动php-fpm

    systemctl start php-fpm

    service php-fpm status  查看服务状态

    11.部署完成之后访问代理服务器http://192.168.33.10/test.php    

          ps:如果访问不通关闭防火墙 或者防火墙打开需要的80端口  关闭防火墙的命令为:systemctl stop firewalld.service

     

问题处理:

访问报错connect() failed (111: Connection refused)  可能是因为php-fpm未启动 

参考资料

http://blog.csdn.net/ye1992/article/details/51371571

原文地址:https://www.cnblogs.com/lglblogadd/p/7424120.html