nginx反向代理多個系統
阿新 • • 發佈:2019-01-10
1、思路:根據不同的URL去跳轉到不同的系統中
2、分析:
$request_uri代表的是URL地址除去“http://域名”字串以後剩下的字串,例如:URL地址為:
,則$request_uri代表的是itoo-basic-majorchoosecourse-web/settingMajorCourse這個字串。
根據if判斷語句進入到不同系統。
$request_uri ~ "^\/itoo-basic":開頭是”/itoo-basic”的進入basic_server。
$request_uri ~"^\/itoo-authority":開頭是”/itoo- authority”的進入authority _server。
3、配置
如果再新增一個系統,只要在nginx.conf配置檔案中再新增upstream和if語句,例如再新增一個新生,則可以寫:
upstream freshmen _server {
server ip:埠
}
if ( $request_uri ~ "^\/ itoo-freshmen" )
{
proxy_pass http:// freshmen _server;
}
完整的反向代理的配置檔案:
nginx.conf
upstream.confuser nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; 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 /var/log/nginx/access.log main; source_charset GB2312; server_names_hash_bucket_size 256; client_header_buffer_size 256k; large_client_header_buffers 4 256k; #size limits client_max_body_size 50m; client_body_buffer_size 256k; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; #引數都有所調整.目的是解決代理過程中出現的一些502 499錯誤 sendfile on; tcp_nopush on; keepalive_timeout 120; #引數加大,以解決做代理時502錯誤 tcp_nodelay on; #gzip on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/upstream.conf; include /etc/nginx/server.conf; }
upstream tomcat_server {
server IP地址:埠;
}
upstream authority_server {
server IP地址:埠;
}
upstream.conf
其實這三個配置檔案可以寫在一個配置檔案中,為了看著結構清晰,寫在三個檔案中。server { listen 8088; server_name 192.168.22.160; charset GB2312; index index.html index.htm; root /date/wwwroot/linuxtone/; location ~ ^/NginxStatus/ { stub_status on; access_log off; } location / { root /date/wwwroot/linuxtone/; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 50m; client_body_buffer_size 256k; proxy_connect_timeout 30; proxy_send_timeout 30; proxy_read_timeout 60; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; proxy_max_temp_file_size 128m; if ( $request_uri ~ "^\/itoo-basic" ) { proxy_pass http://tomcat_server; } if ( $request_uri ~ "^\/itoo-authority" ) { proxy_pass http://authority_server; } } #Add expires header for static content location ~* \.(jpg|jpeg|gif|png|swf)$ { if (-f $request_filename) { root /date/wwwroot/linuxtone/; expires 1d; break; } } }
4、其它辦法
有一種思路,nginx的rewrite功能,感覺它能實現,正在研究中。