CentOS如何配置Nginx反向代理
一、反向代理及演示环境描述
1、反向代理
反向代理是一种代理服务器,通过代表客户端从一个或多个服务器检索资源。将这些资源重新发送给客户端,就好像它们从Web服务器本身返回一样。与正向代理相反,正向代理是与其关联的客户端联系任何服务器的中介,反向代理是任何客户端与其关联的服务器进行联系的中介。
有关正向代理可参考:基于CentOS 7配置Nginx正向代理
2、本演示中的几个服务器

二、常规反向代理配置
1、后端服务器配置(Apache)
后端Apache服务器主机名及IP
# hostname centos7-web.example.com# more /etc/redhat-release CentOS Linux release 7.2.1511 (Core)# ip addr|grep inet|grep global inet 172.24.8.128/24 brd 172.24.8.255 scope global eno16777728# systemctl start httpd.service# echo "This is a httpd test page.">/var/www/html/index.html# curl http://localhost This is a httpd test page.
2、前端Nginx反向代理服务器配置
前端Nginx服务器主机名及IP
# hostname centos7-router # more /etc/redhat-release CentOS Linux release 7.2.1511 (Core) # ip addr |grep inet|grep global inet 172.24.8.254/24 brd 172.24.8.255 scope global eno16777728 inet 192.168.1.175/24 brd 192.168.1.255 scope global dynamic eno33554960
Nginx版本
# nginx -V nginx version: nginx/1.10.2
添加一个新的配置文件用作反向代理
# vim /etc/nginx/conf.d/reverse_proxy.conf server { listen 8090; server_name localhost; location / { proxy_pass http://172.24.8.128; ###反向代理核心指令 proxy_buffers 256 4k; proxy_max_temp_file_size 0; proxy_connect_timeout 30; proxy_cache_valid 200 302 10m; proxy_cache_valid 301 1h; proxy_cache_valid any 1m; } }# systemctl reload nginx# ss -nltp|grep nginx|grep 8090LISTEN 0 128 *:8090 *:* users:(("nginx",pid=78023,fd=8),("nginx",pid=78021,fd=8))# curl http://localhost:8090 ##基于本地测试This is a httpd test page.
查看Apache服务器日志
# more /var/log/httpd/access_log ##请求IP地址为172.24.8.254,当从其他机器请求时也是172.24.8.254这个IP172.24.8.254 - - [30/Oct/2017:14:02:38 +0800] "GET / HTTP/1.0" 200 27 "-" "curl/7.29.0"
3、反向代理服务器及后端服务器日志格式设置
为Nginx服务器添加proxy_set_header指令,修改后如下
# grep proxy_set_header -B2 /etc/nginx/conf.d/reverse_proxy.conf location / { proxy_pass http://172.24.8.128; proxy_set_header X-Real-IP $remote_addr; }# systemctl reload nginx.service
后端服务器Apache日志格式设置
# vim /etc/http/conf/httpd.conf# LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined #注释此行,添加下一行 LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined #关键描述 {X-Real-IP}i# ip addr|grep inet|grep global #从1.132主机访问 inet 192.168.1.244/24 brd 192.168.1.255 scope global eth0# curl http://192.168.1.175:8090 #从1.244主机访问 This is a httpd test page#再次查看apache访问日志,如下,不再是代理服务器IP地址,此时显示为1.244 192.168.1.244 - - [30/Oct/2017:15:49:07 +0800] "GET / HTTP/1.0" 200 27 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh3/1.4.2"
三、基于目录匹配反向代理
后端服务器采用Nginx的配置
# more /etc/redhat-release ##os平台及ip地址 CentOS release 6.7 (Final)# ip addr|grep eth0|grep global inet 192.168.1.132/24 brd 192.168.1.255 scope global eth0# nginx -v ##nginx版本 nginx version: nginx/1.10.2# mkdir -pv /usr/share/nginx/html/images ##创建图片目录 mkdir: created directory `/usr/share/nginx/html/images' # cp /usr/share/backgrounds/nature/*.jpg /usr/share/nginx/html/images/. ##复制图片文件 # cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bk # vim /etc/nginx/conf.d/default.conf ##此处直接修改缺省配置文件 server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } location /images { alias /usr/share/nginx/html/images; ##此处配置了别名 } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # /etc/init.d/nginx reload Reloading nginx: [ OK ]
前端Nginx配置
# vim /etc/nginx/conf.d/reverse_proxy.conf server { listen 8090; server_name localhost; location / { proxy_pass http://172.24.8.128; proxy_set_header X-Real-IP $remote_addr; } location /images { ##将images目录下的文件代理至192.168.1.132 proxy_pass http://192.168.1.132; proxy_set_header X-Real-IP $remote_addr; } }# systemctl reload nginx
验证代理情况,在ip为192.168.1.244测试对images目录下的jpg文件请求
# ip addr|grep inet|grep global inet 192.168.1.244/24 brd 192.168.1.255 scope global eth0# curl -I http://192.168.1.175:8090/images/Garden.jpg HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 31 Oct 2017 01:48:18 GMT Content-Type: image/jpeg Content-Length: 264831 Connection: keep-alive Last-Modified: Mon, 30 Oct 2017 08:21:28 GMT ETag: "59f6e108-40a7f" Accept-Ranges: bytes
四、基于特定文件类型的反向代理配置
php服务器端配置(ip 192.168.1.132)
# ss -nltp|grep php LISTEN 0 128 192.168.1.132:9000 *:* users:(("php-fpm",7147,8),("php-fpm",7148,0),("php-fpm",7149,0))# mkdir -pv /data ###存放php代码# echo "/data 192.168.1.0/24(rw)" >/etc/exports# /etc/init.d/rpcbind start# /etc/init.d/nfslock start# /etc/init.d/nfs start # echo "" > /data/index.php
Nginx代理端配置(ip 192.168.1.175)
# mkdir /data# mount -t nfs 192.168.1.132:/data /data# ls /data index.php# vim /etc/nginx/conf.d/reverse_proxy.conf server { listen 8090; server_name localhost; location / { proxy_pass http://172.24.8.128; proxy_set_header X-Real-IP $remote_addr; } location /images { proxy_pass http://192.168.1.132; proxy_set_header X-Real-IP $remote_addr; } location ~ \.php$ { root /data; fastcgi_pass 192.168.1.132:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } }# systemctl restart nginx
测试反向代理至php
[root@ydq05 ~]# ip addr|grep inet|grep global inet 192.168.1.244/24 brd 192.168.1.255 scope global eth0 [root@ydq05 ~]# curl -I http://192.168.1.175:8090/index.php HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 31 Oct 2017 03:22:59 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.0
五、基于upstream 配置反向代理至tomcat
Nginx upstream指令也可以将请求代理到后端服务器 如下示例,结合upstream指令演示将其代理到tomcat
# vim /etc/nginx/conf.d/tomcat.confupstream app { server localhost:8080; keepalive 32; } server { listen 80; server_name localhost; location / { proxy_set_header Host $host; proxy_set_header x-for $remote_addr; proxy_set_header x-server $host; proxy_set_header x-agent $http_user_agent; proxy_pass http://app; } } [root@node132 conf.d]# ss -nltp|grep javaLISTEN 0 1 ::ffff:127.0.0.1:8005 :::* users:(("java",39559,45)) LISTEN 0 100 :::8009 :::* users:(("java",39559,43)) LISTEN 0 100 :::8080 :::* users:(("java",39559,42)) tomcat版本 [root@node132 conf.d]# /usr/local/tomcat/bin/catalina.sh versionUsing CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat .... Server version: Apache Tomcat/7.0.69 Server built: Apr 11 2016 07:57:09 UTC Server number: 7.0.69.0 OS Name: Linux OS Version: 2.6.32-573.el6.x86_64 Architecture: amd64 JVM Version: 1.7.0_79-b15 JVM Vendor: Oracle Corporation 验证结果# curl http://localhost ......
六、proxy模块指令描述
proxy模块的可用配置指令非常多,它们分别用于定义proxy模块工作时的诸多属性,如连接超时时长、代理时使用http协议版本等。下面对常用的指令做一个简单说明。
proxy_read_timeout 在连接断开之前两次从接收upstream server接收读操作的最大间隔时长;
如下面的一个示例:
proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 30; proxy_send_timeout 15; proxy_read_timeout 15;
以上是CentOS如何配置Nginx反向代理的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

CentOS 中配置 IP 地址的步骤:查看当前网络配置:ip addr编辑网络配置文件:sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0更改 IP 地址:编辑 IPADDR= 行更改子网掩码和网关(可选):编辑 NETMASK= 和 GATEWAY= 行重启网络服务:sudo systemctl restart network验证 IP 地址:ip addr

提升CentOS上HDFS性能:全方位优化指南优化CentOS上的HDFS(Hadoop分布式文件系统)需要综合考虑硬件、系统配置和网络设置等多个方面。本文提供一系列优化策略,助您提升HDFS性能。一、硬件升级与选型资源扩容:尽可能增加服务器的CPU、内存和存储容量。高性能硬件:采用高性能网卡和交换机,提升网络吞吐量。二、系统配置精调内核参数调整:修改/etc/sysctl.conf文件,优化TCP连接数、文件句柄数和内存管理等内核参数。例如,调整TCP连接状态和缓冲区大小

CentOS 和 Ubuntu 的关键差异在于:起源(CentOS 源自 Red Hat,面向企业;Ubuntu 源自 Debian,面向个人)、包管理(CentOS 使用 yum,注重稳定;Ubuntu 使用 apt,更新频率高)、支持周期(CentOS 提供 10 年支持,Ubuntu 提供 5 年 LTS 支持)、社区支持(CentOS 侧重稳定,Ubuntu 提供广泛教程和文档)、用途(CentOS 偏向服务器,Ubuntu 适用于服务器和桌面),其他差异包括安装精简度(CentOS 精

CentOS 关机命令为 shutdown,语法为 shutdown [选项] 时间 [信息]。选项包括:-h 立即停止系统;-P 关机后关电源;-r 重新启动;-t 等待时间。时间可指定为立即 (now)、分钟数 ( minutes) 或特定时间 (hh:mm)。可添加信息在系统消息中显示。

CentOS将于2024年停止维护,原因是其上游发行版RHEL 8已停止维护。该停更将影响CentOS 8系统,使其无法继续接收更新。用户应规划迁移,建议选项包括CentOS Stream、AlmaLinux和Rocky Linux,以保持系统安全和稳定。

CentOS 安装步骤:下载 ISO 映像并刻录可引导媒体;启动并选择安装源;选择语言和键盘布局;配置网络;分区硬盘;设置系统时钟;创建 root 用户;选择软件包;开始安装;安装完成后重启并从硬盘启动。

CentOS系统下GitLab的备份与恢复策略为了保障数据安全和可恢复性,CentOS上的GitLab提供了多种备份方法。本文将详细介绍几种常见的备份方法、配置参数以及恢复流程,帮助您建立完善的GitLab备份与恢复策略。一、手动备份利用gitlab-rakegitlab:backup:create命令即可执行手动备份。此命令会备份GitLab仓库、数据库、用户、用户组、密钥和权限等关键信息。默认备份文件存储于/var/opt/gitlab/backups目录,您可通过修改/etc/gitlab

在CentOS系统上启用PyTorchGPU加速,需要安装CUDA、cuDNN以及PyTorch的GPU版本。以下步骤将引导您完成这一过程:CUDA和cuDNN安装确定CUDA版本兼容性:使用nvidia-smi命令查看您的NVIDIA显卡支持的CUDA版本。例如,您的MX450显卡可能支持CUDA11.1或更高版本。下载并安装CUDAToolkit:访问NVIDIACUDAToolkit官网,根据您显卡支持的最高CUDA版本下载并安装相应的版本。安装cuDNN库:前
