Linux服务器配置不同域名绑定不同的项目(Tomcat+Nginx实现)
目前有以下两个域名,都解析到 47.95.225.29 这台服务器
wx.qiansuida.com
www.jxmstory.com
服务器默认项目部署在Tomcat中,前端项目在Tomcat的ROOT目录下,后台项目在webapps文件夹下
这个时候用域名可以访问到ROOT里的index.html,访问后台项目则需要输入域名+项目名
如果想要再部署一个前端项目也要直接输入域名就可以访问怎么办?
同一端口
可以通过修改tomcat/conf下面的server.xml文件来实现域名绑定
在Engine里添加一段Host配置
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"
maxThreads="3000"
compression="on" compressableMimeType="text/html,text/xml"
/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
【defaultHost必须指向下面其中一个host的name,表示默认的访问都转到该虚拟主机】
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
【一个Host对应一个虚拟主机(在这里虚拟主机就是tomcat的一个项目目录) 】
【这里没有配置<Context></Context> 默认指向ROOT文件夹 】
<Host name="localhost" debug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
【新添加的Host 修改name为要绑定的域名 】
<Host name="www.jxmstory.com" debug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true">
【添加虚拟目录,如果项目放在webapps下,docBase为项目名,如果项目在其他文件夹下,要写全目录 】
<Context path="" docBase="Tour"></Context>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
现在输入域名 www.jxmstory.com 就可以访问到Tour项目下面
其他域名IP等均会默认访问ROOT
当然你也可以在第一个Host里面添加一个Context 修改默认虚拟主机路径为Tomcat外部路径/www/server(Linux系统路径)
<Context path="" docBase="/www/server"></Context>
或者修改Engine里的name来修改默认虚拟主机
<Engine name="Catalina" defaultHost="www.jxmstory.com">
Host 属性说明
name:虚拟主机的域名( localhost 代表本机指向127.0.0.1,域名通过外网ip最终都绑定到localhost)
autoDeploy:是否允许自动部署应用程序
unpackWARs:设置是否自动解压 war 包再运行,默认值是 true
appBase:表示这个目录下面的子目录将自动被部署为应用 一般都为 “webapps”
不同端口
上面我们实现的是在同一个Tomcat中来配置,所以端口号都是80
但是我另一个项目是SpringBoot打成jar包的形式 我们知道SpringBoot jar包是内置tomcat 打包项目时端口号就不能再设置为80端口了 假设端口为8081
通过 wx.qiansuida.com 访问 Tomcat默认项目
通过 www.jxmstory.com 访问 SpringBoot项目
现在我们就可以通过Nginx做映射,因为直接用域名访问到的都是80端口,那么在nginx就要监听80端口 :
如果监听地址是 www.jxmstory.com 则转向 http://127.0.0.1:8081/ (SpringBoot内置Tomcat)
47.95.225.29 或 wx.qiansuida.com 等则转向 http://127.0.0.1:8080/ (服务器Tomcat端口号要为8080)
具体配置方法是修改nginx.cnf 通常在/etc/nginx/nginx.conf
查看是否安装 没有安装nginx自行百度安装
nginx -v
查找nginx.conf
cd /
find -name nginx.conf
编辑
vi etc/nginx/nginx.conf
按i开始编辑 在http{}里添加server{}如下
编辑完成后按Esc 输入:wq 保存退出
重启nginx即可
systemctl restart nginx 或 nginx -s reload
没有加入到服务可以用 /etc/init.d/nginx restart
#user nobody;
worker_processes 1;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
client_max_body_size 100M;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8080/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80; 【监听端口】
server_name www.jxmstory.com; 【监听地址】
location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8081/; 【转向地址】
}
}
}
到此完美实现我的需求~~
nginx.conf 部分详解(下面一段转自网络)
########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
}
}
上一篇: View创建的那些事儿
下一篇: python学习—scipy知识点