為什么80%的碼農都做不了架構師?>>> ??
apt-get update -y
apt-get install wget -y
#下載nginx和相關軟件包 pcre是為了編譯rewrite模塊,zlib是為了支持gzip功能。額,這里nginx版本有點舊,因為我還要做升級nginx的實驗用。大家可以裝新版本。 cd /usr/local/srcwget?<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.gztar xf zlib-1.2.8.tar.gz
#安裝編譯環境 apt-get install build-essential libtool -y
#創建nginx用戶 所謂的unprivileged user 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_modulemakemake install
#給文件夾授權 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; #緩沖區代理緩沖用戶端請求的最大字節數,可以理解為保存到本地再傳給用戶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;}}
}
編輯反向代理服務器配置文件: 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域名指向公司靜態IP,這樣就成功的做到了在瀏覽器中輸入xxx123.tk的時候訪問的內網服務器192.168.10.38的3000端口,輸入xxx456.tk訪問192.168.10.40的80端口的作用。 如果想對后端機器做負載均衡,像下面這配置就可以把對nagios.xxx123.tk的請求分發給內網的131和132這兩臺機器做負載均衡了。 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: 在后端配置文件里面加入這一段即可: 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;
再看看原來日志的格式長什么樣: #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,由于偶爾出現,所以不太好排查
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。于是把這個參數調大,加入上面那段配置,解決問題了。 作者郵箱:790455803@qq.com,有問題可以直接右鍵作者,當然也可以加入我們ttlsa群單獨私聊或者群里發提問。 站點:運維生存時間??網址: http://www.ttlsa.com/html/4208.html