情景
內(nèi)網(wǎng)有多臺(tái)服務(wù)器的http服務(wù)要映射到公司外網(wǎng)靜態(tài)IP,如果用路由的端口映射來(lái)做,就只能一臺(tái)內(nèi)網(wǎng)服務(wù)器的80端口映射到外網(wǎng)80端口,其他服務(wù)器的80端口只能映射到外網(wǎng)的非80端口。非80端口的映射在訪問(wèn)的時(shí)候要域名加上端口,比較麻煩。入口路由做端口映射數(shù)量有限。太多肯定以后不夠用。
其實(shí)可以在內(nèi)網(wǎng)搭建個(gè)nginx反向代理服務(wù)器,將nginx反向代理服務(wù)器的80映射到外網(wǎng)IP的80,這樣指向到公司外網(wǎng)IP的域名的HTTP請(qǐng)求就會(huì)發(fā)送到nginx反向代理服務(wù)器,利用nginx反向代理將不同域名的請(qǐng)求轉(zhuǎn)發(fā)給內(nèi)網(wǎng)不同機(jī)器的端口,就起到了“根據(jù)域名自動(dòng)轉(zhuǎn)發(fā)到相應(yīng)服務(wù)器的特定端口”的效果,而路由器的端口映射做到的只是“根據(jù)不同端口自動(dòng)轉(zhuǎn)發(fā)到相應(yīng)服務(wù)器的特定端口”。
涉及的知識(shí):nginx編譯安裝,nginx反向代理基本配置,路由端口映射知識(shí),還有網(wǎng)絡(luò)域名等常識(shí)。
本次實(shí)驗(yàn)?zāi)繕?biāo)是做到:在瀏覽器中輸入xxx123.tk能訪問(wèn)到內(nèi)網(wǎng)機(jī)器192.168.10.38的3000端口,輸入xxx456.tk能訪問(wèn)到內(nèi)網(wǎng)機(jī)器192.168.10.40的80端口。
一、【簡(jiǎn)單配置】 nginx實(shí)現(xiàn)請(qǐng)求轉(zhuǎn)發(fā)
反向代理適用于很多場(chǎng)合,負(fù)載均衡是最普遍的用法。nginx 作為目前最流行的web服務(wù)器之一,可以很方便地實(shí)現(xiàn)反向代理。nginx 反向代理官方文檔: NGINX REVERSE PROXY
假設(shè)服務(wù)器域名為example.com,則對(duì)應(yīng)的 nginx http配置如下:
http {
server {
server_name example.com;
location /mail/ {
proxy_pass http://example.com:protmail/;
}
location /com/ {
proxy_pass http://example.com:portcom/main/;
}
location / {
proxy_pass http://example.com:portdefault;
}
}
}
以上的配置會(huì)按以下規(guī)則轉(zhuǎn)發(fā)請(qǐng)求( GET
和 POST
請(qǐng)求都會(huì)轉(zhuǎn)發(fā)):
- 將
http://example.com/mail/
下的請(qǐng)求轉(zhuǎn)發(fā)到http://example.com:portmail/
- 將
http://example.com/com/
下的請(qǐng)求轉(zhuǎn)發(fā)到http://example.com:portcom/main/
- 將其它所有請(qǐng)求轉(zhuǎn)發(fā)到
http://example.com:portdefault/
需要注意的是,在以上的配置中,webdefault 的代理服務(wù)器設(shè)置是沒(méi)有指定URI的,而 webmail 和 webcom 的代理服務(wù)器設(shè)置是指定了URI的(分別為 /
和 /main/
)。
如果代理服務(wù)器地址中是帶有URI的,此URI會(huì)替換掉 location
所匹配的URI部分。
而如果代理服務(wù)器地址中是不帶有URI的,則會(huì)用完整的請(qǐng)求URL來(lái)轉(zhuǎn)發(fā)到代理服務(wù)器。
以上配置的轉(zhuǎn)發(fā)示例:
-
http://example.com/mail/index.html
->http://example.com:portmail/index.html
-
http://example.com/com/index.html
->http://example.com:portcom/main/index.html
-
http://example.com/mail/static/a.jpg
->http://example.com:portmail/static/a.jpg
-
http://example.com/com/static/b.css
->http://example.com:portcom/main/static/b.css
-
http://example.com/other/index.htm
->http://example.com:portdefault/other/index.htm
二、詳細(xì)配置
給文件夾授權(quán)
chown -R www:www /usr/local/nginx
chown -R www:www /usr/local/nginx
修改配置文件
vim nginx.conf
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; #緩沖區(qū)代理緩沖用戶端請(qǐng)求的最大字節(jié)數(shù),可以理解為保存到本地再傳給用戶
client_body_buffer_size 256k;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
proxy_connect_timeout 300s; #nginx跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí))
proxy_read_timeout 300s; #連接成功后,后端服務(wù)器響應(yīng)時(shí)間(代理接收超時(shí))
proxy_send_timeout 300s;
proxy_buffer_size 64k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小
proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁(yè)平均在32k以下的話,這樣設(shè)置
proxy_busy_buffers_size 64k; #高負(fù)荷下緩沖大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個(gè)值,將從upstream服務(wù)器傳遞請(qǐng)求,而不緩沖到磁盤(pán)
proxy_ignore_client_abort on; #不允許代理端主動(dòng)關(guān)閉連接
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;
}
}
}
編輯反向代理服務(wù)器配置文件:
vim /usr/local/nginx/conf/reverse-proxy.conf
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域名指向公司靜態(tài)IP,這樣就成功的做到了在瀏覽器中輸入xxx123.tk的時(shí)候訪問(wèn)的內(nèi)網(wǎng)服務(wù)器192.168.10.38的3000端口,輸入xxx456.tk訪問(wèn)192.168.10.40的80端口的作用。
如果想對(duì)后端機(jī)器做負(fù)載均衡,像下面這配置就可以把對(duì)nagios.xxx123.tk的請(qǐng)求分發(fā)給內(nèi)網(wǎng)的131和132這兩臺(tái)機(jī)器做負(fù)載均衡了。
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;
}
額,關(guān)于負(fù)載均衡和緩存就不多說(shuō)了,這里只是要起到一個(gè)簡(jiǎn)單的“域名轉(zhuǎn)發(fā)”功能。
另外,由于http請(qǐng)求最后都是由反向代理服務(wù)器傳遞給后段的機(jī)器,所以后端的機(jī)器原來(lái)的訪問(wèn)日志記錄的訪問(wèn)IP都是反向代理服務(wù)器的IP。
要想能記錄真實(shí)IP,需要修改后端機(jī)器的日志格式,這里假設(shè)后端也是一臺(tái)nginx:
在后端配置文件里面加入這一段即可:
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;
遇到的問(wèn)題
之前沒(méi)配置下面這段,訪問(wèn)時(shí)候偶爾會(huì)出現(xiàn)504 gateway timeout,由于偶爾出現(xiàn),所以不太好排查
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;
可能出現(xiàn)的報(bào)錯(cuò)
.upstream timed out (110: Connection timed out) while reading response header from upstream, client: ...(后面的省略)
從日志看來(lái)是連接超時(shí)了,網(wǎng)上一通亂查之后估計(jì)可能是后端服務(wù)器響應(yīng)超時(shí)了,本著大膽假設(shè),小心求證的原則,既然假設(shè)了錯(cuò)誤原因就要做實(shí)驗(yàn)重現(xiàn)錯(cuò)誤:那就調(diào)整代理超時(shí)參數(shù),反過(guò)來(lái)把代理超時(shí)閥值設(shè)小(比如1ms)看會(huì)不會(huì)次次出現(xiàn)504。后來(lái)發(fā)現(xiàn)把proxy_read_timeout 這個(gè)參數(shù)設(shè)置成1ms的時(shí)候,每次訪問(wèn)都出現(xiàn)504。于是把這個(gè)參數(shù)調(diào)大,加入上面那段配置,解決問(wèn)題了。