Nginx教程负载均衡机制

 

 

安装上传下载插件

[root@localhost ~]# yum install -y lrzsz

 

设置主机名

关闭防火墙

[root@localhost ~]# hostnamectl set-hostname nod-01

[root@localhost ~]# systemctl stop firewalld

 

安装Nginx

上传文件到root的家目录下

[root@nod-01 ~]# pwd

/root

[root@nod-01 ~]# ll

total 964

-rw-------. 1 root root   1410 Jul  7 22:46 anaconda-ks.cfg

-rw-r--r--. 1 root root 981687 Jul 11 00:46 nginx-1.12.2.tar.gz

 

安装Nginx依赖

yum -y install gcc gcc-c++ autoconf automake

 

[root@nod-01 ~]# yum -y install gcc gcc-c++ autoconf automake

[root@nod-01 ~]# yum -y install zlib zlib-devel  openssl openssl-devel pcre pcre-devel

 

 

查看CPU核数

[root@nod-01 ~]# cat  /proc/cpuinfo | grep processor | wc -l

 

安装模块

/root/nginx-1.12.2/configure --prefix=/usr/local/nginx    --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module

  

查看硬件核数

cat  /proc/cpuinfo | grep processor | wc -l

安装(xxx是查出来的硬件核数)

make -j XXX
make install

 

增加用户

useradd -u 8000 -s /sbin/nologin nginx

  

查询增加的用户的信息

id nginx

 

 

开启Nginx服务

/usr/local/nginx/sbin/nginx  开启服务
/usr/local/nginx/sbin/nginx -t 测试配置文件是否正确的命令
/usr/local/nginx/sbin/nginx -s reload 重载Nginx服务
/usr/local/nginx/sbin/nginx -s stop  停止服务  

 

将Nginx的服务加入到开机启动服务中

 echo '/usr/local/nginx/sbin/nginx &' >> /etc/rc.local

  

在centos7中安装netstat命令

yum -y install net-tools

 查看nginx启动后的端口情况

netstat -anput

 

修改配置文件,配置文件中内容

 

user  nginx nginx;
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 / {
            root   html;
            index  index.html index.htm;
        if ($request_uri ~* \.html$ ){
        proxy_pass http://htmlservers;
        }
         if ($request_uri ~* \.php$ ){
        proxy_pass http://phpservers;
        }
        proxy_pass http://picservers;
        }

        #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;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_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 {
        #    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;
    #    }
    #}
    
    
    upstream htmlservers{
        server 192.168.3.129:80;
        server 192.168.3.130:80;
    }
    
    upstream phpservers{
        server 192.168.3.129:80;
        server 192.168.3.130:80;
    }

    
    upstream picservers{
        server 192.168.3.129:80;
        server 192.168.3.130:80;
    }

    

}
nginx.conf

修改配置文件之后查看配置文件配置是否正确

/usr/local/nginx/sbin/nginx -t

重启Nginx

/usr/local/nginx/sbin/nginx -s reload

 

 在页面访问Nginx可以正常访问,Nginx安装结束

 

 

安装两台Apache服务器做测试

 

安装后Apache

yum install httpd php -y

 

安装成功后再  /var/www/html/    目录下新建三个文件

 

 

posted @ 2019-07-11 17:09  cerofang  阅读(185)  评论(0编辑  收藏  举报