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没变 而页面发生该变

相关推荐
chen9459 小时前
mysql 3节点mgr集群部署
运维·后端
LH_R10 小时前
OneTerm开源堡垒机实战(三):功能扩展与效率提升
运维·后端·安全
dessler10 小时前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
少妇的美梦1 天前
logstash教程
运维
chen9451 天前
k8s集群部署vector日志采集器
运维
chen9451 天前
aws ec2部署harbor,使用s3存储
运维
東雪蓮☆1 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_264220891 天前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs
乌萨奇也要立志学C++1 天前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
雨落Liy1 天前
Nginx 从入门到进阶:反向代理、负载均衡与高性能实战指南
运维·nginx·负载均衡