Nginx服務器作反向代理實現內部局域網的url轉發配置


情景 由於公司內網有多台服務器的http服務要映射到公司外網靜態IP,如果用路由的端口映射來做,就只能一台內網服務器的80端口映射到外網80端口,其他服務器的80端口只能映射到外網的非80端口。非80端口的映射在訪問的時候要域名加上端口,比較麻煩。並且公司入口路由最多只能做20個端口映射。肯定以后不夠用。 然后k兄就提議可以在內網搭建個nginx反向代理服務器,將nginx反向代理服務器的80映射到外網IP的80,這樣指向到公司外網IP的域名的HTTP請求就會發送到nginx反向代理服務器,利用nginx反向代理將不同域名的請求轉發給內網不同機器的端口,就起到了“根據域名自動轉發到相應服務器的特定端口”的效果,而路由器的端口映射做到的只是“根據不同端口自動轉發到相應服務器的特定端口”,真是喜大普奔啊。 涉及的知識:nginx編譯安裝,nginx反向代理基本配置,路由端口映射知識,還有網絡域名等常識。 本次實驗目標是做到:在瀏覽器中輸入xxx123.tk能訪問到內網機器192.168.10.38的3000端口,輸入xxx456.tk能訪問到內網機器192.168.10.40的80端口。 配置步驟 服務器ubuntu 12.04

?
1
2
3
4
5
###更新倉庫
 
apt-get update -y
apt-get install wget -y
#下載nginx和相關軟件包

pcre是為了編譯rewrite模塊,zlib是為了支持gzip功能。額,這里nginx版本有點舊,因為我還要做升級nginx的實驗用。大家可以裝新版本。

?
1
2
3
4
5
6
7
8
9
10
cd /usr/local/src
  wget <a href= "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz" > ftp : //ftp .csx.cam.ac.uk /pub/software/programming/pcre/pcre-8 .33. tar .gz< /a >
  wget <a href= "http://zlib.net/zlib-1.2.8.tar.gz" >http: //zlib .net /zlib-1 .2.8. tar .gz< /a >
  wget <a href= "http://nginx.org/download/nginx-1.4.2.tar.gz" >http: //nginx .org /download/nginx-1 .4.2. tar .gz< /a >
  tar xf pcre-8.33. tar .gz
  tar xf zlib-1.2.8. tar .gz
#安裝編譯環境
 
  apt-get install build-essential libtool -y
#創建nginx用戶

所謂的unprivileged user

?
1
2
3
4
5
6
7
8
9
10
11
12
useradd -s /bin/false -r -M -d /nonexistent www
#開始編譯安裝
 
/configure --with-pcre= /usr/local/src/pcre-8 .33 --with-zlib= /usr/local/src/zlib-1 .2.8 --user=www --group=www \
  --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
  make
  make install
#給文件夾授權
 
chown -R www:www /usr/local/nginx
#修改配置文件
vim 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
36
37
38
39
40
41
42
user www www;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
   use epoll;
   worker_connections 65535;
}
http {
   include mime.types;
   default_type application/octet-stream;
   include /usr/local/nginx/conf/reverse-proxy.conf;
   sendfile on;
   keepalive_timeout 65;
   gzip on;
   client_max_body_size 50m; #緩沖區代理緩沖用戶端請求的最大字節數,可以理解為保存到本地再傳給用戶
   client_body_buffer_size 256k;
   client_header_timeout 3m;
   client_body_timeout 3m;
   send_timeout 3m;
   proxy_connect_timeout 300s; #nginx跟后端服務器連接超時時間(代理連接超時)
   proxy_read_timeout 300s; #連接成功后,后端服務器響應時間(代理接收超時)
   proxy_send_timeout 300s;
   proxy_buffer_size 64k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
   proxy_buffers 4 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置
   proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2)
   proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳遞請求,而不緩沖到磁盤
   proxy_ignore_client_abort on; #不允許代理端主動關閉連接
   server {
     listen 80;
     server_name localhost;
     location / {
       root html;
       index index.html index.htm;
     }
     error_page 500 502 503 504 /50x.html;
     location = /50x.html {
       root html;
     }
   }
}

編輯反向代理服務器配置文件:

?
1
vim /usr/local/nginx/conf/reverse-proxy .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
server
{
   listen 80;
   server_name xxx123.tk;
   location / {
     proxy_redirect off;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_pass http://192.168.10.38:3000;
   }
   access_log logs/xxx123.tk_access.log;
}
  
server
{
   listen 80;
   server_name xxx456.tk;
   location / {
     proxy_redirect off;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_pass http://192.168.10.40:80;
   }
   access_log logs/xxx456.tk_access.log;
}

然后重新加載nginx配置文件,使之修改生效,再把xxx123.tk域名指向公司靜態IP,這樣就成功的做到了在瀏覽器中輸入xxx123.tk的時候訪問的內網服務器192.168.10.38的3000端口,輸入xxx456.tk訪問192.168.10.40的80端口的作用。 如果想對后端機器做負載均衡,像下面這配置就可以把對nagios.xxx123.tk的請求分發給內網的131和132這兩台機器做負載均衡了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
upstream monitor_server {
   server 192.168.0.131:80;
     server 192.168.0.132:80;
}
  
server
{
   listen 80;
   server_name nagios.xxx123.tk;
   location / {
     proxy_redirect off;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_pass http://monitor_server;
   }
   access_log logs/nagios.xxx123.tk_access.log;
}

額,關於負載均衡和緩存就不多說了,這里只是要起到一個簡單的“域名轉發”功能。 另外,由於http請求最后都是由反向代理服務器傳遞給后段的機器,所以后端的機器原來的訪問日志記錄的訪問IP都是反向代理服務器的IP。 要想能記錄真實IP,需要修改后端機器的日志格式,這里假設后端也是一台nginx: 在后端配置文件里面加入這一段即可:

?
1
2
3
4
5
log_format access '$HTTP_X_REAL_IP - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $HTTP_X_Forwarded_For';
  
access_log logs/access.log access;

再看看原來日志的格式長什么樣:

?
1
2
3
4
5
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
  
#access_log logs/access.log main;

看出區別了吧   遇到的問題   之前沒配置下面這段,訪問時候偶爾會出現504 gateway timeout,由於偶爾出現,所以不太好排查

?
1
2
3
4
5
6
7
8
proxy_connect_timeout 300s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_ignore_client_abort on;

報錯日志:

...upstream timed out (110: Connection timed out) while reading response header from upstream, client: ...(后面的省略) 從日志看來是連接超時了,網上一通亂查之后估計可能是后端服務器響應超時了,本着大膽假設,小心求證的原則,既然假設了錯誤原因就要做實驗重現錯誤:那就調整代理超時參數,反過來把代理超時閥值設小(比如1ms)看會不會次次出現504。后來發現把proxy_read_timeout 這個參數設置成1ms的時候,每次訪問都出現504。於是把這個參數調大,加入上面那段配置,解決問題了。

PS:關於域名轉發

所謂域名URL轉發,是通過服務器的特殊設置,將訪問您當前域名的用戶引導到您指定的另一個網絡地址。 地址轉向(也可稱“URL轉發”)即將一個域名指向到另外一個已存在的站點,英文稱為“ URL FORWARDING ”。域名指向可能這個站點原有的域名或網址是比較復雜難記的。 已經注冊成功的域名,若初設或取消 URL 轉發設置,一般均在 24-48 小時之內生效。對於原有已經設置成功的 URL 轉發域名,如果修改 URL 轉發的目標地址,則只需 1-2 個小時即可生效。 不隱藏路徑 URL 轉發:例如: http://b.com/ 指向 http://a.com/xxx/ (任意目錄);當在瀏覽器地址欄中敲入 http://b.com/ 后回車, IE 瀏覽器的地址欄里顯示的地址會由原來您敲入的 http://b.com/ 自動變為顯示真正的目標地址 http://a.com/xxx/ ; 隱藏路徑的 URL 轉發:例如:先同上, IE 瀏覽器的地址欄里顯示的地址保持不變,仍是 http://b.com/ ,但實際訪問到的是 http://a.com/xxx/ 的內容。


免責聲明!

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



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