nginx实现tomcat端口转发

回复 收藏
本帖最后由 小辉辉 于 2016-7-25 17:06 编辑

需求

www.aaa.comwww.bbb.com访问不同的项目
比如IP/aaa用www.aaa.com、IP/b bb用www.bbb.com



搭建好tomcat1/tocmat2分别不同的的端口
比如:192.168.1.108:8888用www.aaa.com访问
      192.168.1.108:8080用www.bbb.com访问



搭建nginx

编译安装NGINX
yum -y install gcc gcc-c++ pcre-devel zlib-devel -y

cd nginx-1.2.4

./configure --prefix=/usr/local/nginx --with-http_stub_status_module&&make&&make install

NGINX控制脚本:
#!/bin/bash
# chkconfig: 35 11 99

PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"

case "$1" in
  start)
    $PROG ;;

  stop)
    kill -s QUIT $(cat $PIDF) ;;

  restart)
    $0 stop
    $0 start ;;
  
  reload)
    kill -s HUP $(cat $PIDF) ;;

       *)
  echo "USAGE: $0 {start|stop|restart|reload}"
  exit 1
esac
exit 0

chmod a+x nginx
cp nginx /etc/init.d
chkconfig --add nginx
chkconfig nginx on
service nginx start


nginx用1.6.1跟1.2都一样



实际环境

/usr/tomcat/apache-tomcat-7.0.42/webapps/zhishi    www.aaa.com/192.168.1.108:8888
/usr/tomcat/tomcat/apache-tomcat-7.0.42/webapps/shizhiban/    www.bbb.com/192.168.1.108:8080

nginx 目录    /usr/local/ nginx



conf/nginx.conf配置文件如下:
nginx.conf


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    access_log  nul;
    sendfile        on;
    keepalive_timeout  65;

   

    upstream tomcat-aaa {
        server localhost:8888;
    }
upstream tomcat-bbb {
        server localhost:8080;
    }
    include usr/local/nginx/conf/conf.d/*.conf;
}




conf/conf.d/default.conf

server {
        listen       80;
        server_name  www.aaa.com www.bbb.com;
location / {
     if ($host = 'www.aaa.com') {
                 proxy_pass http://tomcat-aaa;
      
}
if ($host = 'www.bbb.com') {
                 proxy_pass http://tomcat-bbb;
}
    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;
            client_max_body_size    100m;
            access_log off;
           
        }
      
}



然后重启nginx 就可以咯
2016-07-25 17:04 举报
已邀请:
0

Yajun

赞同来自:

挺好的  不错,

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: