当前位置: 首页 > news >正文

6-Ngnix配置反向代理

1.前提

虚拟机能连接外网

仿真http应用需在本虚拟机启用(原因:只有一台虚拟机做测试)

http_8080和http_8081要启用(http测试应用)

[root@cent79-2 ~]# ls -l http_*
-rwxr-xr-x 1 root root 6391676 Jul 19 13:39 http_8080
-rwxr-xr-x 1 root root 6391676 Jul 19 13:39 http_8081
[root@cent79-2 ~]# ./http_8080 &
[1] 1490
[root@cent79-2 ~]# ./http_8081 &
[2] 1496
[root@cent79-2 ~]# netstat -antulp |grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      1490/./http_8080    
[root@cent79-2 ~]# netstat -antulp |grep 8081
tcp6       0      0 :::8081                 :::*                    LISTEN      1496/./http_8081    
[root@cent79-2 ~]# curl 192.168.10.156:8080
{"text":"I am one!"}
[root@cent79-2 ~]# curl 192.168.10.156:8081
{"text":"I am two!"}
[root@cent79-2 ~]# 

2.Ngnix配置反向代理

2.1.Nginx配置单个反向代理

1>.编辑nginx配置文件nginx.conf,添加反向代理配置

user  nginx  nginx;

worker_processes  2;

worker_rlimit_nofile 1024;

error_log  /var/log/nginx/error.log notice;

pid        /var/run/nginx.pid;

events {

    use epoll;

    worker_connections  1024;

    multi_accept  on;

}

http {

    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;

    tcp_nopush     on;

    autoindex    on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {

    listen  80;

    server_name 192.168.10.156;

    location / {

      proxy_pass http://www.baidu.com;

    }

    }

}

2>.nginx语法验证

命令:

nginx -t

[root@cent79-2 ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@cent79-2 ~]#

3>.重启nginx

命令:

systemctl restart nginx

systemctl status nginx

[root@cent79-2 ~]# systemctl restart nginx

[root@cent79-2 ~]# systemctl status nginx

● nginx.service - nginx - high performance web server

   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)

   Active: active (running) since Thu 2023-07-20 20:16:16 CST; 3s ago

     Docs: http://nginx.org/en/docs/

  Process: 2504 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)

  Process: 2509 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)

 Main PID: 2510 (nginx)

   CGroup: /system.slice/nginx.service

           ├─2510 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

           ├─2511 nginx: worker process

           └─2512 nginx: worker process

Jul 20 20:16:15 cent79-2 systemd[1]: Starting nginx - high performance web server...

Jul 20 20:16:16 cent79-2 systemd[1]: Started nginx - high performance web server.

[root@cent79-2 ~]#

4>.反向代理验证

地址:

http://192.168.10.156

转向为:

2.2.Nginx配置反向代理集群

1>.编辑nginx配置文件nginx.conf,添加反向代理配置

user  nginx  nginx;

worker_processes  2;

worker_rlimit_nofile 1024;

error_log  /var/log/nginx/error.log notice;

pid        /var/run/nginx.pid;

events {

    use epoll;

    worker_connections  1024;

    multi_accept  on;

}

http {

    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;

    tcp_nopush     on;

    autoindex    on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

  

    upstream www.ztj.com {

      server 192.168.10.156:8080 weight=1 max_fails=2 fail_timeout=15s;

      server 192.168.10.156:8081 weight=1 max_fails=2 fail_timeout=15s;

    }

    server {

    listen  80;

    server_name 192.168.10.156;

    location / {

     proxy_pass http://www.ztj.com;

    }

    }

}

2>.nginx语法验证

命令:

nginx -t

[root@cent79-2 ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@cent79-2 ~]#

3>.重启nginx

命令:

systemctl restart nginx

systemctl status nginx

[root@cent79-2 ~]# systemctl restart nginx

[root@cent79-2 ~]# systemctl status nginx

● nginx.service - nginx - high performance web server

   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)

   Active: active (running) since Thu 2023-07-20 20:32:50 CST; 4s ago

     Docs: http://nginx.org/en/docs/

  Process: 2673 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)

  Process: 2678 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)

 Main PID: 2679 (nginx)

   CGroup: /system.slice/nginx.service

           ├─2679 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

           ├─2680 nginx: worker process

           └─2681 nginx: worker process

Jul 20 20:32:50 cent79-2 systemd[1]: Stopped nginx - high performance web server.

Jul 20 20:32:50 cent79-2 systemd[1]: Starting nginx - high performance web server...

Jul 20 20:32:50 cent79-2 systemd[1]: Started nginx - high performance web server.

[root@cent79-2 ~]#

4>.反向代理集群验证

命令:curl 192.168.10.156

[root@cent79-2 ~]# curl 192.168.10.156

{"text":"I am one!"}

[root@cent79-2 ~]# curl 192.168.10.156

{"text":"I am two!"}

[root@cent79-2 ~]#

2.3.Nginx配置基于context的反向代理(重点)

1>.编辑nginx配置文件nginx.conf,添加反向代理配置

user  nginx  nginx;

worker_processes  2;

worker_rlimit_nofile 1024;

error_log  /var/log/nginx/error.log notice;

pid        /var/run/nginx.pid;

events {

    use epoll;

    worker_connections  1024;

    multi_accept  on;

}

http {

    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;

    tcp_nopush     on;

    autoindex    on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

  

    server {

    listen  80;

    server_name 192.168.10.156;

    location / {

     proxy_pass http://www.baidu.com;

    }

   

    location /root {

      proxy_pass http://127.0.0.1:8080;

        location /root/api {

         proxy_pass http://127.0.0.1:8081;

        }

      }

    }

}

2>.nginx语法验证

命令:

nginx -t

[root@cent79-2 ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@cent79-2 ~]#

3>.重启nginx

命令:

systemctl restart nginx

systemctl status nginx

[root@cent79-2 ~]# systemctl restart nginx

[root@cent79-2 ~]# systemctl status nginx

● nginx.service - nginx - high performance web server

   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)

   Active: active (running) since Thu 2023-07-20 20:39:33 CST; 2s ago

     Docs: http://nginx.org/en/docs/

  Process: 2713 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)

  Process: 2718 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)

 Main PID: 2719 (nginx)

   CGroup: /system.slice/nginx.service

           ├─2719 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

           ├─2720 nginx: worker process

           └─2721 nginx: worker process

Jul 20 20:39:33 cent79-2 systemd[1]: Starting nginx - high performance web server...

Jul 20 20:39:33 cent79-2 systemd[1]: Started nginx - high performance web server.

[root@cent79-2 ~]#

4>.反向代理context验证

http://192..168.10.156

http://192..168.10.156/root

http://192..168.10.156/root/api 

相关文章:

6-Ngnix配置反向代理

1.前提 虚拟机能连接外网 仿真http应用需在本虚拟机启用(原因:只有一台虚拟机做测试) http_8080和http_8081要启用(http测试应用) [rootcent79-2 ~]# ls -l http_* -rwxr-xr-x 1 root root 6391676 Jul 19 13:39 http_8080 -rwxr-xr-x 1 …...

构建 LVS-DR 群集、配置nginx负载均衡。

目录 一、基于 CentOS 7 构建 LVS-DR 群集 1、准备四台虚拟机 2、配置负载调度器(192.168.2.130) 3、部署共享存储(192.168.2.133) 4、配置两个Web服务器(192.168.2.131、192.168.2.132) 测试集群 二…...

【UE4的垃圾回收】

UE4的垃圾回收 1 UObjects及子类1.1 UObjects类包含UObjects成员(UPROPERTY)1.2 UObjects类包含非UObjects成员 2 非UObject及子类2.1 非UObjects类包含UObjects成员12.2 非UObjects类包含UObjects成员22.3 非UOjbects类包含非UObjects成员 3 UStructs4 …...

nginx负载均衡的几种配置方式介绍

一.负载均衡含义简介 二.nginx负载均衡配置方式 准备三台设备: 2.190均衡服务器,2.191web服务器1,2.160web服务器2,三台设备均安装nginx,两台web服务器均有网页内容 1.一般轮询负载均衡 (1&#xff09…...

uniapp发布插件显示components/xxx文件没找到,插件格式不正确

uniapp发布插件显示components/xxx文件没找到,插件格式不正确 将插件文件这样一起选中,然后右键压缩成zip文件,而不是外层文件压缩...

Kubernetes(K8s)入门

一、Kubernetes是什么 Kubernetes是什么? 首先,它是一个全新的基于容器技术的分布式架构领先方案。这个方案虽然还很新,但它是谷歌十几年以来大规模应用容器技术的经验积累和升华的一个重要成果。确切地说,Kubernetes是谷歌严格保密十几年的…...

[前端系列第3弹]JS入门教程:从零开始学习JavaScript

本文将带领大家,从零开始学习JavaScript,fighting~ 目录 一、JavaScript简介 二、变量和数据类型 三、注释和分号 四、算术运算符 五、表达式和语句 六、代码块和作用域 七、函数(最重要) 一、JavaScript简介 JavaScript&…...

html 计算器界面

其他链接&#xff1a; https://www.freecodecamp.org/news/how-to-build-an-html-calculator-app-from-scratch-using-javascript-4454b8714b98/ https://codepen.io/pen/tour/welcome/start 下面展示一些 内联代码片。 <!DOCTYPE html> <html lang"en">…...

性能测试工具——LoadRunner(1)

一、LoadRunner三大组件 1.1每个组件是干什么的 VUG&#xff1a;录制脚本(编写脚本) Controller&#xff1a;设计场景&#xff0c;运行场景 Analysis&#xff1a;产生性能测试报告 1.2三大组件之间的关系 二、LoadRunner脚本录制 2.1了解WebTours系统 启动WebTours&#xf…...

安科瑞物联网表在虚拟电厂的应用

安科瑞 崔丽洁 应用场景 一般应用于控制中心 功能 能计量当前组合有功电能&#xff0c;正向有功电能&#xff0c;反向有功电能&#xff0c;正向无功电能&#xff0c;反向无功电能&#xff1b; ADW300支持RS485通讯、LORA通讯、NB、4G及Wifi通讯&#xff1b; 三套时段表,一年可以…...

XSS和CSRF

web安全策略和同源策略的意义 如果登陆了一个网站&#xff0c;不小心又打开另一个恶意网站&#xff0c;如果没有安全策略&#xff0c;则他可以对已登录的网站进行任意的dom操作、伪造接口请求等&#xff0c;因此安全策略是必要的&#xff1b; 浏览器的同源策略限制了非同源的域…...

2.物联网LWIP网络

一。创建工程 1.Cubemx创建工程 &#xff08;1&#xff09;操作系统的时钟配置 &#xff08;2&#xff09;配置ETH 注意&#xff1a;根据底板原理图&#xff0c;不是核心板原理图 &#xff08;3&#xff09;配置USART1串口&#xff0c;配置为异步通信 注意&#xff1a;配置结…...

tomcat多实例与动静分离

实验&#xff1a;在一台虚拟机上配置多台tomcat 1.配置 tomcat 环境变量 vim /etc/profile.d/tomcat.sh source /etc/profile.d/tomcat.sh 2.修改 tomcat2 中的 server.xml 文件&#xff0c;要求各 tomcat 实例配置不能有重复的端口号 vim /usr/local/tomcat/tomcat2/conf/…...

K8S下SpringCloud应用无损下线

废话不多说直接上代码&#xff0c;一种2个步骤 步骤一&#xff1a; 添加以下代码到SpringCloud应用中 import cn.hutool.extra.spring.SpringUtil; import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration; import lombok.RequiredArgsConstructor; import lo…...

CEC2013(MATLAB):遗传算法(Genetic Algorithm,GA)求解CEC2013的28个函数

一、遗传算法GA 遗传算法&#xff08;Genetic Algorithm&#xff0c;GA&#xff09;起源于对生物系统所进行的计算机模拟研究&#xff0c;是一种随机全局搜索优化方法&#xff0c;它模拟了自然选择和遗传中发生的复制、交叉(crossover)和变异(mutation)等现象&#xff0c;从任…...

Linux tar包安装 Prometheus 和 Grafana

0. 介绍 用tar包的方式安装 Prometheus 和 Grafana Prometheus:开源的监控方案Grafana:将Prometheus的数据可视化平台 1. Prometheus 1. 下载 与 解压 官网下载: https://prometheus.io/download/#prometheus上传至机器解压命令:tar -xzf prometheus-*.tar.gz 2. 启动与暂…...

新一代分布式融合存储,数据场景All In One

1、摘要 2023年5月11日&#xff0c;浪潮信息全国巡展广州站正式启航。会上&#xff0c;重磅发布新一代分布式融合存储AS13000G7&#xff0c;其采用极致融合架构设计理念&#xff0c;实现同一套存储满足四种非结构化数据的“All In One”高效融合&#xff0c;数据存力提升300%&a…...

CGroupAndroid实践篇】三、Android CGroup控制组初始化

前面已经提到,android在init阶段,通过init trigger来触发控制组节点的创建,包括foreground,background,top-app,rt,system,dex2opt,system-background,nnapi-hal,camera-daemon,restricted等。 我们来看下android在init.rc中,是如何创建这些控制组节点的,如下:…...

lscpu的各个参数是什么意思?

$ lscpu Architecture: x86_64 #架构 CPU op-mode(s): 32-bit, 64-bit #运行方式 Byte Order: Little Endian #字节顺序 CPU(s): 96 #逻辑cpu数 On-line CPU(s) list: 0-95 #在线cpu Thread(s) per core: 2 #每个核包含线程…...

Linux学习————redis服务

目录 一、redis主从服务 一、redis主从服务概念 二、redis主从服务作用 三、缺点 四、主从复制流程 五、搭建主从服务 配置基础环境 下载epel源&#xff0c;下载redis​编辑 二、哨兵模式 一、概念 二、作用 三、缺点 四、结构 五、搭建 修改哨兵配置文件 启动服务…...

Swift 协议扩展精进之路:解决 CoreData 托管实体子类的类型不匹配问题(下)

概述 在 Swift 开发语言中&#xff0c;各位秃头小码农们可以充分利用语法本身所带来的便利去劈荆斩棘。我们还可以恣意利用泛型、协议关联类型和协议扩展来进一步简化和优化我们复杂的代码需求。 不过&#xff0c;在涉及到多个子类派生于基类进行多态模拟的场景下&#xff0c;…...

深入浅出:JavaScript 中的 `window.crypto.getRandomValues()` 方法

深入浅出&#xff1a;JavaScript 中的 window.crypto.getRandomValues() 方法 在现代 Web 开发中&#xff0c;随机数的生成看似简单&#xff0c;却隐藏着许多玄机。无论是生成密码、加密密钥&#xff0c;还是创建安全令牌&#xff0c;随机数的质量直接关系到系统的安全性。Jav…...

前端导出带有合并单元格的列表

// 导出async function exportExcel(fileName "共识调整.xlsx") {// 所有数据const exportData await getAllMainData();// 表头内容let fitstTitleList [];const secondTitleList [];allColumns.value.forEach(column > {if (!column.children) {fitstTitleL…...

HTML 列表、表格、表单

1 列表标签 作用&#xff1a;布局内容排列整齐的区域 列表分类&#xff1a;无序列表、有序列表、定义列表。 例如&#xff1a; 1.1 无序列表 标签&#xff1a;ul 嵌套 li&#xff0c;ul是无序列表&#xff0c;li是列表条目。 注意事项&#xff1a; ul 标签里面只能包裹 li…...

测试markdown--肇兴

day1&#xff1a; 1、去程&#xff1a;7:04 --11:32高铁 高铁右转上售票大厅2楼&#xff0c;穿过候车厅下一楼&#xff0c;上大巴车 &#xffe5;10/人 **2、到达&#xff1a;**12点多到达寨子&#xff0c;买门票&#xff0c;美团/抖音&#xff1a;&#xffe5;78人 3、中饭&a…...

跨链模式:多链互操作架构与性能扩展方案

跨链模式&#xff1a;多链互操作架构与性能扩展方案 ——构建下一代区块链互联网的技术基石 一、跨链架构的核心范式演进 1. 分层协议栈&#xff1a;模块化解耦设计 现代跨链系统采用分层协议栈实现灵活扩展&#xff08;H2Cross架构&#xff09;&#xff1a; 适配层&#xf…...

Nginx server_name 配置说明

Nginx 是一个高性能的反向代理和负载均衡服务器&#xff0c;其核心配置之一是 server 块中的 server_name 指令。server_name 决定了 Nginx 如何根据客户端请求的 Host 头匹配对应的虚拟主机&#xff08;Virtual Host&#xff09;。 1. 简介 Nginx 使用 server_name 指令来确定…...

什么?连接服务器也能可视化显示界面?:基于X11 Forwarding + CentOS + MobaXterm实战指南

文章目录 什么是X11?环境准备实战步骤1️⃣ 服务器端配置(CentOS)2️⃣ 客户端配置(MobaXterm)3️⃣ 验证X11 Forwarding4️⃣ 运行自定义GUI程序(Python示例)5️⃣ 成功效果![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/55aefaea8a9f477e86d065227851fe3d.pn…...

【JavaWeb】Docker项目部署

引言 之前学习了Linux操作系统的常见命令&#xff0c;在Linux上安装软件&#xff0c;以及如何在Linux上部署一个单体项目&#xff0c;大多数同学都会有相同的感受&#xff0c;那就是麻烦。 核心体现在三点&#xff1a; 命令太多了&#xff0c;记不住 软件安装包名字复杂&…...

【碎碎念】宝可梦 Mesh GO : 基于MESH网络的口袋妖怪 宝可梦GO游戏自组网系统

目录 游戏说明《宝可梦 Mesh GO》 —— 局域宝可梦探索Pokmon GO 类游戏核心理念应用场景Mesh 特性 宝可梦玩法融合设计游戏构想要素1. 地图探索&#xff08;基于物理空间 广播范围&#xff09;2. 野生宝可梦生成与广播3. 对战系统4. 道具与通信5. 延伸玩法 安全性设计 技术选…...