centOS7 安装nginx以及配置及负载均衡等讲解

centOS7 安装nginx

一、Nginx是什么?
Nginx是一款轻量级Web服务器,也是一款反向代理服务器
官网:http://nginx.org/
中文文档: http://www.nginx.cn/doc/

#####二、 Nginx能干什么?

Nginx能干的事情很多,这里简要罗列一-些:

1、可直接支持Rails和PHP的程序
2、可作为HTTP反向代理服务器
3、作为负载均衡服务器
4、作为邮件代理服务器
5、帮助实现前端动静分离
6、防止攻击
三、Nginx安装
官网下载:http://nginx.org/en/download.html
或者直接在linux执行命令:wget http://nginx.org/download/nginx-1.12.2.tar.gz
这里下载的版本是1.12.2
//一键安装四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
//进入/usr/local目录下
cd /usr/local

//新建一个目录
mkdir nginx

//进入新建目录
cd nginx

//下载tar包
wget http://nginx.org/download/nginx-1.12.2.tar.gz
//解压
tar -xvf nginx-1.12.2.tar.gz

//进入nginx目录
cd /usr/local/nginx/nginx-1.12.2
//执行命令
./configure

//执行make命令
make

//执行make install命令
make install

//查找安装目录
whereis nginx

//测试配置文件
/usr/local/nginx/sbin/nginx -t


修改nginx端口,配置nginx.conf(如果80端口冲突可以执行该步)


# 打开配置文件
vi /usr/local/nginx/conf/nginx.conf

#将端口号改成8089,因为可能apeache占用80端口,apeache端口尽量不要修改,我们选择修改nginx端口

在这里插入图片描述

//查看进程命令
ps -ef|grep nginx

//平滑重启
kill -HUP Nginx的主进程号
#防火墙配置

//打开防火墙文件
vim /etc/sysconfig/iptables
//新增行  开放80端口
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
//保存退

//重启防火墙
service iptables restart
#Nginx启动

//进入nginx安装目录
cd sbin

//启动
./nginx  【启动】
./nginx -s stop 【此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程】
./nginx -s quit 【此方式停止步骤是待nginx进程处理任务完毕进行停止】
./nginx -s reload 【重启】
#测试访问nginx

http://ip     【ip是所在服务器的主机ip】

效果:
在这里插入图片描述

四、修改nginx配置,访问端口为8089 的tomcat服务器:

第一种:

在这里插入图片描述

启动端口为8089的tomcat

重启nginx

./nginx -s reload

效果:页面输入nginx配置文件中的 server_name对应的值,即为: 192.168.200.135

在这里插入图片描述

第二种:

在这里插入图片描述
配置hosts文件:(192.168.200.135为服务器主机ip)

在这里插入图片描述

效果:页面输入nginx配置文件中的 server_name对应的值,即为: www.yh.com

在这里插入图片描述

nginx配置:

nginx.conf文件:
注意 :backup 的作用:

   upstream backend {
     #ip_hash;
     server 192.168.200.135:8089 weight=1;
     server 192.168.200.137:8080 backup;
    }

backup 的作用:只要在希望成为备份的服务器ip后面多添加一个backup参数,这台服务器就会成为备份服务器。 在平时不使用,nginx不会给它转发任何请求。只有当其他节点全部无法连接的时候,nginx才会启用这个节点。 一旦有可用的节点恢复服务,该节点则不再使用,又进入后备状态。
可以两台机子互为热备,平时各自负责各自的服务。在做上线更新的时候,关闭一台服务器的tomcat后,nginx自动把流量切换到另外一台服务的后备机子上,从而实现无痛更新,保持服务的持续性,提高服务的可靠性。

#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;
    # 设定负载均衡后台服务器列表
    upstream backend {
     #ip_hash;
     server 192.168.200.135:8089 weight=1;
     server 192.168.200.137:8080 backup;
    }
    #浏览器访问的ip+端口
    server {
        listen       80;
        server_name  192.168.200.135;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
	    #proxy_pass   http://127.0.0.1:8089;
	    proxy_pass http://backend;
        }

        #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;
    #    }
    #}

}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

配置location的匹配规则:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

location表达式类型
~ 表示执行一个正则匹配,区分大小写
~* 表示执行一个正则匹配,不区分大小写
^~ 表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他location。
= 进行普通字符精确匹配。也就是完全匹配。
@ 它定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
优先级:
在nginx的location和配置中location的顺序没有太大关系。正location表达式的类型有关。相同类型的表达式,字符串长的会优先匹配。

以下是按优先级排列说明:

等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项。
^~类型表达式。一旦匹配成功,则不再查找其他匹配项。
正则表达式类型(~ ~*)的优先级次之。如果有多个location的正则能匹配的话,则使用正则表达式最长的那个。
常规字符串匹配类型。按前缀匹配
location优先级示例

参考: https://www.cnblogs.com/cheyunhua/p/8576701.html
以下例子说明都以该server为基础:

server {
   listen    8861;
   server_name  abc.com;
 }
location = / {
# 仅仅匹配请求 /
[ configuration A ]
}
location / {
# 匹配所有以 / 开头的请求。
# 但是如果有更长的同类型的表达式,则选择更长的表达式。
# 如果有正则表达式可以匹配,则优先匹配正则表达式。
[ configuration B ]
}
location /documents/ {
# 匹配所有以 /documents/ 开头的请求。
# 但是如果有更长的同类型的表达式,则选择更长的表达式。
# 如果有正则表达式可以匹配,则优先匹配正则表达式。
[ configuration C ]
}
location ^~ /images/ {
# 匹配所有以 /images/ 开头的表达式,如果匹配成功,则停止匹配查找。
# 所以,即便有符合的正则表达式location,也不会被使用
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配所有以 gif jpg jpeg结尾的请求。
# 但是 以 /images/开头的请求,将使用 Configuration D
[ configuration E ]
}

客户端访问返回结果:

客户端访问: http://abc.com/   返回-> configuration A
客户端访问: http://abc.com/index.html 返回-> configuration B
客户端访问: http://abc.com/documents/document.html 返回-> configuration C
客户端访问: http://abc.com/images/1.gif 返回-> configuration D
客户端访问: http://abc.com/documents/1.jpg 返回-> configuration E

在这里插入图片描述

例如:

在这里插入图片描述
在这里插入图片描述

负载均衡:

在这里插入图片描述

在这里插入图片描述

重定向:last 和break的区别:

在这里插入图片描述

在这里插入图片描述

注意:break与last的区别:

break是终止循环,但是还会继续往下执行

last是终止执行last之后的内容。

.
.
.
.

注意:访问www.meiduo.site:80/a/b 会有如下三种情况:

在这里插入图片描述

在这里插入图片描述

情况一:上图配置的root对应目录下面的 a文件夹下的b文件如果存在,假设b文件中的内容是:“ 欢迎访问我b文件” 即:/home/python/Desktop/code/meiduo_project01/server_static/a/b

那么客户端访问www.meiduo.site:80/a/b 就会显示b文件中的内容,即显示:“ 欢迎访问我b文件”

情况二:上图配置的root对应目录下面的 a文件夹下的b文件如果不存在,而b是一个文件夹,b文件夹下面没有默认文件 即:/home/python/Desktop/code/meiduo_project01/server_static/a/b/

那么访问www.meiduo.site:80/a/b 就会被重定向到www.meiduo.site:80/a/b/ 然后页面报错禁止访问403

解决办法:给location加一个配置,即: autoindex on ;

情况三:上图配置的root对应目录下面的 a文件夹下的b文件如果不存在,而b是一个文件夹,b文件夹下面有默认文件index.html 即:/home/python/Desktop/code/meiduo_project01/server_static/a/b/index.html

而且index.html中的内容是:“欢迎访问index.html页面!”

那么访问www.meiduo.site:80/a/b 就会被重定向到www.meiduo.site:80/a/b/ 然后页面显示index.html中的内容,即:欢迎访问index.html页面!”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值