最近有打算研读nginx源代码,看到网上介绍nginx可以作为一个反向代理服务器完成负载均衡。所以搜罗了一些关于反向代理服务器的内容,整理综合。

一  概述

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

通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。当一个代理服务器能够代理外部网络上的主机,访问内部网络时,这种代理服务的方式称为反向代理服务。

图1  反向代理服务器的基本原理

二  反向代理服务器的工作原理

反向代理服务器通常有两种模型,它可以作为内容服务器的替身,也可以作为内容服务器集群的负载均衡器。

1,作内容服务器的替身

如果您的内容服务器具有必须保持安全的敏感信息,如信用卡号数据库,可在防火墙外部设置一个代理服务器作为内容服务器的替身。当外部客户机尝试访问内容服务器时,会将其送到代理服务器。实际内容位于内容服务器上,在防火墙内部受到安全保护。代理服务器位于防火墙外部,在客户机看来就像是内容服务器。

当客户机向站点提出请求时,请求将转到代理服务器。然后,代理服务器通过防火墙中的特定通路,将客户机的请求发送到内容服务器。内容服务器再通过该通道将结果回传给代理服务器。代理服务器将检索到的信息发送给客户机,好像代理服务器就是实际的内容服务器(参见图 2)。如果内容服务器返回错误消息,代理服务器会先行截取该消息并更改标头中列出的任何 URL,然后再将消息发送给客户机。如此可防止外部客户机获取内部内容服务器的重定向 URL。

这样,代理服务器就在安全数据库和可能的恶意攻击之间提供了又一道屏障。与有权访问整个数据库的情况相对比,就算是侥幸攻击成功,作恶者充其量也仅限于访问单个事务中所涉及的信息。未经授权的用户无法访问到真正的内容服务器,因为防火墙通路只允许代理服务器有权进行访问。

图2  反向代理服务器作为内容服务器的替身

可以配置防火墙路由器,使其只允许特定端口上的特定服务器(在本例中为其所分配端口上的代理服务器)有权通过防火墙进行访问,而不允许其他任何机器进出。

2,作为内容服务器的负载均衡器

可以在一个组织内使用多个代理服务器来平衡各 Web 服务器间的网络负载。在此模型中,可以利用代理服务器的高速缓存特性,创建一个用于负载平衡的服务器池。此时,代理服务器可以位于防火墙的任意一侧。如果 Web 服务器每天都会接收大量的请求,则可以使用代理服务器分担 Web 服务器的负载并提高网络访问效率。

对于客户机发往真正服务器的请求,代理服务器起着中间调停者的作用。代理服务器会将所请求的文档存入高速缓存。如果有不止一个代理服务器,DNS 可以采用“循环复用法”选择其 IP 地址,随机地为请求选择路由。客户机每次都使用同一个 URL,但请求所采取的路由每次都可能经过不同的代理服务器。

可以使用多个代理服务器来处理对一个高用量内容服务器的请求,这样做的好处是内容服务器可以处理更高的负载,并且比其独自工作时更有效率。在初始启动期间,代理服务器首次从内容服务器检索文档,此后,对内容服务器的请求数会大大下降。

图3  反向代理服务器作为负载均衡器

参考内容:

1,百度百科

2,http://www.oracle.com/technetwork/indexes/documentation/index.html

  1. Chapter: Nginx基本操作释疑
    1. 1. Nginx的端口修改问题
    2. 2. Nginx 301重定向的配置
    3. 3. Windows下配置Nginx使之支持PHP
    4. 4. Linux下配置Nginx使之支持PHP
    5. 5. 以源码编译的方式安装PHP与php-fpm
    6. 6. Nginx多站点配置的一次实践
    7. 7. Nginx反向代理的配置

Nginx 作为 web 服务器一个重要的功能就是反向代理。其实我们在前面的一篇文章《Nginx多站点配置的一次实践》里,用的就是 Nginx 的反向代理,这里简单再提一下。

下面是配置 Nginx 作为 tornado 的反向代理的设置:

01 upstream tornado {
02     server 127.0.0.1:8888;
03 }
04   
05 server {
06     listen   80;
07     root /root/nmapp2_venv;
08     index index.py index.html;
09   
10     server_name server;
11   
12     location / {
13         #if (!-e $request_filename) {
14         #    rewrite ^/(.*)$ /index.py/$1 last;
15         #}
16     }
17   
18     location ~ /index\.py {
19         proxy_pass_header Server;
20         proxy_set_header Host $http_host;
21         proxy_set_header X-Real-IP $remote_addr;
22         proxy_set_header X-Scheme $scheme;
23         proxy_pass http://tornado;
24     }
25 }

Nginx 反向代理的指令不需要新增额外的模块,默认自带 proxy_pass 指令,只需要修改配置文件就可以实现反向代理。

再举一个例子吧。比如要配置后端跑 apache 服务的 ip 和端口,也就是说,我们的目标是实现通过 http://ip:port 能访问到你的网站。

只要新建一个 vhost.conf,加入如下内容(记得修改 ip 和域名为你的 ip 和域名)。修改nginx.conf,添加 include quancha.conf 到http{}段, reload nginx就可以了。

Nginx 反向代理模板:

01 ## Basic reverse proxy server ##
02 upstream apachephp  {
03     server ip:8080; #Apache
04 }
05   
06 ## Start www.nowamagic.net ##
07 server {
08     listen 80;
09     server_name  www.nowamagic.net;
10   
11     access_log  logs/quancha.access.log  main;
12     error_log  logs/quancha.error.log;
13     root   html;
14     index  index.html index.htm index.php;
15   
16     ## send request back to apache ##
17     location / {
18         proxy_pass  http://apachephp;
19   
20         #Proxy Settings
21         proxy_redirect     off;
22         proxy_set_header   Host             $host;
23         proxy_set_header   X-Real-IP        $remote_addr;
24         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
25         proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
26         proxy_max_temp_file_size 0;
27         proxy_connect_timeout      90;
28         proxy_send_timeout         90;
29         proxy_read_timeout         90;
30         proxy_buffer_size          4k;
31         proxy_buffers              4 32k;
32         proxy_busy_buffers_size    64k;
33         proxy_temp_file_write_size 64k;
34    }
35 }

这就完成了 Nginx 反向代理配置。

------使用过的配置

#user  nobody;
worker_processes  4;
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
worker_rlimit_nofile 204800;

events {
    worker_connections 16384;
    multi_accept on;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

log_format  test166  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                      '"[$request_time]" "[$upstream_response_time]" '
                      '"[$connection]" "[$connection_requests]" '
                      '"$http_imei" "$http_mobile" "$http_type" "$http_key" "$cookie_sfpay_jsessionid"';
    access_log  logs/access.log  test166;

sendfile        on;
    #tcp_nopush     on;
    underscores_in_headers on;

keepalive_timeout  65;
    proxy_connect_timeout 120;
    proxy_read_timeout 120;
    proxy_send_timeout 60;
    proxy_buffer_size 16k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    proxy_temp_path /home/temp_dir;
    proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

client_header_buffer_size 12k;
    open_file_cache max=204800 inactive=65s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;

gzip  on;
    gzip_types       text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png image/jpg;

upstream ims-oms {
        server 1.1.240.31:8001;
    }

upstream anruy-tomcat {
    server 1.1.231.54:8080;
    server 1.1.231.55:8080;
        keepalive 40;
    }
  
    upstream anruy-tomcat {
    server 1.1.231.84:8080;
    server 1.1.231.85:8080;
    keepalive 40;
    }

# HTTP server
    #
    server {
        listen       8080;
        server_name  anruy01-sit;

location ~ (etc/passwd|\.php|\.asp|win.ini)$ {
        deny    all;
        }
        location /nginx_status {
                stub_status on;
                access_log off;
        }
        location /ims/{
            proxy_pass  http://ims-oms/ims/;
                proxy_set_header  X-Real-IP  $remote_addr;
                 proxy_set_header Host $host;

}
     location /anruy/ {
            proxy_pass  http://anruy-tomcat/anruy/remote/interface;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header Host $host:1443;
            proxy_http_version 1.1;
            proxy_set_header Connection keep-alive;
            proxy_set_header Keep-Alive 600;
            keepalive_timeout 600;
        }

location /anruy-front/ {
            proxy_pass  http://anruy-tomcat/anruy-front/remote/interface;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header Host $host:1443;
            proxy_http_version 1.1;
            proxy_set_header Connection keep-alive;
        proxy_set_header Keep-Alive 600;
            keepalive_timeout 600;
        }

location / {
            root   html;
            index  index.html index.htm;
        }
         #   client_body_temp_path  /usr/local/nginx/html/tmp;
         #   dav_access             group:rw  all:r;
         #   index  index.html index.htm *.jsp   ;
         #   proxy_set_header  X-Real-IP  $remote_addr;
         #   client_max_body_size  100m;
        }
}

Nginx 的方向代理及配置的更多相关文章

  1. Nginx的反向代理的配置

    1.linux下的方向代理(前提域名和P已经映射好了的) 在linux中的输入命令:whereis nginx 查看当前nginx的安装目录 显示 nginx: /usr/local/nginx 命令 ...

  2. Nginx专题(1):Nginx之反向代理及配置

    摘要:本文从Nginx的概念出发,分别从反向代理的概念.优势.配置代码3个方面介绍了Nginx的特性之一反向代理. 文章来源:宜信技术学院 & 宜信支付结算团队技术分享第一期-宜信支付结算八方 ...

  3. nginx的反向代理和配置

    最近有打算研读nginx源代码,看到网上介绍nginx可以作为一个反向代理服务器完成负载均衡.所以搜罗了一些关于反向代理服务器的内容,整理综合. 一  概述 反向代理(Reverse Proxy)方式 ...

  4. linux下nginx【反向代理】配置【负载均衡】配置

    nginx 可以配置多个端口: 1.10088端口 配置反向代理,消除跨域问题. 2.10087端口 配置ip_hash模式的负载均衡,ip_hash可以绕开解决session共享的问题. nginx ...

  5. Nginx学习笔记(三)--- Nginx实现反向代理和配置负载均衡

    1.反向代理 2.Nginx反向代理流程图 3.安装多个tomcat 3.1把tomcat的压缩包传到Linux上 3.2 解压tomcat 3.3 给压缩好的tomcat改个名字用来区分一下 3.4 ...

  6. 使用Nginx做反向代理的配置

    安装Nginx服务之后 修改Nginx配置文件 如下server字段中主要是配置listen监听8080 端口,然后静态文件袋里到8001  后端端口代理到8000 server { listen 8 ...

  7. Nginx HTTP反向代理基础配置

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  8. nginx http正向代理简单配置及systemd 配置

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  9. nginx的https代理http配置

    http { upstream https2http_proxy{ server 192.168.22.103:80; } server { listen 1443 ssl; server_name ...

随机推荐

  1. 【转】js监听浏览器离开页面操作

    [转]https://www.cnblogs.com/slly/p/7991474.html 序言 大家是否经常遇到在关闭网页的时候,会看到一个确定是否离开当前页面的提示框?想一些在线测试系统.信息录 ...

  2. 【NOIP2016提高A组五校联考4】square

    题目 分析 首先,设\(f_{i,j}\)表示最大的以(i,j)为左下角的正方形的边长. 转移显然,\(f_{i,j}=\max(f_{i-1,j},f_{i,j-1},f_{i-1,j-1})+1\ ...

  3. elasticsearch基本Restful操作

    1.添加数据curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/emplo ...

  4. (转)electron主线程中通过mainWindow.webContents.send发送事件,渲染线程接收不到

    转自 https://segmentfault.com/q/1010000015599245/ 准备实现的功能: 页面1(渲染进程1)中点击按钮,发送事件给到主进程.主进程成功接收事件后,通过main ...

  5. python 使用嵌套函数报local variable xxx referenced before assignment或者 local variable XXX defined in enclosing scope

    情况一: a 直接引用外部的,正常运行 def toplevel(): a = 5 def nested(): print(a + 2) # theres no local variable a so ...

  6. java-webuploader+Java如何实现分片+断点续传

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...

  7. Static使用

    1.什么是static? static 是C++中很常用的修饰符,它被用来控制变量的存储方式和可见性. 其余控制变量存储方式的关键字为auto.register.extern. 2.为什么要引入sta ...

  8. 与HTTP协作的Web服务器——代理、网关、隧道

    1.虚拟主机 (1)HTTP/1.1规范允许一台HTTP服务器搭建多个Web站点: (2)在互联网上,域名通过DNS服务映射到IP地址(域名解析)之后访问目标网站,即当请求发送到服务器时,已经是以IP ...

  9. 【Leetcode】对称二叉树

    递归法 执行用时 :12 ms, 在所有 C++ 提交中击败了43.44%的用户 内存消耗 :14.6 MB, 在所有 C++ 提交中击败了95.56%的用户 /** * Definition for ...

  10. 【Python】学习笔记三:序列

    sequence(序列) sequence(序列)是一组有序的元素的集合,序列可以有任何元素,也可以没有元素 元组与表的区别:一旦建立,tuple的各个元素不可再变更,而list的各个元素可以再变更 ...