• nginx的安装和负载均衡例子(RHEL/CentOS7.4)


    首先安装RHEL/CentOS7.4 mini ,然后关闭防火墙和 selinux ,更新系统(参看配置linux使用本地yum安装源Redhat7/CentOS7 关闭防火墙和 selinux两个笔记)

     nginex分为稳定版(stable vision)和主线版(mainline vision),生产服务器上使用稳定版,以下例子所使用的是1.14稳定版。

     安装

    1、增加/etc/yum.repos.d/nginex.repo文件(这一步可能不需要):

    vi /etc/yum.repos.d/nginex.repo

    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/  #OS =rhel 或者CentOS,

                         #OSERLEASE=版本号,5、6、7等,$basearch=硬件架构如x86_64等,

                         #建议输入http://nginx.org/packages/查看下目录结构
    gpgcheck=0
    enabled=1

    2、安装必要的第三方程序:

    yum install gcc gcc-c++

    yum insall pcre*

    yum install openssl-devel

    3、下载稳定版的nginx解压出来:

    wget http://nginx.org/download/nginx-1.14.0.tar.gz

    tar -zvxf nginx-1.14.0.tar.gz

     4、进入目录安装:

    cd nginx-1.14.0

    ./configure --prefix=/usr/local/nginx     #设定nginx的安装路径为/usr/local/nginx

    详细安装参数见:nginx ./configura 参数说明

    #如果你需要安装一些模块,必须在./configure 里面指明,比如:

    ./configure --prefix=/usr/local/nginx   ----with-http_stub_status_module     #监控模块

    --with-poll_module    #轮询模块

    --with-threads     #多线程模块

    --with-http_stub_status_module    #状态管理模块

    #然后安装

    make

    make install

    5、启动服务

    cd /usr/local/nginx/sbin

    ./nginx    #启动服务

    ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx  #在SBIN目录中建立静态链接,就直接输入nginx就可以运行程序了

    6、检查服务是否启动:

    ss -napt | grep 80       #检查80端口是否在侦听

    ps -ef | grep nginx       #检查nginx进程是否在后台跑

    curl 127.0.0.1        #看返回的结果是否有“Welcome to nginx!”字样,有就成

         #功了。

    ./nginx -s reload          #如果做了修改,这是重启服务的方法。

    测试配置文件是否有错:   nginx -t    (已经在/usr/sbin目录中建好映射,没有的话请到安装目录下运行  ./nginx -t)

    参考 建立nginx的后台服务

    7、nginx的命令参数

    Options:

      -?,-h         : this help帮助

      -v            : show version and exit 显示版本不执行

      -V            : show version and configure options then exit显示版本及配置项(没什么用)

      -t            : test configuration and exit测试配置并退出

      -T            : test configuration, dump it and exit测试配置,不应用配置,退出

      -q            : suppress non-error messages during configuration testing

      -s signal     : send signal to a master process: stop, quit, reopen, reload

      -p prefix     : set prefix path (default: /usr/local/nginx/)

      -c filename   : set configuration file (default: conf/nginx.conf)指定运行的配置文件

      -g directives : set global directives out of configuration file

    建立nginx的后台服务

    用chkconfig定义后台服务

    用systemctl定义后台服务

    测试配置文件是否有错:   nginx -t    (已经在/usr/sbin目录中建好映射,没有的话请到安装目录下运行  ./nginx -t)

    服务操作:

    nginx -s reload(重启服务)

    nginx(启动服务)

    nginx -s stop (停止服务)

    虽然可以用service 或者systemctl来启停服务,但必须养成修改配置后,先测试配置文件是否通过的习惯。如果配置文件有错,不测试直接重启服务会导致服务停止。

    nginx一般http负载均衡配置(内附注释)

    nginx.conf 位于: /usr/local/nginx/conf目录中

    一个例子

    vim nginx.conf

    #user  nobody;   #关闭了非特权用户启动

    worker_processes  3;  #与CPU核心数相等

    #error_log  logs/error.log;   #这几个是日志的级别,如果不记日志就关掉

    #error_log  logs/error.log  notice;

    #error_log  logs/error.log  info;

    pid        logs/nginx.pid;  #PID文件

    events {

        worker_connections  1024;    #最大连接数,每个worker只支持1024个链接

    }

    http {

        include       mime.types;

        default_type  application/octet-stream;

        sendfile        on;

        keepalive_timeout  65;   #连接保持时间65秒

        upstream ltwww {   #一个服务器池

        server 192.168.4.106:80;   #池中的节点地址和端口号

        server 192.168.4.107:80;

        server 192.168.4.108:80;

        server 192.168.4.109:80;

        }

        server {    #一个对外服务,提供ltwww.kefangapp.com:80端口的对外服务

    listen 80;    #侦听端口

    server_name ltwww.kefangapp.com;    #绑定的域名

    location / {

        proxy_pass http://ltwww    #转发到哪个服务器池

       client_max_body_size 1000m;   #默认上传文件大小是1M,改为1000M。

       proxy_cache off;   #在测试环境中经常改变服务器配置时最好把缓存关闭,以免节点服务器的老配置还存在nginx的缓存中。

        proxy_set_header Host $host;    #设置主机头为绑定的域名

        proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for; #启用x转发

            }

        } 

            server {    #在相同域名下以“/目录名/”区分的不同对外应用

                    listen 80;

                    server_name ltapi.kefangapp.com;   #公共的域名

                    location /webapi/ {    #第一个目录

                            proxy_pass http://ltwww;    #转发到对应的节点服务器组中

                            proxy_cache off;   #在测试环境中经常调试所以关闭Cache,生产环境中打开

                            client_max_body_size 1000m;  #允许客户端上传文档限制为1G,默认为1M。

                            proxy_set_header Host $host;   #转发请求的头部

                            proxy_set_header X-Real-IP $remote_addr;  #替换掉远程地址

                            proxy_set_header X-Forwarder-For @proxy_add_x_forwarded_for;  

                    }

                    location /wx/ {    #第二个目录

                            proxy_pass http://ltwww;

                            proxy_cache off;

                            client_max_body_size 1000m;

                            proxy_set_header Host $host;

                            proxy_set_header X-Real-IP $remote_addr;

                            proxy_set_header X-Forwarder-For @proxy_add_x_forwarded_for;

                    }

                            location /mjd/ {   #第三个目录

                            proxy_pass http://ltwww;

                            proxy_cache off;

                            client_max_body_size 1000m;

                            proxy_set_header Host $host;

                            proxy_set_header X-Real-IP $remote_addr;

                            proxy_set_header X-Forwarder-For @proxy_add_x_forwarded_for;

                    }

            }

        server {    #这是一个状态信息的对外服务,还非常简陋

            listen      80;

            server_name  192.168.10.224;

                  location /status {

                  stub_status on;

                 access_log off;

       }

          #这是一个本地的报错页

            error_page   500 502 503 504  /50x.html;

            location = /50x.html {

                root   html;

            }

        }

    }

    用OpenSSL生成SSL使用的 Crt和key文件

    OpenSSL包含在RHEL7.4的光盘中,把光盘做成安装源就能安装,而且一般都在最小安装里面包含了。

    mkdir SSL-KEY  #新建一个文件来存放各种证书文件

    cd SSL-KEY  #进入目录

    证书一般用到三个文件:*.key(私钥文件) *.crt(证书文件)  *.csr(证书请求文件)

    步骤如下:

    1、生成CA和server的key文件,要记住密码(参考密码:a1b2c3d/)

    openssl genrsa -des3 -out CA.key 2048   #加密算法是des3,openssl格式,2048位强度,名为CA.key

    #如果要去除key文件的密码(前提是你还记得它的密码)多一步操作:

    openssl rsa -in CA.key -out CA.key    #在输入密码后就能得到一个无密码保护的key文件

    #生成server端(ltwww.kefangapp.com)的key文件,并去除密码:

    openssl genrsa -des3 -out ltwww.kefangapp.com.key 2048

    openssl rsa -in ltwww.kefangapp.com.key -out ltwww.kefangapp.com.key

    2、用CA的key来生成CA的crt证书:

    openssl req -new -x509 -key CA.key -out CA.crt -days 7300   #生成x509证书,使用CA.key私钥进行签名,证书名为CA.crt,有效期7300天(20年)。

    #在生成证书过程中还会让你输入:国家代码(CN),省/州(GuangDong)/城市(ShenZhen)/组织机构(CA)/机构名称(CAB)/公开名称(CAB.CA.com)/电邮地址(admin@CA.com)

    3、生成服务器端的证书

    #生成证书的请求(*.csr文件)

    openssl req -new -key ltwww.kefangapp.com.key -out ltwww.kefangapp.com.csr   #用对应的私钥生成对应的证书请求文件

    #生成证书(*.crt文件),也就是用CA的证书来签名之后的文件

    openssl x509 -req -days 3650 -in ltwww.kefangapp.com.csr -CA CA.crt -CAkey CA.key -CAcreateserial -out ltwww.kefangapp.com.crt    #生成x509证书,有效期10年,在证书的请求文件上加上CA的证书和私钥,输出成*.crt的证书文件

    4、最后,可以选择吧私钥和SSL证书合并成*.pem文件

    cat ltwww.kefangapp.com.key ltwww.kefangapp.com.crt > ltwww.kefangapp.com.pem

    nginx 自动把访问http的请求转换到https(内附注释)

    本说明需要使用证书,证书生成方法见:用OpenSSL生成SSL使用的 Crt和key文件

     cat nginx.conf

    #user noboy;

    worker_processes 3;   #处理器核心数量

    error_log logs/error.log info;    #日志级别info

    pid logs/nginx.pid;   #把进程ID号写到此文件中

    events {

            worker_connections  1024;   #max for 1024   定义最大连接数

    }

    http {   #这是一个http的负载均衡,

            include mime.types;   #定义mime的类型

            default_type application/octet-stream;

            sendfile on;

            keepalive_timeout 65;   #连接活动时间,超时会关闭

            upstream ltwww {    #定义节点服务器组的名称,以及内部的节点

                    server 192.168.4.106:80;

            }

            server {   #定义一个对外接受访问的http服务器

                    listen 443 ssl;    #侦听443端口,也就是接受HTTPS访问的参数

                    server_name ltwww.kefangapp.com;  #接受访问请求的绑定域名

                    ssl on;    #打开SSL

                    ssl_certificate SSL-KEY/ltwww.kefangapp.com.pem;   #SSL证书的位置

                    ssl_certificate_key SSL-KEY/ltwww.kefangapp.com.key;  #证书密钥的位置

                   

                    listen 80;   #还要侦听http 80端口

                    if ($scheme = http) {      #如果访问的是80端口

                            return 301 https://$host$request_uri;    #替换成https

                    }

                    location / {    #请求的转发操作

                            proxy_pass http://ltwww;    #以Http协议转发到ltwww节点服务器组

                            proxy_cache off;    #测试环境关闭Cache,生产环境要打开

                            client_max_body_size 1000m;   #允许上传文件大小一千兆视频没问题

                            proxy_set_header Host $host;  #头部完整保留转发

                            proxy_set_header X-Real-IP $remote_addr;  #IP部分换成服务器内部IP

                            proxy_set_header X-Forwarder-For @proxy_add_x_forwarded_for; #应用转发参数

                    }

            }

            error_page 500 502 503 504  /50x.html;   #一个错误页的处理

            location = /50x.html {  root html; }

            }

    }

  • 相关阅读:
    [Codeforces #494] Tutorial
    [BZOJ 3223] 文艺平衡树
    [P2698][USACO12MAR]花盆Flowerpot
    [Atcoder Regular Contest 061] Tutorial
    [BZOJ 1855] 股票交易
    [BZOJ 1076] 奖励关
    [BZOJ 2298] Problem A
    数据库三大范式
    mybatis插件机制原理
    Mybatis有哪些执行器?
  • 原文地址:https://www.cnblogs.com/gucb/p/11157347.html
Copyright © 2020-2023  润新知