nginx反向代理原理和配置講解


最近有打算研讀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;
        }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM