1、思路:根据不同的URL去跳转到不同的系统中
2、分析:
$request_uri代表的是URL地址除去“http://域名”字符串以后剩下的字符串,例如:URL地址为:
http://192.154.222.191:8000/itoo-basic-majorchoosecourse-web/settingMajorCourse
,则$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
user 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.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功能,感觉它能实现,正在研究中。