nginx+tomcat單個(gè)域名及多個(gè)域名配置教程
項(xiàng)目開發(fā)接近尾聲,開始著手在生產(chǎn)環(huán)境部署項(xiàng)目,開發(fā)階段部署項(xiàng)目都沒用nginx。項(xiàng)目是采用SOA架構(gòu),多系統(tǒng)開發(fā),主要包括服務(wù)系統(tǒng)、中臺(tái)系統(tǒng)、后臺(tái)系統(tǒng)、金融系統(tǒng)、接口系統(tǒng)、調(diào)度系統(tǒng)、報(bào)表系統(tǒng)等。這類分布式的系統(tǒng),一般也都會(huì)用到nginx來做負(fù)載均衡。
從公司剛成立就進(jìn)來,趕鴨子上架來做架構(gòu)師,負(fù)責(zé)公司的所有研發(fā)事情,搭建公司的整個(gè)技術(shù)架構(gòu),起初的所有核心業(yè)務(wù)代碼基本都由自己親自把關(guān)來進(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í)候都有過無助和苦惱,因?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ā)人員。
回想這一路,覺得之前看似爬不過去的山也不過如此,也許這就是成長吧,成長總是會(huì)伴隨些許汗水與淚水吧。由于是負(fù)責(zé)團(tuán)隊(duì)的所有事情,所以數(shù)據(jù)庫的維護(hù)、遷移數(shù)據(jù)、建索引等性能優(yōu)化,項(xiàng)目部署等所有事情必須得一肩挑,不要問我為什么公司沒有DBA?為什么沒有運(yùn)維?我真的只能給你一個(gè)眼神,讓你慢慢去體會(huì)。
話不多說,直接開始技術(shù)干貨分享。
nginx做負(fù)載均衡的優(yōu)勢網(wǎng)上有很多介紹資料,這里我不再多做介紹。因?yàn)橛泻芏嘞到y(tǒng)要部署,涉及到域名、二級域名、多個(gè)域名等的部署。在實(shí)際的部署由于對nginx的不夠熟悉,遇到過很多坑,其中這種多域名的配置,xxxx.com轉(zhuǎn)發(fā)到www.xxxx.com、訪問域名轉(zhuǎn)發(fā)到tomcat里的項(xiàng)目等,現(xiàn)在先總結(jié)一部坑的解決辦法。
如將xxxx.com這個(gè)域名指向8082端口里的tomcat項(xiàng)目,在做這個(gè)介紹前先講個(gè)插曲,如訪問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; #} }
上面的配置還包括了訪問xxxx.com轉(zhuǎn)向www.xxxx.com的配置,如下:
server { server_name xxxx.com; rewrite ^(.*) http://www.xxxx.com$1 permanent; }
nginx的基本配置大致就是這樣,如果綁定多個(gè)域名(不管是一級域名還是二級域名),需配置多個(gè)server,你會(huì)發(fā)現(xiàn)這幾個(gè)server配置都差不多,主要是更改server_name及proxy_pass指向即可。upstream節(jié)點(diǎn)其實(shí)就是代理服務(wù)的訪問路徑。
如果此時(shí)訪問域名,你會(huì)發(fā)現(xiàn)nginx的配置生效了,只是目前顯示的是tomcat的默認(rèn)界面。nginx的配置基本就這樣了,接下來對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,訪問域名就指向了tomcat里的項(xiàng)目。
總結(jié)
以上所述是小編給大家介紹的nginx+tomcat單個(gè)域名及多個(gè)域名配置,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Nginx + Tomcat實(shí)現(xiàn)請求動(dòng)態(tài)數(shù)據(jù)和請求靜態(tài)資源的分離詳解
- tomcat+nginx域名配置方法
- nginx https反向代理tomcat的2種實(shí)現(xiàn)方法
- 詳解實(shí)現(xiàn)Nginx+Tomcat實(shí)現(xiàn)單IP、多域名、多站點(diǎ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)文章
Nginx+Tomcat關(guān)于Session的管理的實(shí)現(xiàn)
本篇文章主要介紹了Nginx+Tomcat關(guān)于Session的管理,通過實(shí)例的方式循序漸進(jìn)的介紹了幾種管理session的方式。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06IDEA中Tomcat在控制臺(tái)亂碼問題及IDEA編碼設(shè)置UTF-8的方法
這篇文章主要介紹了IDEA中Tomcat在控制臺(tái)亂碼問題及IDEA編碼設(shè)置UTF-8的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Tomcat與JDK版本對應(yīng)關(guān)系以及Tomcat各版本特性
這篇文章主要介紹了Tomcat與JDK版本對應(yīng)關(guān)系以及Tomcat各版本特性,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11解決Tomcat?Caused?by:?java.lang.ClassNotFoundException:?ja
這篇文章主要給大家介紹了如何解決Tomcat?Caused?by:?java.lang.ClassNotFoundException:?java.util.logging.Logger的問題,文中有詳細(xì)的原因分析及解決方法,需要的朋友可以參考下2023-10-10idea發(fā)布web項(xiàng)目后Tomcat服務(wù)器找不到該項(xiàng)目的問題及解決方法
這篇文章主要介紹了idea發(fā)布web項(xiàng)目后Tomcat服務(wù)器找不到該項(xiàng)目,本文給大家分享解決方案,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09新版Eclipse集成Tomcat時(shí)找不到server選項(xiàng)的解決方法
這篇文章主要給大家分享了新版Eclipse集成Tomcat時(shí)找不到server選項(xiàng)的解決方案,文章通過圖文介紹講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10