nginx+tomcat單個(gè)域名及多個(gè)域名配置教程
項(xiàng)目開(kāi)發(fā)接近尾聲,開(kāi)始著手在生產(chǎn)環(huán)境部署項(xiàng)目,開(kāi)發(fā)階段部署項(xiàng)目都沒(méi)用nginx。項(xiàng)目是采用SOA架構(gòu),多系統(tǒng)開(kāi)發(fā),主要包括服務(wù)系統(tǒng)、中臺(tái)系統(tǒng)、后臺(tái)系統(tǒng)、金融系統(tǒng)、接口系統(tǒng)、調(diào)度系統(tǒng)、報(bào)表系統(tǒng)等。這類(lèi)分布式的系統(tǒng),一般也都會(huì)用到nginx來(lái)做負(fù)載均衡。
從公司剛成立就進(jìn)來(lái),趕鴨子上架來(lái)做架構(gòu)師,負(fù)責(zé)公司的所有研發(fā)事情,搭建公司的整個(gè)技術(shù)架構(gòu),起初的所有核心業(yè)務(wù)代碼基本都由自己親自把關(guān)來(lái)進(jìn)行編碼。系統(tǒng)也從最初的只有一個(gè)pc端,發(fā)展到如今pc中臺(tái)、后臺(tái)、android端3個(gè)app、iOS端3個(gè)app,產(chǎn)品越做越多,親自負(fù)責(zé)招聘面試、培訓(xùn)。之前很多時(shí)候都有過(guò)無(wú)助和苦惱,因?yàn)樨?fù)責(zé)公司整個(gè)架構(gòu),又要負(fù)責(zé)核心業(yè)務(wù)的編碼,技術(shù)難點(diǎn)的攻克,新員工的招聘及培訓(xùn),現(xiàn)在團(tuán)隊(duì)已經(jīng)都發(fā)展到16個(gè)人,而且這全是研發(fā)人員。
回想這一路,覺(jué)得之前看似爬不過(guò)去的山也不過(guò)如此,也許這就是成長(zhǎng)吧,成長(zhǎng)總是會(huì)伴隨些許汗水與淚水吧。由于是負(fù)責(zé)團(tuán)隊(duì)的所有事情,所以數(shù)據(jù)庫(kù)的維護(hù)、遷移數(shù)據(jù)、建索引等性能優(yōu)化,項(xiàng)目部署等所有事情必須得一肩挑,不要問(wèn)我為什么公司沒(méi)有DBA?為什么沒(méi)有運(yùn)維?我真的只能給你一個(gè)眼神,讓你慢慢去體會(huì)。
話不多說(shuō),直接開(kāi)始技術(shù)干貨分享。
nginx做負(fù)載均衡的優(yōu)勢(shì)網(wǎng)上有很多介紹資料,這里我不再多做介紹。因?yàn)橛泻芏嘞到y(tǒng)要部署,涉及到域名、二級(jí)域名、多個(gè)域名等的部署。在實(shí)際的部署由于對(duì)nginx的不夠熟悉,遇到過(guò)很多坑,其中這種多域名的配置,xxxx.com轉(zhuǎn)發(fā)到www.xxxx.com、訪問(wèn)域名轉(zhuǎn)發(fā)到tomcat里的項(xiàng)目等,現(xiàn)在先總結(jié)一部坑的解決辦法。
如將xxxx.com這個(gè)域名指向8082端口里的tomcat項(xiàng)目,在做這個(gè)介紹前先講個(gè)插曲,如訪問(wèn)xxxx.com需轉(zhuǎn)向到www.xxxx.com,這一點(diǎn)很多人都會(huì)忽略。
現(xiàn)在如果要部署中臺(tái)、后臺(tái)、金融系統(tǒng),找到nginx/conf/nginx.conf,修改配置:
upstream web{
server localhost:8082;
}
upstream admin{
server localhost:8083;
}
upstream finance{
server localhost:8084;
}
server {
listen 80;
server_name finance.xxxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://finance;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
}
}
server {
listen 80;
server_name www.xxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://web;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
#}
}
server {
server_name xxxx.com;
rewrite ^(.*) http://www.xxxx.com$1 permanent;
}
server {
listen 80;
server_name admin.xxxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://admin;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
#}
}
上面的配置還包括了訪問(wèn)xxxx.com轉(zhuǎn)向www.xxxx.com的配置,如下:
server {
server_name xxxx.com;
rewrite ^(.*) http://www.xxxx.com$1 permanent;
}
nginx的基本配置大致就是這樣,如果綁定多個(gè)域名(不管是一級(jí)域名還是二級(jí)域名),需配置多個(gè)server,你會(huì)發(fā)現(xiàn)這幾個(gè)server配置都差不多,主要是更改server_name及proxy_pass指向即可。upstream節(jié)點(diǎn)其實(shí)就是代理服務(wù)的訪問(wèn)路徑。
如果此時(shí)訪問(wèn)域名,你會(huì)發(fā)現(xiàn)nginx的配置生效了,只是目前顯示的是tomcat的默認(rèn)界面。nginx的配置基本就這樣了,接下來(lái)對(duì)tomcat做些配置的修改。找到tomcat里的conf/server.xml,注釋掉默認(rèn)的Host配置,添加如下Host配置:
<Host name="localhost" appBase="E:\tomcat\apache-tomcat-8.0.35-8082\webapps\web" deployOnStartup ="false" autoDeploy="false" unpackWARs="true">
<Context path="/" docBase="E:\tomcat\apache-tomcat-8.0.35-8082\webapps\web" />
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
以上是windows服務(wù)器下的配置,如為linux,只需更改appBase和docBase,指向項(xiàng)目的路徑。tomcat的配置也已經(jīng)完成,重啟tomcat,訪問(wèn)域名就指向了tomcat里的項(xiàng)目。
總結(jié)
以上所述是小編給大家介紹的nginx+tomcat單個(gè)域名及多個(gè)域名配置,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Nginx + Tomcat實(shí)現(xiàn)請(qǐng)求動(dòng)態(tài)數(shù)據(jù)和請(qǐng)求靜態(tài)資源的分離詳解
- tomcat+nginx域名配置方法
- nginx https反向代理tomcat的2種實(shí)現(xiàn)方法
- 詳解實(shí)現(xiàn)Nginx+Tomcat實(shí)現(xiàn)單IP、多域名、多站點(diǎn)的訪問(wèn)
- linux服務(wù)器部署tomcat和Nginx的教程
- 詳解nginx 配置多個(gè)tomcat共用80端口
- nginx實(shí)現(xiàn)tomcat動(dòng)靜分離詳解
- 詳解基于Centos7+Nginx+Tomcat8的負(fù)載均衡服務(wù)器的搭建
- LINUX中NGINX反向代理下的TOMCAT集群(詳解)
- Tomcat獲取Nginx反向代理的客戶端域名
相關(guān)文章
tomcat虛擬主機(jī)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
對(duì)于Tomcat服務(wù)器,可以放置多個(gè)網(wǎng)站(多個(gè)web應(yīng)用),這就是講配置多個(gè)虛擬主機(jī),可以看成好像有多個(gè)主機(jī),而每個(gè)主機(jī)上有一個(gè)web應(yīng)用2017-07-07
Tomcat中catalina.out 和 catalina.log的區(qū)別和用途詳解
本文主要介紹了Tomcat中catalina.out 和 catalina.log的區(qū)別和用途詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Eclipse啟動(dòng)Tomcat后無(wú)法訪問(wèn)項(xiàng)目解決辦法
這篇文章主要介紹了Eclipse啟動(dòng)Tomcat后無(wú)法訪問(wèn)項(xiàng)目解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04
一文詳解tomcat是如何處理HTTP長(zhǎng)連接的
HTTP長(zhǎng)連接,也稱為持久連接,是一種使用同一個(gè)TCP連接來(lái)發(fā)送和接收多個(gè)HTTP請(qǐng)求/應(yīng)答的方法,那么tomcat作為最常用的WEB容器,是怎么處理HTTP的長(zhǎng)連接呢,下面我們就來(lái)深入了解下吧2024-01-01
Tomcat配置HTTPS訪問(wèn)的實(shí)現(xiàn)步驟
本文主要介紹了Tomcat配置HTTPS訪問(wèn)的實(shí)現(xiàn)步驟,在tomcat中存在兩種證書(shū)驗(yàn)證情況單向驗(yàn)證和雙向驗(yàn)證,下面就詳細(xì)的介紹一下這兩種情況的配置,感興趣的可以了解一下2022-07-07
Tomcat解決catalina.out文件過(guò)大的問(wèn)題
本文主要介紹了Tomcat解決catalina.out文件過(guò)大的問(wèn)題,文件過(guò)大,不僅占系統(tǒng)的存儲(chǔ),我們還將無(wú)法使用過(guò)常規(guī)的編輯工具進(jìn)行查看,感興趣的可以了解一下2022-02-02

