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 

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

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

PAT1044 Shopping in Mars

个人学习记录,代码难免不尽人意。 做了这么多题难得本题不看答案一遍过,很是激动。 Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the pa…

【数据结构】反转链表、链表的中间节点、链表的回文结构(单链表OJ题)

正如标题所说,本文会图文详细解析三道单链表OJ题,分别为: 反转链表 (简单) 链表的中间节点 (简单) 链表的回文结构 (较难) 把他们放在一起讲的原因是: 反转链…

QGIS二次开发六:VS不借助QT插件创建UI界面

上一篇博客我们说了在VS中如何使用QT插件来创建UI界面,但是我们二次开发QGIS的第一篇博客就说了,最好使用OSGeo4W中自动下载的QT进行QGIS二次开发,这样兼容性是最好的,那么该如何在VS中不使用外部安装的QT以及QT的VS插件情况下进行…

提取有像素的掩码和原图

有些数据集给的掩码是全黑图片,需要将全黑的掩码剔除,保留有标签的掩码。 DDR-dataset 眼底图像处理 from PIL import Image import cv2 import osdef extract_mask_and_original(mask_path, original_path, output_folder):# 读取黑白掩码图片和同名原…

【Java】智慧工地云平台源码-支持私有化部署+硬件设备

智慧工地硬件设备包括:AI识别一体机、智能广播音响、标养箱、塔机黑匣子、升降机黑匣子、吊钩追踪控制设备、扬尘监测设备、喷淋设备。 1.什么是AI危险源识别 AI危险源识别是指基于智能视频分析技术,对视频图像信息进行自动分析识别,以实时监…

photoshop指定打开psd文件方式

1、打开考生文件夹下的psd文件 2、右击这个psd——打开——或打开方式(选其他默认程序) 3、路径一般在 C:\Program Files\Adobe 或者是C:\Program Files(x86)\Adobe 下,打开后找到Photoshop.exe后——打开 4、点击勾选…

C++文件类(整理自C语言中文网-全)

C文件类(文件流类)及用法详解 《C输入输出流》一章中讲过,重定向后的 cin 和 cout 可分别用于读取文件中的数据和向文件中写入数据。除此之外,C 标准库中还专门提供了 3 个类用于实现文件操作,它们统称为文件流类&…

C语言案例 分数列求和-11

题目:有一分数列:2 / 1,3 / 2,5 / 3,8 / 5,13 / 8,21 / 13 …求出这个数列的前20项之和。 程序分析 这是一个典型的分数列数学逻辑题,考究这类题目是需要从已知的条件中找到它们的分布规律 我们把前6荐的分子与分母分别排列出来,…

快速使用公网远程访问内网群晖NAS 7.X版 【内网穿透】

公网远程访问内网群晖NAS 7.X版 【内网穿透】 文章目录 公网远程访问内网群晖NAS 7.X版 【内网穿透】前言1. 在群晖控制面板找到“终端机和SNMP”2. 建立一条连接公网数据隧道3. 获取公网访问内网群晖NAS的数据隧道入口 前言 群晖NAS作为应用较为广泛的小型数据存储中心&#…

ⅰsee是什么意思_see是什么意思

展开全部 v. 看见,明白,e68a84e8a2ad3231313335323631343130323136353331333433626539了解,经历,设想 n. 主教教区,主角权限 用法; 1.see的基本意思是指一般视觉意义上的“看见”,也可指有意识地“观察”,引…

DBeaver下载地址

数据库IDE工具DBeaver,开源,免费,好用。 DBeaver Community Free Universal Database Tool 所有版本都有: Archive Files | DBeaver Community 如果想下载32位,最后一个版本是6.0.0

dbeaver 下载

1.下载安装 在官网(dbeaver)进行下载。 2.快捷键 1.ctrlenter 执行sql 2.ctrl\ 执行sql,保留之前窗口结果 3.ctrlshift↑ 向上复制一行 4.ctrlshift↓ 向下复制一行 5.ctrlaltF 对sql语句进行格式化,对于很长的sql语句很有用 6.ctrld 删…

大都会人寿线下培训第三天回顾

今天是大都会人寿培训的第三天,早上每个人都发表了自己写的爱的书信,由于我是个理性的人,一直自以为没多少感性的细胞,但是轮到我发言时,我却有想哭的感觉,可能是自女儿出生8年来自己时常不在她身边而感觉的…

中国人寿保险研发中心2021校招开始啦!

感兴趣的同学可以把简历发送至邮箱 chenhongyune-chinalife.com 往期推荐 Java 15 转正了,国内几大互联网公司均有贡献,其中腾讯最为突出! Spring Boot 缓存应用实践 赠书:一本书带你吃透Nginx应用与运维 超全的 Linux Shell 文本…

大都会人寿线下培训第五天回顾

参加培训后,突然生活变得超级充实,每天培训,回来写作业,然后一周就过去了… 今天考试,昨晚突击了一遍,实在太困,想着今早考试前再看一遍得了,结果早上去晚了,没时间复习…

健康人寿保险服务平台

开发工具(eclipse/idea/vscode等): 数据库(sqlite/mysql/sqlserver等): 功能模块(请用文字描述,至少200字):

中国平安真牛,把中国人寿给替了!!!!

刚才在Google搜中国人寿,看见中国人寿就点了,靠,进入中国平安了。 没想到,Google也能被收买!

01- 机器学习经典流程 (中国人寿保费项目) (项目一)

删除特征: data data.drop([region, sex], axis1)特征数据调整: data.apply( ) # 体重指数,离散化转换,体重两种情况:标准、肥胖 def convert(df,bmi):df[bmi] fat if df[bmi] > bmi else standardreturn df data data.apply(convert,…

中国人寿旗下多地国寿金融中心吸引新机构入驻

写字楼市场需求与经济增长高度相关,当经济活动日益恢复,写字楼市场逐步迎来复苏。戴德梁行的《2020中国商业地产投资意向调查报告》显示,大多数投资人十分看好中国商业地产市场的长期发展,有82%的受访者表示计划投资写字楼和/或商…

Java集合知识回顾:从分类到工具类,掌握精髓

文章目录 1. 集合的分类2. Collection 接口3. Map 接口4. 泛型5. Collections 工具类总结 在Java编程世界中,集合是一项极为重要的知识,为我们的程序设计提供了强大的数据结构和处理手段。在本篇文章中,我们将回顾集合的分类以及相关的重要概…