转载自:https://blog.csdn.net/name_chc/article/details/73332272;亲测可用,加了一些注释;javascript
配置多个tomcat转发php
另附上tomcat启动慢的解决方案css
须要在jdk下面修改一个配置html
/usr/jdk/jdk1.7.0_80/jre/lib/securityjava
找到java.security这个文件里面的securerandom.sourcenode
修改里面的配置以下便可nginx
securerandom.source=file:/dev/./urandomshell
本服务器有3个相同的tomcat对外分别为8080端口8090端口9000端口浏览器
须要在服务器根目录下面找到tomcat
vi /etc/profile
增长里面的配置以下(建议在文件内容的末尾处增长)
TOMCAT_HOME_8090=/usr/tomcat/tomcat-8090
CATALINA_HOME_8090=/usr/tomcat/tomcat-8090
CATALINA_BASE_8090=/usr/tomcat/tomcat-8090
export TOMCAT_HOME_8090 CATALINA_HOME_8090 CATALINA_BASE_8090
TOMCAT_HOME=/usr/tomcat/tomcat-8080
CATALINA_HOME=/usr/tomcat/tomcat-8080
CATALINA_BASE=/usr/tomcat/tomcat-8080
export TOMCAT_HOME CATALINA_HOME CATALINA_BASE
TOMCAT_HOME_9000=/usr/tomcat/tomcat-9000
CATALINA_HOME_9000=/usr/tomcat/tomcat-9000
CATALINA_BASE_9000=/usr/tomcat/tomcat-9000
export TOMCAT_HOME_9000 CATALINA_HOME_9000 CATALINA_BASE_9000
增长完成后须要让配置文件的配置当即生效
执行命令source /etc/profile
而后修改tomcat的启动项和中止项的命令文件(演示一个tomcat的修改方式,其余tomcat修改方式相似)
startup.sh修改以下
须要在exec "$PRGDIR"/"$EXECUTABLE" start "$@"前增长以下配置(注:配置这些是由于tomcat由jvm实例启动,而启动jvm实例又经过shell脚本,故能够在shell脚本里配置用什么jre,启动参数什么的)
export JAVA_HOME=/usr/jdk/jdk1.7.0_80 #jdk的目录
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib
export CATALINA_HOME=$CATALINA_HOME_9000 #注意这里关联的是profile中刚刚设置的环境变量名(不一样的tomcat变量名称不同)
export CATALINA_BASE=$CATALINA_BASE_9000 #注意这里关联的是profile中刚刚设置的环境变量名(不一样的tomcat变量名称不同)
shutdown.sh修改以下
须要在exec "$PRGDIR"/"$EXECUTABLE" start "$@"前增长以下配置
export JAVA_HOME=/usr/jdk/jdk1.7.0_80 #jdk的目录
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib
export CATALINA_HOME=$CATALINA_HOME_9000 #注意这里关联的是profile中刚刚设置的环境变量名(不一样的tomcat变量名称不同)
export CATALINA_BASE=$CATALINA_BASE_9000 #注意这里关联的是profile中刚刚设置的环境变量名(不一样的tomcat变量名称不同)
注意端口号须要在tomcat下面的conf文件夹中修改server.xml(注:有多少个tomcat可能被启动就要修改多少个tomcat下的server.xml配置,后者tomcat启动会产生端口占用的状况)
修改端口号的地方为之前是8080如今被改为了9000
<Connector port="9000" protocol="HTTP/1.1"(注:这个地方还能够修改默认的字符编码,若是是tomcat8以上默认是utf8)
connectionTimeout="20000"
redirectPort="8443" />(注:这个redirectPort彷佛是若是以相似https访问8080端口则会被转接到8443端口上??,可是通常不会出现)
光修改端口号可不行还须要修改的配置以下
这里原始的是8005被修改为了8007(由于这里存在3个tomcat因此如今被修改为了8007)(注:是三个分别为8005/8006/8007,这个server port是相似rabbitmq中给ctl开放的端口,即我能够向这个端口发送shutdown命令来执行tomcat内部的关闭操做而不是kill)
<Server port="8007" shutdown="SHUTDOWN">
这里原始的是8009被修改为了8011(由于这里存在3个tomcat因此如今被修改为了8011)(注:即使本身没用到这个功能可是这里配置了那么启动tomcat时就会去监听它,故不能重复)
<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
以上条件知足的是不一样的tomcat都须要修改以上配置且不能相同
若是tomcat启动不报错就算完成了
好了tomcat须要修改的配置就完成了接下来是nginx的配置
注意:改配置文件的user配置可能与本身配的不一致(注:我我的的user是nginx,其实用root也能够)
nginx.config 配置参照以下如需直接使用须要修改为本身的域名便可
user www www; # 注:可不配 worker_processes 1; #设置值和CPU核心数一致 error_log /usr/nginx/logs/nginx_error.log crit; #日志位置和日志级别;注:可用相对路径,相对于nginx安装目录 pid /usr/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; # 注:这个是配置worker_processes能够打开的最大文件数,通常不须要这么高; events { use epoll; # 注:一个颇有名的组件,并为nginx实现只是用到了此工具 worker_connections 65535; # 最大tcp链接数 } http { include 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'; #charset gb2312; # 注server内也能够定义,这个是通用的?? server_names_hash_bucket_size 128; client_header_buffer_size 32k; # 注:这几行应该是指客户端请求数据的限制 large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip on; # 注:开启gzip,tomcat里能够不用开了,或者这里不开tomcat里开启(感受后者好一点,毕竟一个nginx服务于多个tomcat) gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; #下面是server虚拟主机的配置 server { listen 80;#监听端口 server_name app.sunmear.com;#域名 location / { root html; proxy_pass http://127.0.0.1:9000; } #解决没法显示图片的问题 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico|js|css)$ { root html; proxy_pass http://127.0.0.1:9000; expires 30d; # access_log off; } #解决的没法加载样式的问题 location ~ .*\.(js|css)?$ { root html; proxy_pass http://127.0.0.1:9000; expires 15d; # access_log off; }修改完成后须要先中止nginx而后再启动就好了(注:中止用./nginx -s quit|stop,且彷佛没有status功能,能够本身ps -ef|grep nginx)
# 这一块不清楚干吗用的 location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } access_log off; } server { listen 80;#监听端口 server_name www.sunmear.com;#域名 location / { root html; proxy_pass http://127.0.0.1:8080; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico|js|css)$ { root html; proxy_pass http://127.0.0.1:8080; expires 30d; # access_log off; } location ~ .*\.(js|css)?$ { root html; proxy_pass http://127.0.0.1:8080; expires 15d; # access_log off; } location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:8080; fastcgi_index index.php; include fastcgi.conf; } access_log off; # 注:关闭访问记录日志; } server { listen 80;#监听端口 server_name wap.sunmear.com;#域名(注:并不是context,浏览器客户端区分不了context,只能区分不一样的域名,context也会在请求行里而非host里) location / { root html; proxy_pass http://127.0.0.1:8090; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico|js|css)$ { root html; proxy_pass http://127.0.0.1:8090; expires 30d; # access_log off; } location ~ .*\.(js|css)?$ { root html; proxy_pass http://127.0.0.1:8090; expires 15d; # access_log off; } location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:8090; fastcgi_index index.php; include fastcgi.conf; } access_log off; } }