nginx实战

nginx实战

一、安装

1.1 下载

下载地址 地址,本次下载的是nginx-1.20.1稳定版本,下载完毕后拖入服务器 /opt/文件夹里

1.2 安装必备的环境

  • gcc安装

    安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装

    yum -y install gcc gcc-c++ autoconf automake
    
  • zlib安装 PCRE pcre-devel 安装 OpenSSL 安装

    zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos上安装 zlib 库。

    yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
    

1.3 nginx配置安装并启动

解压nginx

tar -zxvf nginx-1.20.1.tar.gz

进入ngnix目录,执行以下命令, 注意–prefix等号后面的一串为 /opt/nginx-1.20.1 nginx目录

cd /opt/nginx-1.20.1
./configure --prefix=/opt/nginx-1.20.1 --with-http_stub_status_module --with-http_ssl_module

安装命令

make && make install

查看nginx 版本

./sbin/nginx -v

常见nginx的log文件夹

mkdir logs

到此, nginx安装完成 启动nginx启动sbin目录下的nginx

启动nginx

./sbin/nginx

查看nginx进程,可以根据IP访问进行测试

ps -ef | grep nginx

二、常用命令

//启动nginx [root@iz2zedkdcntqh9bniwae18z sbin]# ./sbin/nginx//停止 [root@iz2zedkdcntqh9bniwae18z sbin]# ./sbin/nginx -s stop //重启[root@iz2zedkdcntqh9bniwae18z sbin]# ./sbin/nginx -s reload//查看配置文件知否正确[root@iz2zedkdcntqh9bniwae18z sbin]# ./sbin/nginx -t//详情 [root@iz2zedkdcntqh9bniwae18z sbin]# ps -ef | grep nginx

三、常用配置讲解

	server{    	# 监听端口		listen	80;    	# 域名地址		server_name amoqi.cn;		location / {        	# 转发到本地			proxy_pass http://127.0.0.1:8080/bsdn/;        	# head转发			proxy_set_header request_uri $request_uri;            proxy_set_header   Host    $host;            proxy_set_header   Remote_Addr    $remote_addr;            proxy_set_header   X-Real-IP    $remote_addr;            proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;			client_max_body_size 1000m;		}	}

五、负载均衡

5.1 配置负载均衡

现在有4个机器分别为192.168.67.67,192.168.67.68,192.168.67.69,192.168.67.70,67机器上有个nginx,其他三个机器上装上tomcat,四个机器的tomcat

<%@ page session="true" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>    <body>        <h1>Welcome to 豆豆把VOB</h1>        <h3><%=session.getId() %></h3>        <h3><%=request.getLocalAddr() %>--<%=request.getLocalPort() %></h3>        <h3><%=request.getRemoteAddr() %></h3>    </body>

启动三个tocmat

/opt/apache-tomcat-8.5.70/bin/startup.sh

修改 /opt/nginx-1.20.1/conf,配置如下,添加了upstream项修改了location的数据

http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;	upstream amoqi{	     server 192.168.67.68:8080;	     server 192.168.67.69:8080;	     server 192.168.67.70:8080;		}    server {        listen       80;        server_name  localhost;        location / {	     proxy_pass http://amoqi;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}

重启nginx

./sbin/nginx -s reload

重复访问url http://192.168.67.67/ ,发现负载到不同的服务器上了

5.2 负载均衡策略

5.2.1 请求轮询
  • 依次转发给配置的服务器
5.2.2 增加权重
  • 使用服务器的权重,谁的权重越大,分发到的请求越多

    upstream amoqi{    server 192.168.67.68:8080 weight=2;    server 192.168.67.69:8080 weight=3;    server 192.168.67.70:8080 weight=1;}
    
5.2.3 最少连接数
  • 在连接负载最少的情况下,nginx会避免将过多的请求发放给繁忙的服务器,而将新的请求分发给不太繁忙的服务器,避免服务器过载

    upstream amoqi{	least_conn;    server 192.168.67.68:8080;    server 192.168.67.69:8080;    server 192.168.67.70:8080;}
    
5.2.4 IP hash
  • 确保来自同一客户端的请求将始终定向到同一服务器,除非服务器不可用

    upstream amoqi{	ip_hash;    server 192.168.67.68:8080;    server 192.168.67.69:8080;    server 192.168.67.70:8080;}
    

四、配置资源静态化

4.1 配置静态资源

在被转发的服务器上配置静态资源

<body>	<h1>Welcome to 豆豆把VOB</h1>	<h3><%=session.getId() %></h3>	<h3><%=request.getLocalAddr() %>--<%=request.getLocalPort() %></h3>	<h3><%=request.getRemoteAddr() %></h3>	<img src="/static/img/ms.jpg" height="200px"/></body>
## 在Nginx的html文件夹下创建/static/img文件夹,放入图片ms.jpg## 放入locationlocation ^~ /static/ {	root html;}

4.2 路径路由规则

location = / {精确匹配,必须是127.0.0.1/#规则A}location = /login {精确匹配,必须是127.0.0.1/login#规则B}location ^~ /static/ {非精确匹配,并且不区分大小写,比如127.0.0.1/static/js.#规则C}location ~ \.(gif|jpg|png|js|css)$ {区分大小写,以gif,jpg,js结尾#规则D}location ~* \.png$ {不区分大小写,匹配.png结尾的#规则E}location !~ \.xhtml$ {区分大小写,匹配不已.xhtml结尾的#规则F}location !~* \.xhtml$ {#规则G}location / {什么都可以#规则H}那么产生的效果如下:访问根目录/, 比如http://localhost/ 将匹配规则A访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H访问 http://localhost/static/a.html 将匹配规则C访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而 http://localhost/static/c.png 则优先匹配到 规则C访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

结语

码字不易,希望能多多支持。一名四年工作经验的程序猿,目前从事物流行业的工作,有自己的小破网站amoqi.cn。欢迎大家关注公众号【CoderQi】,一起来交流JAVA知识,包括但不限于SpringBoot+微服务,更有奇奇JAVA学习过程中的工具、面试资料和专业书籍等免费放送,也可以加个人联系方式,见公众号下方工具栏上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是刘奇奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值