配置nginx+tomcat

来源:互联网 发布:编辑矢量图的软件 编辑:程序博客网 时间:2024/04/30 03:58

配置nginx并指定默认的jsp目录,官方给了一个jetty的例子

目标:域名http://app29551.qzoneapp.com 直接访问tomcat下的jiuchongju目录的项目

假设tomcat的服务端口为8080,nginx端口为80


1、主要是修改nginx的配置文件,这里我让nginx将请求转发给tomcat,并指定默认的请求文件

[html] view plaincopy
  1. #user  nobody;  
  2. #modify by kongqz from 1 to 4  
  3. worker_processes  4;  
  4.   
  5. #error_log  logs/error.log;  
  6. #error_log  logs/error.log  notice;  
  7. #error_log  logs/error.log  info;  
  8.   
  9. #pid        logs/nginx.pid;  
  10.   
  11.   
  12. events {  
  13.    #modify by kongqz  
  14.     use epoll;  
  15.     worker_connections  2048;  
  16.   
  17. }  
  18.   
  19.   
  20. http {  
  21.     include       mime.types;  
  22.     default_type  application/octet-stream;  
  23.   
  24.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  25.     #                  '$status $body_bytes_sent "$http_referer" '  
  26.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  27.   
  28.     #access_log  logs/access.log  main;  
  29.   
  30.     sendfile        on;  
  31.     #tcp_nopush     on;  
  32.   
  33.     #keepalive_timeout  0;  
  34.     keepalive_timeout  65;  
  35.   
  36.     #gzip  on;  
  37.      #  upstream  jiuchongju  {  
  38.       #     server   localhost:8080;  
  39.       #     ip_hash;  
  40.       # }  
  41.   
  42.     server {  
  43.         listen       80;  
  44.         server_name  app29551.qzoneapp.com;  
  45.   
  46.         #charset koi8-r;  
  47.   
  48.         #access_log  logs/host.access.log  main;  
  49.   
  50.     root   /usr/local/services/tomcat6/webapps/jiuchongju/;  
  51.   
  52.         location / {  
  53.               
  54.              index  index_tel.jsp index.jsp index.html index.htm ;  
  55.   
  56.              proxy_redirect          off;    
  57.              proxy_set_header        Host            $host;    
  58.              proxy_set_header        X-Real-IP       $remote_addr;    
  59.              proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;    
  60.              client_max_body_size    10m;    
  61.              client_body_buffer_size 128k;    
  62.              proxy_buffers           32 4k;  
  63.              proxy_connect_timeout   3;    
  64.              proxy_send_timeout      30;    
  65.              proxy_read_timeout      30;    
  66.              proxy_pass http://127.0.0.1:8080;  
  67.         }  
  68.   
  69.         #error_page  404              /404.html;  
  70.   
  71.         # redirect server error pages to the static page /50x.html  
  72.         #  
  73.         error_page   500 502 503 504  /50x.html;  
  74.         location = /50x.html {  
  75.             root   html;  
  76.         }  
  77.   
  78.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  79.         #  
  80.         #location ~ \.php$ {  
  81.         #    proxy_pass   http://127.0.0.1;  
  82.         #}  
  83.   
  84.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  85.         #  
  86.         #location ~ \.php$ {  
  87.         #    root           html;  
  88.         #    fastcgi_pass   127.0.0.1:9000;  
  89.         #    fastcgi_index  index.php;  
  90.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  91.         #    include        fastcgi_params;  
  92.         #}  
  93.   
  94.         # deny access to .htaccess files, if Apache's document root  
  95.         # concurs with nginx's one  
  96.         #  
  97.         #location ~ /\.ht {  
  98.         #    deny  all;  
  99.         #}  
  100.     }  


2、配置tomcat,指定默认的项目目录。其实就是修改tomcat的conf下的server.xml文件

[html] view plaincopy
  1. <Host name="localhost"  appBase="webapps"  
  2.       unpackWARs="true" autoDeploy="true"  
  3.       xmlValidation="false" xmlNamespaceAware="false">  
  4.   
  5.       <Context docBase="jiuchongju" path="/" reloadable="true" />        
  6.   
  7.   <!-- SingleSignOn valve, share authentication between web applications  
  8.        Documentation at: /docs/config/valve.html -->  
  9.   <!-- 
  10.   <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> 
  11.   -->  
  12.   
  13.   <!-- Access log processes all example.  
  14.        Documentation at: /docs/config/valve.html -->  
  15.   <!--  
  16.   <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"    
  17.          prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>  
  18.   -->  
  19.   
  20. </Host>  


3、配置项目的默认访问页,这里我想让请求当前项目的默认访问页面为index_tel.jsp

定义项目目录下的WEB-INF/web.xml文件

[html] view plaincopy
  1. <welcome-file-list>  
  2. <welcome-file>index_tel.jsp</welcome-file>  
  3.     <welcome-file>index.jsp</welcome-file>  
  4. </welcome-file-list>  

这样就会先寻找index_tel.jsp作为默认页,找不到后会访问 index.jsp
0 0
原创粉丝点击