tomcat作为servlet容器,本身就是一个完整独立的服务器,nginx既可以作为单独的服务器也可以做为代理服务器来处理请求,nginx擅于处理静态请求。本例中,使用nginx+tomcat组合来进行部署,工作原理:
请求从客户端发送,首先由nginx接收,如果为静态请求(即请求静态文件比如图片、html页面或多媒体等),nginx直接处理,返回请求的文件;如果是动态请求(对应controller里面的RequestMapping),则nginx为代理将请求转发至tomcat,tomcat将处理结果返回nginx,再由nginx反馈给客户端。
通常一台服务器会部署多个应用,也就是拥有多个虚拟主机,在这种情况下如何判断请求是指向哪个应用的呢?判断依据就是请求头的‘Host’参数,如何实现,下面会作详细的介绍。。。
1.配置tomcat
tomcat/conf/server.xml格式:
<Server port="8005" shutdown="SHUTDOWN">
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.core.JasperListener"/>
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Service name="Catalina">
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
</Host>
</Engine>
</Service>
</Server>
请求头中的Host参数用来匹配<Host>标签中的name属性,匹配成功则由当前host处理请求
存在默认的host--localhost,要在同一个tomcat上同时运行多个应用,则要每个应用对应一个<Host>标签,并进行设置。假定$Tom_Home为tomcat根目录,tomcat上添加虚拟主机步骤:
在server.xml文件中添加<Host appBase=”$Tom_Home/app1” name=”www.xxx.com”>
还有其它的属性,可以根据要求设值,都有默认值,结果大体为:
<Host appBase="/home/dev/ProgramFiles/apache-tomcat-7.0.29/l-app" autoDeploy="true" name="www.lxcf.com" unpackWARs="true" deployOnStartup="false" deployIgnore="^(?!(manager)|(tomee)$).*">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt" />
</Host>
创建相关目录:$Tom_Home/app1、$Tom_Home/conf/Catalina/www.xxx.com(tomcat启动时也会根据host自动创建)
在$Tom_Home/conf/Catalina/www.lxcf.com文件中添加文件ROOT.xml用来定义应用context path,文件格式:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="" docBase="/home/dev/work/projects/lxcf/WebContent" />
docBase 的值为应用的根的绝对路径,path的值由tomcat自动设置
另一种配置方式是将context属性放在Host标签内,无需创建文件,也不用添加ROOT.xml文件:
<Host name="zbb.gtn.com" appBase="webapps/zbb">
<Context path="/" docBase="/home/dev/app/zbb/zbb_war_exploded" allowLinking="true" reloadable="false"/>
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="zbb_access_log." suffix=".txt"
pattern="common" resolveHosts="false"/>
</Host>
2.配置Nginx
声明一下nginx的作用,当客户端发来请求,首先有nginx接收,如果是动态请求则转交给tomcat处理;如果是请求的静态资源(ie 图片,css,js,多媒体等),则直接返回请求的资源
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;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
请求到达nginx时与server中的server_name进行匹配,匹配成功则由当前server处理该请求
存在一个默认的server名为localhost,并有示例说明
添加<server>并进行设置,如:
server {
listen 80;
root /home/dev/work/projects/xxx/WebContent;
server_name www.xxx.com;
client_max_body_size 100M;
location / {
rewrite ^/$ /home.do;
}
location ~ \.do$ {
proxy_pass http://www.xxx.com:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
root 指定到应用的根目录
3.启动tomcat和nginx
命令行启动tomcat $Tom_Home/bin 目录下有个startup.sh 文件,命令:sh startup.sh 即可启动
如果需要传参数到tomcat的话,则用:p=v sh startup.sh;终止tomcat:sh shutdown.sh
如果配置没问题的话,就可以实现同一台机器、一个tomcat、一个nginx下部署多个应用,即设置多个虚拟主机
相关推荐
├─2.01 Nginx进阶常见配置-虚拟主机+PHP-FPM.mp4 ├─2.02 Nginx进阶-常见配置-反向代理.mp4 ├─2.03 Nginx进阶基础-常见配置-反向代理.mp4 ├─2.04 Nginx进阶基础-upstream.mp4 ├─2.05 Nginx进阶基础-常用...
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理... 支持作为基本 HTTP 服务器的功能,例如日志、压缩、Byte ranges、Chunked responses、SSL、虚拟主机等等,应有尽有
虽然在课程中还讲解了部分HTTP协议的技术,但是课程的重点还是NGINX、JVM、Tomcat三相运维与配置技术。课程内容包括了Nginx...├─2.01 Nginx进阶常见配置-虚拟主机+PHP-FPM.mp4 ├─2.02 Nginx进阶-常见配置-反向代
#设定本虚拟主机的访问日志 access_log logs/host.access.log main; #对 "/" 所有应用启用负载均衡 location / { proxy_pass http://127.0.0.1; #保留用户真实信息 proxy_set_header X-Real-IP $...
由于工作的需求,在使用中,需要搭建负载均衡,研究了Apache+...手动安装好Nginx后,一切正常,虚拟主机,负载均衡的配置全部没有任何问题。顺利启用,具体的配置文件如下: 在conf的nginx.conf的文件中加入了:include
3.2 Nginx的虚拟主机配置 3.3 Nginx的日志文件配置与切割 3.4 Nginx的压缩输出配置 .3.5 Nginx的自动列目录配置 3.6 Nginx的浏览器本地缓存设置 第4章 Nginx与PHP(FastCGI)的安装、配置与优化 4.1 获取相关开源...
要求 Nginx可以通过域名构建虚拟主机的方式同时对外提供 http://www.example.com, http://erp.example.com, http://oa.example.com 三个网站的访问。 访问 http://www.example.com 网站返回的HTML代码“<h1> ...
3.2 Nginx的虚拟主机配置 3.3 Nginx的日志文件配置与切割 3.4 Nginx的压缩输出配置 .3.5 Nginx的自动列目录配置 3.6 Nginx的浏览器本地缓存设置 第4章 Nginx与PHP(FastCGI)的安装、配置与优化 4.1 获取相关...
详细介绍了Linux服务器上常用WEB应用环境安装与配置及优化,包括: nginx配置及优化; ...nginx+tomcat建立虚拟主机; ant环境配置; jdk环境配置; 按日期切割tomcat日志; 按日期切割nginx日志;
3.2 Nginx的虚拟主机配置 3.3 Nginx的日志文件配置与切割 3.4 Nginx的压缩输出配置 3.5 Nginx的自动列目录配置 3.6 Nginx的浏览器本地缓存设置 第4章 Nginx与PHP( FastCGI) 的安装、 配置与优化 4.1 获取相关开源...
第2.5篇-Nginx部分-虚拟主机配置 共12页 第2.6篇-Nginx部分-反向代理和负载均衡-反向代理配置 共13页 第2.6篇-Nginx部分-反向代理和负载均衡-负载均衡配置 共9页 第2.7篇-Nginx部分-Rewrite功能 共29页 第2.8篇-...
第2.5篇-Nginx部分-虚拟主机配置 共12页 第2.6篇-Nginx部分-反向代理和负载均衡-反向代理配置 共13页 第2.6篇-Nginx部分-反向代理和负载均衡-负载均衡配置 共9页 第2.7篇-Nginx部分-Rewrite功能 共29页 第2.8篇-...
A机器:zabbix服务端(192.168.234.128) B机器:zabbix客户端(192.168.234.125) 在B机器(zabbix客户端)操作: 编辑nginx虚拟主机配置文件: 在server{}中添加以下内容: 重载nginx配置
5.1.2 nginx虚拟主机配置 5.1.3 nginx性能优化 5.1.4 nginx参数深入理解 5.1.5 Nginx Rewrite规则 面面面面 5.2构建 Rsync同步服务器 5.2.1 Rsync服务端配置………… 5.2.2Rsyc基于SsH同步 5.2.3 Rsync实时同步配置 ...
第2.5篇-Nginx部分-虚拟主机配置 共12页 第2.6篇-Nginx部分-反向代理和负载均衡-反向代理配置 共13页 第2.6篇-Nginx部分-反向代理和负载均衡-负载均衡配置 共9页 第2.7篇-Nginx部分-Rewrite功能 共29页 第2.8篇-...
5.1. 2 Nginx 虚拟主机配置 74 5.1. 3 Nginx性能优化 75 5.1. 4 Nginx参数深入理解 77 5.1. 5 Nginx Rewrite规则 78 5. 2 构建Rsync同步服务器 80 5.2. 1 Rsync服务端配置 80 5.2. 2 Rsync基于SSH同步 83 5.2. 3 ...
第2.5篇-Nginx部分-虚拟主机配置 共12页 第2.6篇-Nginx部分-反向代理和负载均衡-反向代理配置 共13页 第2.6篇-Nginx部分-反向代理和负载均衡-负载均衡配置 共9页 第2.7篇-Nginx部分-Rewrite功能 共29页 第2.8篇-...
#设定本虚拟主机的访问日志 access_log logs/www.xx.com.access.log main; #默认请求 location / { root /root; #定义服务器的默认网站根目录位置 index index.php index.html index.htm; #定义首页索引文件...
第2.5篇-Nginx部分-虚拟主机配置 共12页 第2.6篇-Nginx部分-反向代理和负载均衡-反向代理配置 共13页 第2.6篇-Nginx部分-反向代理和负载均衡-负载均衡配置 共9页 第2.7篇-Nginx部分-Rewrite功能 共29页 第2.8篇-...
2、对系统常见应用软件有运维能力,如nginx、mysql、lvs,memcached、redis、tomcat、dns等,能够快速实施部署、配置及排错; 3、熟悉Docker、K8s等容器技术,有Docker集群实际维护经验; 4、能够独立维护大中型...