Nginx介绍+openresty配置

参考:资源下载 Nginx介绍+openresty配置

nginx使用场景

1.什么是nginx性能高,官方测试5万并发连接;对cpu 内存资源消耗很低,而且运行非常稳定   免费 开源2.nginx应用场景1.http服务器静态资源  图片 js css 2.虚拟主机"虚拟"出多个主机,  域名80    www.51mmr.net   av.51mmr.com   目录 IP  端口 域名3.反向代理正向代理  简单说 代理服务器  从内-->外  反向代理   	代理服务器    外-->4.负载均衡字面上理解:负载要变得均衡,负载理解为:工作量 

nginx安装

1.nginx安装下载:http://nginx.org/en/download.htmlwget http://nginx.org/download/nginx-1.12.2.tar.gz
2.安装前提因为nginx是c开发的1.gcc对源码进行编译yum install gcc-c++2.PCREperl库 包括了perl兼容的正则表达式 nginx的http模块使用的是pcre来解析正则yum install -y pcre pcre-devel3.zlib压缩和解压的库  nginx 使用的zlib对http包进行 gzip yum install -y zlib zlib-devel4.opensslyum install -y openssl openssl-devel3.nginx安装1.解压tar -zxvf nginx-1.12.2.tar.gz 2.cd 到解压目录cd nginx-1.12.23.configure./configure./configure --prefix=/app/nginx \
--with-pcre \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_ssl_module \
--with-file-aio \
--with-debug \会生成MakeFile4.编译安装make && make install5.启动nginxcd /app/nginx/cd sbin	./nginx 此种方式默认加载的是conf/nginx.conf配置文件6.修改加载配置文件./nginx -c /app/nginx/conf/nginx.conf7.关闭nginx./nginx -s stop先查出pid 然后再kill方式二:完整停止 优雅关闭./nginx -s quit等待nginx处理进程把请求处理完毕 然后再停止8.重启./nginx -s reload./nginx -s quit./nginx

Nginx功能
正向代理:从局域网访问Nginx,成功访问到公网。(代理服务器)
反向代理:公网通过连接Nginx,成功访问到局域网。(代理客户端)
负载均衡:Nginx对大量的访问作转发,让这些访问量均衡的转发到不同服务器,来实现负载均衡。
PCRE apt install pcre pcre-devel
:这个库包含了perl库,nginx的http模块使用的是pcre来解析正则表达式
zlib apt install zlib zlib-devel
nginx使用zlib对http包进行解压和压缩
./nginx 启动
关闭nginx
./nginx -s stop //先查处pid,然后kill
./nginx -s quit //等nginx处理完毕进程所有请求后,再关闭,称为优雅关闭
./nginx -s reload //重新加载配置文件

虚拟主机的几种方法

把一台物理机划分成多个虚拟的服务器,每个服务器都可以有独立的域名和目录。Nginx代理这些虚拟主机,将请求转发给他们。
有几种配置虚拟主机的方法:
1.基于IP,几乎不用,公网ip费用高
2.基于端口,很少用,比如http://www.sherlock.net //80端口为默认可以省略 http://www.sherlock.net:8080,只需要复制conf/nginx.conf里的server,然后换不同的端口即可。

虚拟主机的配置1.ip一台服务器绑定多个ipifconfig	绑定一个192.168.101.103/sbin/ifconfig eth0:1 192.168.101.103  broadcast 10.117.87.255  netmask 255.255.248.0 up/sbin/route add-host 192.168.101.103 dev eth0:1服务器重启就会失效方式二:/etc/sysconfig/network-scripts/ifcfg-eth0 文件复制一份 命名为ifcfg-eth0:1DEVICE=eth0ONBOOT=yesBOOTPROTO=staticIPADDR=192.168.101.103NETMASK=255.255.248.0服务器重启生效nginx配置一个server就是一个虚拟主机server {listen       80;server_name  192.168.101.100;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}server {listen       80;server_name  192.168.101.103;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}重启nginx  ./nginx -s reload 2.端口添加一个server 就相当于 添加了一个虚拟主机修改listen 改为8077  修改root 指定你虚拟主机的跟目录server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}server {listen       8077;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html2;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}以上两种配置不足:公网ip本省就是一个稀缺资源 端口号 我们上线都要求是80 
基于域名的虚拟主机配置(真实线上使用的方案)

3.基于域名

基于域名的虚拟主机nginx配置conf/nginx.conf修改server_name 域名如果没有域名呢,可以修改window hosts文件 添加对于的ip server {listen       80;server_name  www.51mmr.net;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}server {listen       80;server_name  av.51mmr.net;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html2;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}

配置反向代理

1.nginx的反向代理2.需求,局域网里有两台tomcat服务器,配置一台nginx反向代理他们,公网设备访问以下两个网址,nginx分别代理到这两个服务器上。http://8066.51mmr.com  #域名需要备案http://8077.51mmr.com安装了nginx的服务器:tracking4:公网iP:120.26.247.73内网IP:10.117.85.48安装了tomcat两台服务器tracking2:8066内网IP:10.117.61.252datav:8077内网ip:10.117.183.170nginx配置:=========================================	upstream tomcat8066{#内网ipserver	10.117.61.252:8066;}upstream tomcat8077{#内网ipserver	10.117.183.170:8077;}server {listen       80;server_name  8066.mmr.com;#charset koi8-r;#access_log  logs/host.access.log  main;location / {#反向代理使用了nginx的proxy_pass功能proxy_pass http://tomcat8066; #通过这里找到上面的upstream tomcat8066进而访问到局域网的10.117.61.252:8066 index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}#多一个server就是多一个虚拟服务器server {listen       80;server_name  8077.mmr.com;#charset koi8-r;#access_log  logs/host.access.log  main;location / {proxy_pass http://tomcat8077;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}=========================================

配置负载均衡

其实也就是配置正向代理,代理两台服务器,这两台服务器可以跟nginx是同一局域网的,也可以是不同区域有公网IP的。只要nginx能访问到他们就能代理他们。nginx不关注自己代理的是客户端还是服务器,它其实代理的是它能访问到的ip,把流量转发到这些ip上。

1.需求请求:http://slb.mmr.com需要配置hosts文件安装了nginx的服务器:tracking4:公网iP:120.26.247.73内网IP:10.117.85.48安装了tomcat两台服务器tracking2:8066 16cpu公网IP:120.26.216.148内网IP:10.117.61.252datav:8077 	2cpu公网ip:121.41.19.63内网ip:10.117.183.170server load balance  SLB
2.nginx配置upstream slb{#内网ipserver	10.117.183.170:8077;server  10.117.61.252:8066;}server {listen       80;server_name  slb.hikt.com;#charset koi8-r;#access_log  logs/host.access.log  main;location / {proxy_pass http://slb;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
-----------------------------------------------------
配置权重默认是weight=1 越高处理的请求 越多upstream slb{#内网ipserver	10.117.183.170:8077 weight=1;server  10.117.61.252:8066   weight=4;}定义负载均衡的状态:
1.	upstream myServer {   
2.	
3.	    server 192.168.0.102:9090 down; 
4.	    server 192.168.0.101:8080 weight=2; 
5.	    server 192.168.0.100:6060; 
6.	    server 192.168.0.106:7070 backup; 
7.	}down :表示当前的server暂时不参与负载
weight:默认1 weight越大 负载的权重就越大
backup:所有非backup的机器down或者忙的时候,才会请求backup机器,所以这台机器压力会很小
max_fails:准许请求失败的次数 默认是1 超过设定的失败次数 就会返回proxy_next_upstream 模块定义 的错误
fail_timeout :  max_fails 次 失败后 (服务器挂了,就不要把请求转给他; ,设置的暂停请求时间,这段时间不会有转发过来) 暂停时间;

负载均衡的session解决

问题:如果在一台服务器上登陆了,这台服务器保留了用户名密码,如果下次刷新登陆在另一台服务器上了,那么还需要重新登陆一遍。这就是记录这些用户名和密码的session问题。
解决方案:

1.session管理
1.session sticky根据每次请求 标识 进行转发 保证落到同一台服务器
2.session replicationsession 复制此种方案 在小量的 服务器 可以使用 tomcat 网络开销3.session数据独立redis  expire 失效时间
重写session  移动端开发 token    服务端 cookie 4.开源组件tomcat-redis-manager github

con/nginx.conf配置文件详解

1.nginx 虚拟主机的配置 反向代理 负载均衡2.nginx 配置详细解释#指定的是用户或者用户组 默认nobody
#user  nobody;#开启的线程数 一般跟逻辑cpu核数一致
worker_processes  1;#logs目录是放的日志文件
access.log访问日志 统计uv ip pv 
error.log;错误日志日志级别:debug info warn error crit   
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#进程号 id
#pid        logs/nginx.pid;events {#use epoll  ;select poll #每个进程最大的连接数worker_connections  1024;
}#nginx http服务器的配置 
http {#主模块指令	mime.types是一段代码,可以直接复制在这里,这样引入有利于排版;也可以一个lua脚本引入进来。就会自动执行。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"';#引用日志main#access_log  logs/access.log  main;#开启高效文件传输sendfile        on;#开启防止网络堵塞#tcp_nopush     on;#设置客户端连接保存活的的超时时间#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;#一个server就是一个虚拟主机server {#监听端口号listen       8080;#主机名	server_name  localhost;#设置访问的语言编码#charset koi8-r;#设置虚拟机主机访问的日志文件路径#access_log  logs/host.access.log  main;#设置虚拟主机的  信息location / {#根目录 虚拟主机的根目录 /虚拟路径root   html;# 默认的访问页index  index.html index.htm;}#找不到资源的html#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;#}# HTTPS server   80  443##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;#    }#}

openresty安装和第三方模块配置

1.Openresty其实就是一个nginx +lua    打包 提供了 包括redis mysql http客户端的大量组件
2.Openresty安装https://openresty.org/download/openresty-1.11.2.5.tar.gz
3.准备目录mkdir -p /app/serverscd /app/servers/
4.安装依赖的组件环境yum install -y readline-devel pcre-devel openssl-devel gcc
5.下载wget https://openresty.org/download/openresty-1.11.2.5.tar.gz
6.解压tar -zxvf openresty-1.11.2.5.tar.gz 
7.进入目录cd openresty-1.11.2.5cd bundle/LuaJIT-2.1-20170808/make clean && make && make install安装完 提示执行ln -sf luajit-2.1.0-beta3 /usr/local/bin/luajitcd ..
回到bundle目录
8.下载模块nxg_cache_purge模块在bundle目录下载http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz或者:wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz tar -zxvf 2.3.tar.gz 
9.下载nginx_upstream_check_module模块在bundle目录下载web健康检查wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gztar -zxvf v0.3.0.tar.gz	10.进入主目录 编译安装cd /app/servers/openresty-1.11.2.5./configure --prefix=/app/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2make && make install
11.进入/app/serverscd /app/servers12.查看和启动/app/servers/nginx/sbin/nginx -V/app/servers/nginx/sbin/nginx 	

用lua做一个hello world

1.openresty nginx+lua开发一个helloworld程序1.修改配置文件位置:	/app/servers/nginx/conf/nginx.conf 在http部分添加依赖库lua_package_path "/app/servers/lualib/?.lua;;";  lua_package_cpath "/app/servers/lualib/?.so;;";  2./app/servers/nginx/conf/目录下创建lua.conf文件 内容如下server {listen       8080;server_name  localhost;location /lua {default_type 'text/html';content_by_lua 'ngx.say("hello world by old wang !")';//使用content_by_lua模块,用lua语法ngx.say()}}
3.引入
http {lua_package_path "/app/servers/lualib/?.lua;;";  lua_package_cpath "/app/servers/lualib/?.so;;";  include lua.conf;//引入配置文件,注意不是lua脚本,是include引入普通配置的使用}

参考课程:ngnix实战&openresty;介绍

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/56821.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【AIGC】ChatGPT与人类理解力的共鸣:人机交互中的心智理论(ToM)探索

博客主页: [小ᶻZ࿆] 本文专栏: AIGC | ChatGPT 文章目录 💯前言💯心智理论(Theory of Mind,ToM)心智理论在心理学与神经科学中的重要性心智理论对理解同理心、道德判断和社交技能的重要性结论 💯乌得勒支大学研究对ChatGPT-4…

【C++篇】类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略

文章目录 前言 💬 欢迎讨论:如果你在学习过程中有任何问题或想法,欢迎在评论区留言,我们一起交流学习。你的支持是我继续创作的动力! 👍 点赞、收藏与分享:觉得这篇文章对你有帮助吗&#xff1…

软件设计模式------工厂方法模式

工厂方法模式(Factory Method Pattern),又称工厂模式,也叫虚拟构造器模式(Virtual Constructor Pattern)或多态工厂模式(Polymorphic Pactory Pattern),属于类创建型模式。 我们知道…

WIFI实现透传+接线图

单片机通过TX接WIFI模块的RX将设置的AT代码写入WIFI模块(连接WIFI调为设备模式(有设备,路由,双模等模式)) WIFI模块将响应信号通过TX通过CH340发给PC的RX 通过STC-ISP或安信可串口调试助手查看响应信息 …

Golang | Leetcode Golang题解之第495题提莫攻击

题目: 题解: func findPoisonedDuration(timeSeries []int, duration int) (ans int) {expired : 0for _, t : range timeSeries {if t > expired {ans duration} else {ans t duration - expired}expired t duration}return }

qt QGraphicsEffect详解

一、QGraphicsEffect概述 QGraphicsEffect通过挂接到渲染管道并在源(例如QGraphicsPixmapItem、QWidget)和目标设备(例如QGraphicsView的视口)之间进行操作来更改元素的外观。它允许开发者为图形项添加各种视觉效果,如…

Java网络编程-简单的API调用

Get请求 - 无参数 安装依赖库 首先需要安装一个库&#xff1a; Okhttp3&#xff0c;这是一个非常流行的 HTTP 库&#xff0c;可以简单、快速的实现 HTTP 调用。 安装 Okhttp3 的方式是在 pom.xml 文件中增加依赖&#xff1a; <!-- https://mvnrepository.com/artifact/co…

【算法】哈希表:49.字母异位词分组

目录 1、题目链接 2、题目介绍 3、解法 初始化设定--图解 步骤图解 4、代码 1、题目链接 49. 字母异位词分组 - 力扣&#xff08;LeetCode&#xff09; 2、题目介绍 3、解法 字母异位词的本质是字符相同但排列不同。因此&#xff0c;我们可以对字符串进行排序&#xf…

YOLOv8实战水果识别【数据集+YOLOv8模型+源码+PyQt5界面】

本文采用YOLOv8作为核心算法框架&#xff0c;结合PyQt5构建用户界面&#xff0c;使用Python3进行开发。YOLOv8以其高效的实时检测能力&#xff0c;在多个目标检测任务中展现出卓越性能。本研究针对水果数据集进行训练和优化&#xff0c;该数据集包含丰富的水果图像样本&#xf…

零基础Java第七期:方法的使用

一、方法的概念与使用 1.1. 方法的概念 方法就是一个代码片段. 类似于 C 语言中的 "函数"。方法存在的意义&#xff1a; 是能够模块化的组织代码(当代码规模比较复杂的时候)做到代码被重复使用, 一份代码可以在多个位置使用让代码更好理解更简单直接调用现有方法开…

Solidity基础语法

Solidity的在线编辑器&#xff1a;https://remix.ethereum.org/ 一、合约结构 1、SPDX许可标识&#xff1a;指定代码的开源许可 2、pragma指令&#xff1a;声明Solidity版本 3、导入语句&#xff1a;引入其他合约或库 4、合约声明&#xff1a;使用contract关键字 5、状态变量&…

直流和交流变频压缩机工作原理

直流变频压缩机工作原理&#xff1a; 压缩机定子产生旋转磁场与转子永磁磁场直接作用&#xff0c;实现压缩机运转。转子是永磁体&#xff0c;没有线圈/绕组&#xff0c;无需外部供电&#xff0c;不产生电能损耗&#xff0c;效率高、节能&#xff1b;直流变频压缩机属于同步控制…

uboot中mmc是使用

进入uboot的界面后 mmc命令 mmc list #查看有哪些可用的mmc设备 mmc dev 0 #切换到mmc的0设备&#xff0c;一般是指SD卡 ls mmc 0:1 #查看mmc 0设备&#xff08;sd卡&#xff09;中1分区保存的信息 ls mmc 0:2 #查看mmc 0设备&#xff08;sd卡&#xff09;中2分区保存的信…

Leetcode 1129. 颜色交替的最短路径

1.题目基本信息 1.1.题目描述 给定一个整数 n&#xff0c;即有向图中的节点数&#xff0c;其中节点标记为 0 到 n – 1。图中的每条边为红色或者蓝色&#xff0c;并且可能存在自环或平行边。 给定两个数组 redEdges 和 blueEdges&#xff0c;其中&#xff1a; redEdges[i] …

顺序表算法题【不一样的解法!】

本章概述 算法题1算法题2算法题3彩蛋时刻&#xff01;&#xff01;&#xff01; 算法题1 力扣&#xff1a;移除元素 我们先来看这个题目的要求描述&#xff1a; 把与val相同数值的元素移除掉&#xff0c;忽略元素的相对位置变化&#xff0c;然后返回剩下与val值不同的元素个数…

基于SpringBoot+Vue+uniapp的涪陵区特色农产品交易系统的详细设计和实现(源码+lw+部署文档+讲解等)

详细视频演示 请联系我获取更详细的视频演示 项目运行截图 技术框架 后端采用SpringBoot框架 Spring Boot 是一个用于快速开发基于 Spring 框架的应用程序的开源框架。它采用约定大于配置的理念&#xff0c;提供了一套默认的配置&#xff0c;让开发者可以更专注于业务逻辑而不…

pytest中@pytest.fixture常用顺序function

ytest中pytest.fixture用法讲解 1、测试函数开始之前2、执行测试函数&#xff1a;3、测试函数结束后&#xff1a; 备注&#xff1a;内容来自chatGPT 在 pytest 中&#xff0c;pytest.fixture 是一个非常强大的功能&#xff0c;用于设置测试所需的环境和状态。它可以通过 scope…

AP上线的那些事儿(1)capwap建立过程、设备初始化以及二层上线

1、了解FITAP与AC的建立过程 之前我们已经知道了FATAP与FIT是一对双胞胎一样的兄弟&#xff0c;FAT哥哥能够直接独立使用当AP桥接、路由器等&#xff0c;而弟弟FIT则比较薄弱&#xff0c;独自发挥不出功效&#xff0c;需要一位师傅&#xff08;AC&#xff09;来带领&#xff0c…

ssm配置模式

新版 用Java类&#xff0c;全注解demo案例 1. AppConfig.java (Spring主配置类)package com.example.config;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.cont…

MATLAB实现AM调制解调

1.基本概念 1.1 AM调制原理 调幅就是使载波的振幅随调制信号的变化规律而变化。基带信号m(t)与直流分量A0相加&#xff0c;然后和高频载波相乘实现AM信号的调制&#xff0c;如图1所示。 1.2 AM解调原理 AM信号经过信道传输&#xff0c;引入噪声后&#xff0c;再和载波相乘&…