码迷,mamicode.com
首页 > 其他好文 > 详细

配置Nginx反向代理

时间:2019-10-22 10:37:05      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:code   tab   port   raw   irb   logs   jpg   ever   size   

Ngxin反向代理和负载均衡

技术图片

配置Nginx反向代理

反向代理介绍

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

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

反向代理Demo

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

  1. 准备工作
    1
    2
    3
    - centos 7安装jdk
    - 上传tomcat到服务器(解压出两份,注意修改解压出来的目录名称)
    - 安装nginx

技术图片

  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
    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;
    }
    }
  • 第二种配置方式

    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>
    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;
    }
    }
  1. 运行tomcat,运行nginx

    1
    2
    3
    4
    5
    6

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

    # nginx
    nginx/sbin/nginx
  2. 测试
    技术图片
    技术图片

配置均衡负载

  1. 配置nginx.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
       ## 轮询,每个节点一次
    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; //权重越大,分配到的请求越多
    }

反向代理实例

  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
      # 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;
    }
    }
  2. 测试
    技术图片

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

配置Nginx反向代理

标签:code   tab   port   raw   irb   logs   jpg   ever   size   

原文地址:https://www.cnblogs.com/sanxiandoupi/p/11718102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!