nginx配置域名后的二級(jí)目錄訪問(wèn)不同項(xiàng)目的配置操作
場(chǎng)景描述:
通過(guò)二級(jí)目錄(虛擬目錄,應(yīng)用程序)的方式訪問(wèn)同一ip+端口的不同應(yīng)用,例如location是用戶使用頁(yè)面,location/admin/是管理頁(yè)面,location部署在192.168.1.100的80端口,location/admin部署在172.20.1.32的8080端口上。
解決方案:
使用nginx反向代理,配置如下:
server { listen 80; server_name demo.domain.com; #通過(guò)訪問(wèn)service二級(jí)目錄來(lái)訪問(wèn)后臺(tái) location /service { #DemoBackend1后面的斜杠是一個(gè)關(guān)鍵,沒(méi)有斜杠的話就會(huì)傳遞service到后端節(jié)點(diǎn)導(dǎo)致404 proxy_pass http://DemoBackend1/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #其他路徑默認(rèn)訪問(wèn)前臺(tái)網(wǎng)站 location / { proxy_pass http://DemoBackend2; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } #簡(jiǎn)單的負(fù)載均衡節(jié)點(diǎn)配置 upstream DemoBackend1 { server 192.168.1.1; server 192.168.1.2; ip_hash; } upstream DemoBackend2 { server 192.168.2.1; server 192.168.2.2; ip_hash; }
但是這種方式,二級(jí)目錄的樣式文件都不會(huì)正常顯示,他們不會(huì)自動(dòng)在二級(jí)目錄下查找,而是在根目錄中查找,在跳轉(zhuǎn)頁(yè)面的時(shí)候也會(huì)報(bào)404錯(cuò)誤。不知道是不是配置有誤,在server塊中配置了root或是rewrite都不能解決。
試著在proxy_pass后面加上二級(jí)目錄,并且和location塊的二級(jí)目錄相同,配置如下:
server { listen 80; server_name demo.domain.com; #通過(guò)訪問(wèn)service二級(jí)目錄來(lái)訪問(wèn)后臺(tái) location /service { #DemoBackend1后面的斜杠是一個(gè)關(guān)鍵,沒(méi)有斜杠的話就會(huì)傳遞service到后端節(jié)點(diǎn)導(dǎo)致404 proxy_pass http://DemoBackend1/service;#DemoBackend1網(wǎng)站中要配置一個(gè)名稱為service的虛擬目錄,并且和location的二級(jí)目錄名稱一致 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #其他路徑默認(rèn)訪問(wèn)前臺(tái)網(wǎng)站 location / { proxy_pass http://DemoBackend2; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } #簡(jiǎn)單的負(fù)載均衡節(jié)點(diǎn)配置 upstream DemoBackend1 { server 192.168.1.1; server 192.168.1.2; ip_hash; } upstream DemoBackend2 { server 192.168.2.1; server 192.168.2.2; ip_hash; }
問(wèn)題解決
另外,在實(shí)際應(yīng)用中,我使用了asp.net 的mvc,將mvc設(shè)置為網(wǎng)站的方式?jīng)]有問(wèn)題,如果是虛擬目錄的方式就會(huì)找不到路徑,是因?yàn)樽约涸诰W(wǎng)站中的地址很多寫(xiě)的都不規(guī)范,正確的方式應(yīng)該是:
Here's a typical example of what you should never do:
<script type="text/javascript"> $.ajax({ url: '/home/index' }); </script> and here's how this should be done: <script type="text/javascript"> $.ajax({ url: '@Url.Action("index", "home")' }); </script> Here's another typical example of something that you should never do: <a href="/home/index" rel="external nofollow" >Foo</a> and here's how this should be written: @Html.ActionLink("Foo", "Index", "Home") Here's another example of something that you should never do: <form action="/home/index" method="opst"> </form> and here's how this should be written: @using (Html.BeginForm("Index", "Home")) { }
補(bǔ)充知識(shí):使用nginx服務(wù)器,實(shí)現(xiàn)同一IP同一端口訪問(wèn)不同項(xiàng)目,以域名區(qū)分所訪問(wèn)項(xiàng)目
這里我使用了兩臺(tái)nginx服務(wù)器,一臺(tái)服務(wù)器將不同項(xiàng)目綁定到不同端口,一臺(tái)服務(wù)器將不同域名分發(fā)到不同端口的項(xiàng)目上。
第一臺(tái)nginx的conf文件server部分:
server { listen 8000; server_name localhost; root E:/test/pro1; location / { index index.html index.htm; } } server { listen 8001; server_name localhost; root E:/test/pro2; location / { index index.html index.htm; } }
第二臺(tái)nginx的conf文件的server部分:
server { listen 80; server_name www.testpro01.com testpro01.com; location / { proxy_pass http://127.0.0.1:8000; } } server { listen 80; server_name www.testpro02.com testpro02.com; location / { proxy_pass http://127.0.0.1:8001/; } }
最后用bat文件用以對(duì)兩個(gè)nginx服務(wù)器進(jìn)行操作
啟動(dòng)文件如下:start.bat
@echo off echo [start...] cd /d E: cd spiovnet\nginx-1.16.1 call start nginx.exe cd /d D: cd nginx-1.16.1 call start nginx.exe echo [end...] @pause
其他的雷同,只是命令不一樣
nginx啟動(dòng)命令:start nginx.exe 或者 nginx
nginx重新加載配置命令:nginx -s reload
ngin重啟命令:nginx -s reopen
ngin關(guān)閉命令:nginx -s stop
以上這篇nginx配置域名后的二級(jí)目錄訪問(wèn)不同項(xiàng)目的配置操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue?列表過(guò)濾與排序的實(shí)現(xiàn)
這篇文章主要介紹了Vue?列表過(guò)濾與排序的實(shí)現(xiàn),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值需要的小伙伴可以參考一下2022-05-05Vue響應(yīng)式添加、修改數(shù)組和對(duì)象的值
有些時(shí)候,不得不想添加、修改數(shù)組和對(duì)象的值,但是直接添加、修改后又失去了getter、setter,由于JavaScript的限制, Vue不能檢測(cè)以部分變動(dòng)的數(shù)組2017-03-03使用Vue組件實(shí)現(xiàn)一個(gè)簡(jiǎn)單彈窗效果
這篇文章主要介紹了使用Vue組件實(shí)現(xiàn)一個(gè)簡(jiǎn)單彈窗效果,本文主要內(nèi)容會(huì)涉及到彈窗遮罩的實(shí)現(xiàn), slot 插槽的使用方式,props 、 $emit 傳參,具體組件代碼也傳上去了。需要的朋友可以參考下2018-04-04nuxt框架中路由鑒權(quán)之Koa和Session的用法
后臺(tái)管理頁(yè)面需要有登錄系統(tǒng),所以考慮做一下路由鑒權(quán),這篇文章主要介紹了nuxt框架中路由鑒權(quán)之Koa和Session的用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05vue實(shí)現(xiàn)移動(dòng)端項(xiàng)目多行文本溢出省略
這篇文章主要介紹了vue實(shí)現(xiàn)移動(dòng)端項(xiàng)目多行文本溢出省略功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07vue?perfect-scrollbar(特定框架里使用非該框架定制庫(kù)/插件)
這篇文章主要為大家介紹了vue?perfect-scrollbar在特定框架里使用一款并非為該框架定制的庫(kù)/插件如何實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-05-05