配置Nginx反向代理

Ngxin反向代理和负载均衡
配置Nginx反向代理

反向代理介绍

  1. 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
  1. 反向代理的作用:

(1)保证内网的安全,可以使用反向代理提供WAF功能,阻止web攻击大型网站,通常将反向代理作为公网访问地址,Web服务器是内网。
(2)负载均衡,通过反向代理服务器来优化网站的负载。

反向代理Demo

系统:centos 7
此处使用tomcat作为被代理的服务器

  1. 准备工作
    1
    2
    3
    知识兔td>
    - centos 7安装jdk
    - 上传tomcat到服务器(解压出两份,注意修改解压出来的目录名称)
    - 安装nginx
    知识兔td>
  1. 修改tomcat配置,防止端口冲突(文件位置:tomcat/conf/server.xml),只需要修改其中一个tomcat配置即可


  2. 修改ngxin配置(文件位置:nginx/conf/nginx.conf)

  • 第一种配置方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    知识兔td>
    upstream tomcat-test1 {
    ## 设置被代理的ip
    server 192.168.200.132:8080;
    }
    server {
    listen 80;
    server_name www.tomcat1.com;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
    ## 被代理路径
    proxy_pass http://192.168.200.132:8080;
    index index.html index.htm;
    }
    }

    upstream tomcat-test2 {
    server 192.168.200.132:8081;
    }
    server {
    listen 80;
    server_name www.tomcat2.com;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
    proxy_pass http://192.168.200.132:8081;
    index index.html index.htm;
    }
    }
    知识兔td>
  • 第二种配置方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35大专栏  配置Nginx反向代理an>
    知识兔td>
    upstream tomcat-test1 {
    ## 设置被代理的ip
    server 192.168.200.132:8080;
    }
    server {
    listen 80;
    server_name www.tomcat1.com;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
    ## 被代理路径
    proxy_pass http://tomcat-test1;
    index index.html index.htm;
    }
    }

    upstream tomcat-test2 {
    server 192.168.200.132:8081;
    }
    server {
    listen 80;
    server_name www.tomcat2.com;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
    proxy_pass http://tomcat-test2;
    index index.html index.htm;
    }
    }
    知识兔td>
  1. 运行tomcat,运行nginx

    1
    2
    3
    4
    5
    6
    知识兔td>

    tomcat-test1/bin/startup.sh
    tomcat-test2/bin/startup.sh

    # nginx
    nginx/sbin/nginx
    知识兔td>
  2. 测试

配置均衡负载

  1. 配置nginx.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    知识兔td>
       ## 轮询,每个节点一次
    upstream tomcat-test2 {
    server 192.168.200.132:8081;
    server 192.168.200.132:8082;
    }

    ## 设置权重
    upstream tomcat-test2 {
    server 192.168.200.132:8081;
    server 192.168.200.132:8082 weight=2; //权重越大,分配到的请求越多
    }
    知识兔td>

反向代理实例

  1. 配置nginx.conf文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    知识兔td>
      # erp项目
    upstream erp {
    server 192.168.183.130:8080;
    }
    server {
    listen 80;
    server_name 192.168.183.130:8080;
    ## 代理相关静态资源
    location / {
    proxy_pass http://192.168.183.130:8080;
    proxy_read_timeout 600s;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host:$server_port;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    index index.html index.htm;
    }


    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }
    }
    知识兔td>
  2. 测试

    使用域名能访问到是因为我配置了hosts IP映射,如果没有配置,请使用IP访问

计算机