nginx配置域名后的二級(jí)目錄訪問不同項(xiàng)目的配置操作
場(chǎng)景描述:
通過二級(jí)目錄(虛擬目錄,應(yīng)用程序)的方式訪問同一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;
#通過訪問service二級(jí)目錄來(lái)訪問后臺(tái)
location /service {
#DemoBackend1后面的斜杠是一個(gè)關(guān)鍵,沒有斜杠的話就會(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)訪問前臺(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;
#通過訪問service二級(jí)目錄來(lái)訪問后臺(tái)
location /service {
#DemoBackend1后面的斜杠是一個(gè)關(guān)鍵,沒有斜杠的話就會(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)訪問前臺(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;
}
問題解決
另外,在實(shí)際應(yīng)用中,我使用了asp.net 的mvc,將mvc設(shè)置為網(wǎng)站的方式?jīng)]有問題,如果是虛擬目錄的方式就會(huì)找不到路徑,是因?yàn)樽约涸诰W(wǎng)站中的地址很多寫的都不規(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同一端口訪問不同項(xiàng)目,以域名區(qū)分所訪問項(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í)目錄訪問不同項(xiàng)目的配置操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue iview組件表格 render函數(shù)的使用方法詳解
下面小編就為大家分享一篇vue iview組件表格 render函數(shù)的使用方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-03-03
Vue?數(shù)據(jù)綁定事件綁定樣式綁定語(yǔ)法示例
這篇文章主要為大家介紹了Vue?數(shù)據(jù)綁定事件綁定樣式綁定語(yǔ)法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Vue報(bào)錯(cuò)Syntax?Error:TypeError:?this.getOptions?is?not?a?
前幾天在vue運(yùn)行項(xiàng)目過程中報(bào)錯(cuò)了,所以下面這篇文章主要給大家介紹了關(guān)于Vue報(bào)錯(cuò)Syntax?Error:TypeError:?this.getOptions?is?not?a?function的解決方法,需要的朋友可以參考下2022-07-07
vue使用Vue.extend創(chuàng)建全局toast組件實(shí)例
這篇文章主要介紹了vue使用Vue.extend創(chuàng)建全局toast組件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Vue3項(xiàng)目引入阿里iconfont圖標(biāo)與字體及使用教程
Iconfont國(guó)內(nèi)功能很強(qiáng)大且圖標(biāo)內(nèi)容很豐富的矢量圖標(biāo)庫(kù),提供矢量圖標(biāo)下載、在線存儲(chǔ)、格式轉(zhuǎn)換等功能,下面這篇文章主要給大家介紹了關(guān)于Vue3項(xiàng)目引入阿里iconfont圖標(biāo)與字體及使用教程,需要的朋友可以參考下2023-05-05
關(guān)于Vue3父子組件emit參數(shù)傳遞問題(解決Vue2this.$emit無(wú)效問題)
相信很多人在利用事件驅(qū)動(dòng)向父組件扔?xùn)|西的時(shí)候,發(fā)現(xiàn)原來(lái)最常用的this.$emit咋報(bào)錯(cuò)了,竟然用不了了,下面通過本文給大家分享關(guān)于Vue3父子組件emit參數(shù)傳遞問題(解決Vue2this.$emit無(wú)效問題),需要的朋友可以參考下2022-07-07

