基于CentOS 7配置Nginx反向代理

Nginx作为反向代理服务器被广泛使用在各大互联网企业。它简单易用,可以根据业务的需求将其不同的业务类型代理至不同的服务器,将整个站点请求压力按类型分摊到不同的服务器。该方式使的整个站点请求性能得以极大的提升。本文简要描述了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 8090
LISTEN 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这个IP
172.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 libssh2/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文件请求(基于浏览器查看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 "<?php phpinfo();?>" > /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.conf 
upstream 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 java
LISTEN    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 version
Using 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

<!DOCTYPE html>

<html lang="en">
    <head>
        <title>Apache Tomcat/7.0.69</title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />
    </head>
    ......

五、proxy模块指令描述

proxy模块的可用配置指令非常多,它们分别用于定义proxy模块工作时的诸多属性,如连接超时时长、代理时使用http协议版本等。下面对常用的指令做一个简单说明。

proxy_connect_timeout
  nginx将一个请求发送至upstream server之前等待的最大时长;
proxy_cookie_domain
  将upstream server通过Set-Cookie首部设定的domain属性修改为指定的值,其值可以为一个字符串、正则表达式的模式或一个引用的变量;
proxy_cookie_path
   将upstream server通过Set-Cookie首部设定的path属性修改为指定的值,其值可以为一个字符串、正则表达式的模式或一个引用的变量;
proxy_hide_header
  设定发送给客户端的报文中需要隐藏的首部;
proxy_pass
  指定将请求代理至upstream server的URL路径;
proxy_set_header
  将发送至upsream server的报文的某首部进行重写;
proxy_redirect
  重写location并刷新从upstream server收到的报文的首部;
proxy_send_timeout
  在连接断开之前两次发送至upstream server的写操作的最大间隔时长;
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;

注:最后更新时间20171110,添加反向代理至tomcat内容

DBA牛鹏社(SQL/NOSQL/LINUX)

这里写图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: CentOS 7上配置Nginx反向代理的步骤如下: 1. 安装Nginx 使用以下命令安装Nginx: ``` sudo yum install nginx ``` 2. 配置Nginx反向代理 打开Nginx配置文件: ``` sudo vi /etc/nginx/nginx.conf ``` 在http块中添加以下内容: ``` server { listen 80; server_name example.com; location / { proxy_pass http://localhost:808; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } ``` 其中,example.com是你的域名,localhost:808是你要反向代理的服务地址。 3. 重启Nginx 使用以下命令重启Nginx: ``` sudo systemctl restart nginx ``` 现在,你的Nginx反向代理已经配置完成。当访问example.com时,Nginx会将请求转发到localhost:808。 ### 回答2: CentOS 7 是一款广泛使用的开源操作系统,而Nginx是一种轻量级的高性能 Web 服务器反向代理服务器。 在本文中,我们将讨论如何在CentOS 7上配置Nginx反向代理。 1. 安装 NginxCentOS 7 上安装 Nginx 非常简单,只需使用以下命令即可: sudo yum install nginx 2. 配置反向代理 为了进行反向代理配置,我们需要编辑 /etc/nginx/nginx.conf 文件。打开文件并找到以下位置: http { # ... } 在这个部分的上面添加以下代码: upstream backend { server 192.168.1.100:80; server 192.168.1.101:80; } 这里我们创建了一个名为“backend”的上游服务器块,并添加了两个服务器。这些服务器的 IP 地址和端口号是根据你自己的需求进行配置。 在上面的代码块中,我们使用了 IP 地址,因此正如你所看到的,我们将使用其它服务器上的 Nginx 服务器作为反向代理。接下来,我们需要配置关闭本地缓存、缓存控制、代理请求和日志记录的 Nginx 指令。添加以下代码段: proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m; proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args"; proxy_cache_valid 200 60m; proxy_cache_bypass $http_pragma; proxy_cache_revalidate on; proxy_ignore_headers Cache-Control Expires; proxy_set_header Cache-Control public; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; 最后,我们需要指定我们的 Nginx 服务器作为反向代理。添加以下代码段: server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_cache my_cache; } } 在上述代码段中,我们使用一个名为“example.com”的虚拟主机来实现反向代理。我们还将代理传递到名为“backend”的上游服务器集合中,并启用了 Nginx 反向代理服务器缓存。 此外,代码代码段中的 location 必须位于 server 指令块内,我们还配置了存储在缓存中的页面缓存,以及 Nginx 访问和错误日志记录。 最后,重启Nginx服务来应用更改: sudo systemctl restart nginx 恭喜你!现在你可以使用 Nginx 反向代理来扩展你的服务。 ### 回答3: CentOS 7是目前应用最广泛的Linux操作系统之一,集成了很多开源软件,其中包括一个非常流行的Web服务器——Nginx,其强大的反向代理功能被广泛应用于各种场景中,本文将介绍如何在CentOS 7上配置Nginx反向代理配置前的准备 在进行配置之前,需要确保已经安装了Nginx和其他必要的依赖软件。可以通过以下命令检查: ``` # 安装epel仓库 yum install epel-release # 安装nginx和必要的依赖 yum install nginx openssl net-tools wget -y ``` 配置Nginx反向代理配置Nginx反向代理之前,需要确保已经了解了代理服务器和目标Web服务器的基本架构和访问方式。下面我们将针对一个具体案例进行配置,具体步骤如下: 在Nginx配置文件中添加反向代理配置 首先需要打开Nginx配置文件`/etc/nginx/nginx.conf`,在`http`段中添加下列代码: ``` http { ... server { listen 80; server_name example.com; location / { proxy_pass http://192.168.1.10:8080; } } ... } ``` 其中,`listen`指定Nginx监听的端口和协议(这里是80端口),`server_name`指定代理服务器的域名或IP地址(这里是example.com),`location`指定反向代理的目标Web服务器地址和端口号(这里是192.168.1.10:8080),注意需要确保目标Web服务器是可访问的。 重启Nginx服务 在修改配置文件之后,需要使用以下命令重启Nginx服务,使得新的配置生效: ``` sudo systemctl restart nginx ``` 测试反向代理是否生效 完成上述步骤之后,可以使用浏览器访问`http://example.com`来测试反向代理是否生效。如果一切正常,则应该能够看到目标Web服务器上的页面。 总结 Nginx反向代理是一个非常强大的功能,可以解决多个Web服务器之间的负载均衡、高可用等问题。本文介绍了在CentOS 7上配置Nginx反向代理的具体步骤,希望对有需要的读者有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值