Nginx 反向代理工作原理簡介與配置詳解


Nginx 反向代理工作原理簡介與配置詳解

測試環境
CentOS 6.8-x86_64
nginx-1.10.0
下載地址:http://nginx.org/en/download.html
安裝 nginx
[root@localhost mnt]# tar -xzvf nginx-1.10.0.tar.gz
[root@localhost mnt]# cd nginx-1.10.0
[root@localhost nginx-1.10.0]# ./configure --prefix=/usr/local/ngnix --withpcre=/mnt/pcre-8.36
Configuration summary
+ using PCRE library: /mnt/pcre-8.36
+ OpenSSL library is not used
+ using builtin md5 code
+ sha1 library is not found
+ using system zlib library
nginx path prefix: "/usr/local/ngnix"
nginx binary file: "/usr/local/ngnix/sbin/nginx"
nginx modules path: "/usr/local/ngnix/modules"
nginx configuration prefix: "/usr/local/ngnix/conf"
nginx configuration file: "/usr/local/ngnix/conf/nginx.conf"
nginx pid file: "/usr/local/ngnix/logs/nginx.pid"
nginx error log file: "/usr/local/ngnix/logs/error.log"
nginx http access log file: "/usr/local/ngnix/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
注:
1、編譯時,指定了 pcre 安裝目錄,但是安裝出錯,解決方法如上,指定源碼所在目錄
2、如果不指定--with-pcre 選項,會報類似如下的錯誤
3、需要預先安裝 gcc-c++
[root@localhost nginx-1.10.0]# make && make install

啟動 ngnix
[root@localhostnginx-1.10.0]#
/usr/local/ngnix/sbin/nginx –c /usr/local/ngnix/conf/nginx.conf
反向代理工作原理
客戶端向反向代理發送請求,接着反向代理轉發請求至目標服務器,並把獲得的內容返回給
客戶端
反向代理配置
測試鏈接:
http://192.168.1.104/zentaopms/www/index.php?
http://192.168.1.104/zentaopms/www/index.php?m=project&f=create
如上,想通過 192.168.1.103 代理服務器訪問上述測試鏈接,具體咋操作呢?如下
編輯所使用的配置文件
[root@localhost nginx-1.10.0]# vim /usr/local/ngnix/conf/nginx.conf
找到“Server”結點,增加入下帶背景色內容
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#


error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /zentaopms/www/ {
proxy_pass http://192.168.1.104;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
重新加載配置文件
[root@localhost nginx-1.10.0]# /usr/local/ngnix/sbin/nginx -s reload
訪問測試 url
如下,OK
說明:
傳遞請求給被代理服務器
為了把請求傳遞給被代理服務器,需要在 location 中指定 proxy_pass 機制。如下
location /some/path/ {
proxy_pass http://www.example.com/link/;
}
proxy_pass 既可以是 ip 地址,也可以是域名,同時還可以指定端口
location ~ \.php {
proxy_pass http://127.0.0.1:8000;
}
注意:如果 proxy_pass 指定的地址攜帶了 URI,如上例中 /link/,那么這里的 URI 將替換
請求 URI 中匹配 location 參數的部分,比如 請求 /some/path/page.html,將被替換為
http://www.example.com/link/page.html。
另外,如果請求不是發往 http 類型的被代理服務器,則選擇如下之一:
fastcgi_pass 傳遞請求給 FastCGI 服務器
uwsgi_pass 傳遞請求給 uwsgi 服務器
scgi_pass 傳遞請求給 SCGI 服務器
memcached_pass 傳遞請求給 memcached 服務器
請求也可以發往一命名的組服務器,這種請求下,將根據指定方法,在這些服務器之中進行
請求的分發。
傳遞請求頭
默認的,nginx 在被代理請求中定義兩個頭域:Host 和 Connection,並且清除包含空值的
頭域。 Host 被設置為$proxy_host 變量,而 Connection 則被設置為 close。
使用 proxy_set_header 機制可修改默認配置及其它頭域的值。可以在 location 中,server
上下文,http 塊或者其它更高層級中指定這種機制。
例子:
location /some/path/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8000;
}
如果想阻止某個頭域被傳遞給被代理服務器,可以如下設置頭域的值為空
location /some/path/ {
proxy_set_header Accept-Encoding "";
proxy_pass http://localhost:8000;
}
配置緩沖(Buffer)
默認的,Ngnix buffering 來自被代理服務器的響應。Ngnix 在內部 buffering 中存儲響應,
直到收到整個響應后才發送給客戶端。對於慢客戶端來說,buffering 可優化性能,這樣,
如果響應從 Nginx 同步傳遞給客戶端,這將會浪費被代理服務器的時間。但是,如果開啟
buffering,Nginx 允許被代理服務器快速處理請求,因為 Nginx 會盡可能久的存儲來自被
代理服務器的響應,直到客戶端下載它們。
使用 proxy_buffering 機制開啟或關閉緩沖。默認的,開啟緩沖。
proxy_buffers 控制 buffer 大小和分配給請求的 buffer 數量。來自被代理服務器響應中的
第一部分被存儲在單一的 buffer 中,該 buffer 的大小由 proxy_buffer_size 設定。該部分
通常包含一個相對較小的響應頭,其大小可以設置成比用於存儲剩余響應部分 buffer 小。
例子:
location /some/path/ {
proxy_buffers 16 4k;
proxy_buffer_size 2k;
proxy_pass http://localhost:8000;
}
如果關閉 buffering,當從被代理服務器接收到響應時,將被同步把響應發往客戶端。這對
於想盡快收到請求的快速交互客戶端來說。這是其想要的。
例子:關閉 buffering
location /some/path/ {
proxy_buffering off;
proxy_pass http://localhost:8000;
}
這種情況下,nginx 只用 proxy_buffer_size 來存儲響應的當前部分。
選擇一個出口 IP 地址
如果代理服務器有多個網絡接口,有時候需要選擇特定的源 ip 地址來連接到代理服務器。
當被代理服務器被設置為只接受來自特定 IP 地址或者 IP 范圍的連接請求時,這特別有用。
例子
location /app1/ {
proxy_bind 127.0.0.1;
proxy_pass http://example.com/app1/;
}
location /app2/ {
proxy_bind 127.0.0.2;
proxy_pass http://example.com/app2/;
}
ip 地址也可以是一個變量
例子
location /app3/ {
proxy_bind $server_addr;
proxy_pass http://example.com/app3/;


免責聲明!

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



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