设为首页 收藏本站
查看: 872|回复: 0

[经验分享] 第四十六天:nginx整合tomcat或resin

[复制链接]

尚未签到

发表于 2018-12-7 08:05:27 | 显示全部楼层 |阅读模式
  小Q:葡萄美酒夜光杯,欲饮琵琶马上催;醉卧沙场君莫笑,古来征战几人回;   --唐·张翰
  

  其实说白了就是借用了nginx的代理功能,代理了tomcat或resin的IP端口;即遇到jsp服务页面时,nginx把服务转向代理的tomcat或resin;

  在resin和apache整合是,apapche通过专用模块来访问resin的server端口,所以resin可以不用开启http服务; resin有个优势可以同时有多个http服务,实现负载均衡

  ----------------------------------nginx+tomcat-------------------------------------
  1.安装支持正则的pcre模块
  # rpm -ivh  pcre-devel-6.6-2.el5_1.7.i386.rpm
  2.安装nginx
  # tar zxvf nginx-0.7.62.tar.gz
  # cd nginx-0.7.62
  # ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  # make
  # make install
  3.启动nginx
  # /usr/local/nginx/sbin/nginx  或 /etc/init.d/nginx  start
  4.在/usr/local/nginx/conf下面添加文件proxy.conf

# cat /usr/local/nginx/confg/proxy.conf
proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr; #获取真实IP
#proxy_set_header       X-Forwarded-For   
$proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;  5.配置nginx.conf
# cat /usr/local/nginx/confg/nginx.conf
user  www www;
worker_processes  1;
pid     /usr/local/nginx/logs/nginx.pid;
events {
    use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    include     /usr/local/nginx/conf/proxy.conf;  #一定要指向代理文件
    sendfile        on;
    tcp_nopush      on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        charset gb2312;
        location / {
             root /www/web/ROOT;
             index  index.html index.htm;
        }
        location ~ .*.jsp$ {     #匹配以jsp结尾的,tomcat的网页文件是以jsp结尾         
                index   index.jsp;
                proxy_pass      http://127.0.0.1:8080; #主要在这里,设置一个代理
        }
        location /nginxstatus {
                stub_status on;
                access_log on;
                auth_basic "nginxstatus";
                auth_basic_user_file
/usr/local/nagois/etc/htpasswd.users;
        }
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}  6.测试
  在/www/web/ROOT下添加文件index.html
  # cat index.html
  the port:80
  重启nginx
  http://192.168.2.150
  http://192.168.2.150:8080
  http://192.168.2.150/index.jsp
  

  -----------------------------------nginx+resin---------------------------------
  1.首先确保nginx和resin都是安装正常的,安装完成后,两者都默认指向自己的默认页面即安装成功;
  2.整合更改配置文件
  其实可以把模块都放在nginx.conf配置文件中;复制nginx.conf成nginx-user.conf放到自己新建的conf/userconf目录下,再在这个目录新建两个文件upstream.conf(放负载均衡的配置),server.conf(放虚拟主机的配置)
#upstream.conf 内容如下:
    upstream  module {               #模块名对应下面的调用
        server 192.168.0.1:8081 max_fails=2 fail_timeout=10s weight=200;
        server 192.168.0.2:8080 max_fails=2 fail_timeout=10s weight=100;#server.conf内容如下:
      server {
                listen  80;
                server_name     localhost;
                charset utf-8;
                proxy_connect_timeout   1200;
                proxy_send_timeout      1200;
                root  /data/www;   #网站路径
                location / {
                        index index.jsp index.htm index.html;
                        proxy_pass http://module/app;   #代理的路径
                        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;
                }
        }  nginx-user.conf将原来有的虚拟主机配置去掉加入新如下内容:
  include userconf/server.conf;
  include userconf/upstream.conf;
  重启:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/userconf/nginx-user.conf
  3.另一种配置语句(参考)
  vi /usr/local/resin/conf/resin.conf

  4.测试
  mkdir -p /data/www
  vi index.html
  加入以下内容
  Hello,welcom jnjkdds!
  重启nginx和resin,浏览器访问http://ip应该就能看到了。
  (5.实现负载均衡)
  nginx只能代理resin的http服务,所以要把nginx和resin整合起来的话,resin一定要启动http服务。
  在resin3.0的版本中可以直接起多个http服务,例如

   
   
   
   
      在nginx.conf里面配
upstream APPSRV {
        server   127.0.0.1:6081 max_fails=2 fail_timeout=10s weight=200;
        server   127.0.0.1:6082 max_fails=2 fail_timeout=10s weight=200;
        server   127.0.0.1:6083;
         server   127.0.0.1:6084;
        server   127.0.0.1:6085;
        server   127.0.0.1:6086;
    }  然后再对应server里面加入类似:

location ~ ^/xxx/.*\.jsp$ {
        proxy_pass http://APPSRV;
    }  来达到负载均衡,但是在3.1以后resin不认
  这种配置,所以nginx和resin3.1的整合要换下配置

        
   
   
        
   
   
        
   
   
        
      通过把server端口和http端口绑定起来,这样nginx就可以通过http来访问resin的server了
  

  

  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-644146-1-1.html 上篇帖子: tomcat启动startup.bat一闪而过(最牛的解决办法) 下篇帖子: 使用Logstash multiline 收集PHP、tomcat等应用服务多行堆栈日志
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表