nginx反向代理

简介

Nginx反向代理是一种服务器架构模式,它允许Nginx服务器接收客户端的请求,然后将这些请求转发到上游服务器(例如应用服务器)进行处理,并将处理后的响应返回给客户端。在这个过程中,Nginx充当了客户端和上游服务器之间的中介。

使用Nginx作为反向代理服务器的好处包括:

  1. 负载均衡:Nginx可以配置为将请求分发到多个上游服务器,以实现负载均衡。这有助于平衡服务器负载,提高系统的可扩展性和可靠性。

  2. 缓存:Nginx支持缓存功能,可以缓存上游服务器的响应,从而减少对上游服务器的请求次数,提高响应速度。

  3. 安全性:Nginx提供了多种安全特性,如SSL/TLS加密、访问控制、限流等,可以保护应用服务器免受恶意攻击。

  4. 灵活性:Nginx的配置非常灵活,可以根据需求进行定制,支持各种复杂的路由规则和请求处理逻辑。

环境

Redhat 9.2

代理服务器:192.168.200.133 proxy

后端服务器:192.168.200.128 nginx

步骤

主机更名

复制代码
[root@lnmp ~]# hostnamectl  hostname proxy
[root@lnmp ~]# bash
[root@proxy ~]# 

[root@savle ~]# hostnamectl  hostname nginx
[root@savle ~]# bash
[root@nginx ~]# 

安装nginx(yum安装)

复制代码
[root@proxy ~]# yum -y install  nginx*
正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:0:00:10 前,执行于 2024年04月24日 星期三 15时40分43秒。
软件包 nginx-1:1.20.1-14.el9.x86_64 已安装。
软件包 nginx-all-modules-1:1.20.1-14.el9.noarch 已安装。
软件包 nginx-core-1:1.20.1-14.el9.x86_64 已安装。
软件包 nginx-filesystem-1:1.20.1-14.el9.noarch 已安装。
软件包 nginx-mod-http-image-filter-1:1.20.1-14.el9.x86_64 已安装。
软件包 nginx-mod-http-perl-1:1.20.1-14.el9.x86_64 已安装。
软件包 nginx-mod-http-xslt-filter-1:1.20.1-14.el9.x86_64 已安装。
软件包 nginx-mod-mail-1:1.20.1-14.el9.x86_64 已安装。
软件包 nginx-mod-stream-1:1.20.1-14.el9.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
[root@proxy ~]# 


[root@nginx ~]# yum -y install  nginx*
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Last metadata expiration check: 0:00:37 ago on Wed 24 Apr 2024 03:34:21 PM CST.
Package nginx-1:1.20.1-14.el9.x86_64 is already installed.
Package nginx-all-modules-1:1.20.1-14.el9.noarch is already installed.
Package nginx-core-1:1.20.1-14.el9.x86_64 is already installed.
Package nginx-filesystem-1:1.20.1-14.el9.noarch is already installed.
Package nginx-mod-http-image-filter-1:1.20.1-14.el9.x86_64 is already installed.
Package nginx-mod-http-perl-1:1.20.1-14.el9.x86_64 is already installed.
Package nginx-mod-http-xslt-filter-1:1.20.1-14.el9.x86_64 is already installed.
Package nginx-mod-mail-1:1.20.1-14.el9.x86_64 is already installed.
Package nginx-mod-stream-1:1.20.1-14.el9.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@nginx ~]# 

关闭防火墙和selinux

复制代码
[root@proxy ~]# systemctl  stop firewalld.service 
[root@proxy ~]# setenforce  0
[root@proxy ~]#

[root@nginx ~]# systemctl stop  firewalld
[root@nginx ~]# setenforce  0
[root@nginx ~]# 

重启nginx

复制代码
[root@proxy ~]# systemctl  restart  nginx.service 
[root@proxy ~]# systemctl  enable nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@proxy ~]# 


[root@nginx ~]# systemctl  restart  nginx.service 
[root@nginx ~]# systemctl  enable  nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx ~]# 

更改后端服务器的网页内容以示区分

复制代码
[root@nginx ~]# vim  /usr/share/nginx/html/index.html 
<html>

        <title>测试专用</title>
        <head> 
                 <meta charset="utf-8">
        </head>

        <body>hello word!后端服务器</body>
</html>
[root@nginx ~]# systemctl  restart nginx.service 

在代理服务里新建一个新的配置文件添加上后端服务器IP地址

//注意格式

复制代码
[root@proxy ~]# vim /etc/nginx/conf.d/proxy.conf
server {
    listen 80;
    server_name your_domai;

    location / {
        proxy_pass http://192.168.200.128;    //后端服务的IP
    }
}                                                                               
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@proxy ~]# systemctl  restart  nginx.service                                                             

回到浏览器刷新

IP没变 而页面发生该变

相关推荐
我要见SA姐12 小时前
告别 Copilot?Codex 本地化部署指南
运维·数据库·机器学习·oracle·回归
Elastic 中国社区官方博客3 小时前
列式存储并不等同于列式数据库。Columnar 模式为 Elasticsearch 带来了什么
大数据·运维·数据库·elasticsearch·搜索引擎
fengkai45453 小时前
八、Docker详解4-5
运维·docker
智恒百亿4 小时前
RTX 5090 八卡服务器 vs RTX PRO 6000 整机:AI 推理、微调与渲染选型部署指南
运维·服务器·rtx5090
蓝胖的四次元口袋6 小时前
Docker Compose多容器编排与Nginx集群实战
运维·nginx·docker compose
懂软件的胡子个哥6 小时前
微信 API 消息回调怎么设计,才能避免丢消息和重复处理
运维·微信·架构·wechatapi·个人微信号二次开发
吉甫作诵6 小时前
Redis 常用命令大全:11 大类命令速查手册
运维·数据库·redis·缓存·nosql
师傅别念了7 小时前
Nginx 反向代理配置指南,看完这篇就够了
nginx
Acrellea8 小时前
告别粗放式能耗管理!安科瑞EIOT平台助力产业园区智慧低碳运营
运维·安全·能源
Databuff8 小时前
五款主流 SSH 免费工具介绍
运维·ssh·运维开发