redHat安装ngnix,以及负载均衡配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/su1573/article/details/87883981

1、Nginx说明:Nginx是一款高性能的HTTP服务器,以及反向代理服务器,其由俄罗斯Igor Sysoev创始,经过各种变迁得来。在2008年,Igor Sysoev所在的公司打算将其公司的wordpress博客平台搬到软件上去,当时使用的是Apache,但是当其公司工程师修改安装设置的时候,Apache竟然死机了,于是其发现此Apache在流量比较大的时候,根本不稳定,于是其公司撤除了Apache,将所有的精力投入到当时并不成熟的Nginx开源项目上,5年后,其公司的wordpress仍然高效的、稳定的运行在Nginx上。可想而知,Nginx超强的稳定性、丰富的功能集、示例配置文件和低系统资源消耗是让它远近闻名的最大特点。

在我国,使用Nginx的大型网站很多,比如百度、淘宝、腾讯、新浪博客、网易新闻、酷6网、豆瓣网。。。

注:
(1)HTTP,超文本传输协议,主要应用在web界面;

(2)反向代理服务器:大家都有过这样的经历,拨打10086客服电话,可能一个地区的10086客服有几个或者几十个,你永远都不需要关心在电话那头的是哪一个,叫什么,男的,还是女的,漂亮的还是帅气的,你都不关心,你关心的是你的问题能不能得到专业的解答,你只需要拨通了10086的总机号码,电话那头总会有人会回答你。那么这里的10086总机号码就是我们说的反向代理。客户不知道真正提供服务人的是谁。

反向代理隐藏了真实的服务端,当我们请求 www.baidu.com 的时候,就像拨打10086一样,背后可能有成千上万台服务器为我们服务,但具体是哪一台,你不知道,也不需要知道,你只需要知道反向代理服务器是谁就好了,www.baidu.com 就是我们的反向代理服务器,反向代理服务器会帮我们把请求转发到真实的服务器那里去。

Nginx就是性能非常好的反向代理服务器,用来做负载均衡。


2、Nginx基本特征有: (1)处理静态文件,索引文件以及自动索引,打开文件描述符缓冲。 (2)无缓存的反向代理加速,简单的负载均衡和容错。 (3)模块化的结构,包括gzipping,byte ranges,chunked responses以及SSI-filter等filter,如果由FastCGI或其它代理服务器处理蛋液中存在的多个SSI,则这项处理可以并行运行,而不需要相互等待。 (4)支持SSL和TLSSNI。
3、Nginx软件下载。由于Nginx的安装需要依赖openssl库、Zlib库、Pcre库,所以需要下载这三种类型的库,以及Nginx的安装包。

注:
(1) openssl库是为了SSL功能服务的,SSL是安全套接层,是为网络通信提供安全及数据完整性的一种安全协议。

(2)Zlib库是为Gzip模块服务的,zlib是提供数据压缩用的函式库。

(3)Pcre库是rewrite模块服务的,是正则表达式的一种库。

其四种类型的数据包文件,下载链接如下:链接:https://pan.baidu.com/s/11g5igpfIBGOR_-WlBphEqA 密码:373n

4、将下载的Nginx安装包以及openssl、Zlib、Pcre库通过Xshell软件上传至Redhat Linux系统中,操作步骤如下:

挂载系统镜像

[root@localhost ~]# mount /dev/cdrom /media/cdrom/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ~]# 

通过yum源安装lrzsz

[root@localhost ~]# yum install lrzsz

在linux系统上创建空白文件夹,将上述下载的安装包mv至新文件夹中。

[root@localhost ~]# mkdir -p  /usr/local/yyy/nginx/soft/

[root@localhost ~]# mv openssl-1.1.0h.tar.gz pcre-8.38.tar.gz zlib-1.2.11.tar.gz nginx-1.13.12.tar.gz /usr/local/yyy/nginx/soft/

[root@localhost ~]# cd /usr/local/yyy/nginx/soft/

[root@localhost soft]# ll

安装SSL功能,需要安装 openssl 操作如下:

[root@localhost soft]# tar xzvf openssl-1.1.0h.tar.gz     //解压缩openssl-1.1.0h.tar.gz
[root@localhost soft]# cd openssl-1.1.0h/               
[root@localhost openssl-1.1.0h]# ./config                //编译源码包代码

[root@localhost openssl-1.1.0h]# make && make install  //生成二进制安装程序,并且通过二进制服务程序进行安装。

安装Gzip模块,需要安装zlib库,操作如下:

[root@localhost soft]# tar xzvf zlib-1.2.11.tar.gz 
[root@localhost soft]# cd zlib-1.2.11/
[root@localhost zlib-1.2.11]# ./configure 

[root@localhost zlib-1.2.11]# make && make install

安装rewrite模块,需要先安装pcre库,但是在安装pcre库的过程中,需要先通过yum源安装gcc gcc-c++ 。操作步骤如下:

[root@localhost soft]# yum install -y gcc gcc-c++

[root@localhost soft]# tar pcre-8.38.tar.gz 
[root@localhost soft]# cd pcre-8.38/
[root@localhost pcre-8.38]# ./configure 

[root@localhost pcre-8.38]# make && make install

安装Nginx服务。操作如下:

[root@localhost soft]# tar xzvf nginx-1.13.12.tar.gz 
[root@localhost soft]# cd nginx-1.13.12/
[root@localhost nginx-1.13.12]# ./configure 

[root@localhost nginx-1.13.12]# make && make install

监测Nginx服务是否安装成功。

[root@linux soft]# cd /usr/local/nginx/sbin/
[root@linux sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

注:如果你登录linux系统不是root用户,那么运行。./nginx -t 出现运行出错,则是没有权限执行,那么需要你su 到超级用户中,使用chmod -R 777 user/local/nginx/ 命令,给此文件加权限。然后在进入普通用户,cd/user/local/nginx/sbin/下执行./nginx -t 即可。

由于nginx使用80端口号,则需要将80端口号添加到iptables中,操作如下:

[root@localhost sbin]# vi /etc/sysconfig/iptables

在iptables中输入以下内容:

#sample configuration for iptables service
#you can edit this manually or use system-config-firewall
#please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [30:2888]
-A INPUT -p tcp -m   tcp --dport 80 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

上述配置完成后,打开浏览器,输入linux的ip地址,我的是http://192.168.XXX.XXX/ 出现如下信息 ,则说明nginx安装完成。

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

如访问不到则可能iptables内容配置错误,或者是虚拟机防火墙未关闭。

12、优化Nginx开机自启动,使用server命令即可。操作步骤如下:

[root@localhost ~]# cd /etc/init.d/

[root@localhost init.d]# vi nginx 

将如下脚步文件,复制到nginx文件中。

#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig: - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#   proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config: /etc/nginx/nginx.conf 
# config: /etc/sysconfig/nginx 
# pidfile: /var/run/nginx.pid 
# Source function library. 
. /etc/rc.d/init.d/functions 
# Source networking configuration. 
. /etc/sysconfig/network 
# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 
    nginx="/usr/local/nginx/sbin/nginx" 
    prog=$(basename $nginx) 
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
    lockfile=/var/lock/subsys/nginx 
 
start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE 
    retval=$? 
    echo 
[ $retval -eq 0 ] && touch $lockfile 
    return $retval 
} 
 
stop() { 
    echo -n $"Stopping $prog: " 
    killproc $prog -QUIT 
    retval=$? 
    echo 
[ $retval -eq 0 ] && rm -f $lockfile 
    return $retval 
    killall -9 nginx 
} 
 
restart() { 
    configtest || return $? 
    stop 
    sleep 1 
    start 
} 
 
reload() { 
    configtest || return $? 
    echo -n $"Reloading $prog: " 
    killproc $nginx -HUP 
    RETVAL=$? 
    echo 
} 
 
force_reload() { 
    restart 
} 
 
configtest() { 
    $nginx -t -c $NGINX_CONF_FILE 
} 
 
rh_status() { 
    status $prog 
} 
 
rh_status_q() { 
    rh_status >/dev/null 2>&1 
} 
 
case "$1" in 
    start) 
        rh_status_q && exit 0 
        $1 
    ;; 
    stop) 
        rh_status_q || exit 0 
        $1 
    ;; 
    restart|configtest) 
        $1 
    ;; 
    reload) 
        rh_status_q || exit 7 
        $1 
    ;; 
    force-reload) 
        force_reload 
    ;; 
    status) 
        rh_status 
    ;; 
    condrestart|try-restart) 
        rh_status_q || exit 0 
    ;; 
    *) 
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
        exit 2 
esac 

然后wq,保存退出!

[root@localhost init.d]# chmod  777 nginx   //修改此nginx文件的权限
[root@localhost init.d]# chkconfig --add nginx   // 将脚本文件添加至chkconfig中,以便让chkconfig检查、设置系统的各种服务。

[root@localhost init.d]# chkconfig --level 35 nginx on //设备nginx开机在3和5级别自动启动。

查看nginx进程

[root@linux sbin]# ps -ef|grep nginx

查看进程pip

[root@linux sbin]# ps -C nginx -o pid

-------------------------《启动ngnix命令》-----------------------------

[root@linux ~]# cd /usr/local/nginx/sbin/
[root@linux sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

或者[root@linux sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf

ps: -c参数指定了要加载的nginx配置文件路径

------------------------《停止nginx命令》-----------------------------

第一种方式:从容停止
1、查看进程id
[root@linux sbin]# ps -ef|grep nginx
root       5915      1  0 14:58 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     5916   5915  0 14:58 ?        00:00:00 nginx: worker process
root       6081   2957  0 15:00 pts/0    00:00:00 grep --color=auto nginx
2、杀死进程
[root@linux sbin]# kill -QUIT 5915
[root@linux sbin]#
第二种方式:快速停止
[root@linux sbin]# ps -ef|grep nginx
root       6378      1  0 15:04 ?        00:00:00 nginx: master process ./nginx -c /usr/local/nginx/conf/nginx.conf
nobody     6379   6378  0 15:04 ?        00:00:00 nginx: worker process
root       6514   2957  0 15:05 pts/0    00:00:00 grep --color=auto nginx
[root@linux sbin]# kill -TERM 6378
或 [root@linux sbin]# kill -INT 6378
[root@linux sbin]# 
第三种方式:强制停止
[root@linux sbin]# pkill -9 nginx

------------------------《重启》--------------------------------------------

1、验证nginx配置文件是否正确
[root@linux sbin]# cd /usr/local/nginx/sbin/
[root@linux sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

或者

[root@linux ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
2、重启Nginx服务
[root@linux sbin]# cd /usr/local/nginx/sbin/
[root@linux sbin]# ./nginx -s reload

配置多个tomcat。启动时报

 Cannot find ./catalina.sh The file is absent or does not have execute permission

则对catalina.sh进行权限更改

 chmod 777 catalina.sh

------------------------《负载均衡配置》----------------------------------

修改/usr/local/nginx/conf/nginx.conf

1、在http节点之间增加upstream
upstream testserver {
   server  192.168.6.131:8082;
    server  192.168.6.131:8083;
    server  192.168.6.131:8084;
    server  192.168.6.131:8085;
}
2、在server节点的location节点里面增加proxy_pass
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    access_log  logs/host.access.log  main;

    location / {
        proxy_pass http://testserver;
        # root   html;
        #index  index.html index.htm;
    }
	
}

一个服务器有多个tomcat的话,

1、先在~/.bash_profile中添加
 CATALINA_BASE3=/home/java/tomcat3/apache-tomcat-8.5.35
 CATALINA_HOME3=/home/java/tomcat3/apache-tomcat-8.5.35
 
 CATALINA_BASE4=/home/java/tomcat4/apache-tomcat-8.5.35
 CATALINA_HOME4=/home/java/tomcat4/apache-tomcat-8.5.35

然后执行 source ~/.bash_profile 重新加载配置文件

2、然后修改tomcat/bin/catalina.sh

在第一行增加

 CATALINA_BASE=$CATALINA_BASE3
 CATALINA_HOME=$CATALINA_HOME3

猜你喜欢

转载自blog.csdn.net/su1573/article/details/87883981