nginx配置域名后的二級目錄訪問不同項目的配置操作
場景描述:
通過二級目錄(虛擬目錄,應(yīng)用程序)的方式訪問同一ip+端口的不同應(yīng)用,例如location是用戶使用頁面,location/admin/是管理頁面,location部署在192.168.1.100的80端口,location/admin部署在172.20.1.32的8080端口上。
解決方案:
使用nginx反向代理,配置如下:
server { listen 80; server_name demo.domain.com; #通過訪問service二級目錄來訪問后臺 location /service { #DemoBackend1后面的斜杠是一個關(guān)鍵,沒有斜杠的話就會傳遞service到后端節(jié)點導(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ǎ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; } } #簡單的負(fù)載均衡節(jié)點配置 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; }
但是這種方式,二級目錄的樣式文件都不會正常顯示,他們不會自動在二級目錄下查找,而是在根目錄中查找,在跳轉(zhuǎn)頁面的時候也會報404錯誤。不知道是不是配置有誤,在server塊中配置了root或是rewrite都不能解決。
試著在proxy_pass后面加上二級目錄,并且和location塊的二級目錄相同,配置如下:
server { listen 80; server_name demo.domain.com; #通過訪問service二級目錄來訪問后臺 location /service { #DemoBackend1后面的斜杠是一個關(guān)鍵,沒有斜杠的話就會傳遞service到后端節(jié)點導(dǎo)致404 proxy_pass http://DemoBackend1/service;#DemoBackend1網(wǎng)站中要配置一個名稱為service的虛擬目錄,并且和location的二級目錄名稱一致 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ǎ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; } } #簡單的負(fù)載均衡節(jié)點配置 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; }
問題解決
另外,在實際應(yīng)用中,我使用了asp.net 的mvc,將mvc設(shè)置為網(wǎng)站的方式?jīng)]有問題,如果是虛擬目錄的方式就會找不到路徑,是因為自己在網(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ǔ)充知識:使用nginx服務(wù)器,實現(xiàn)同一IP同一端口訪問不同項目,以域名區(qū)分所訪問項目
這里我使用了兩臺nginx服務(wù)器,一臺服務(wù)器將不同項目綁定到不同端口,一臺服務(wù)器將不同域名分發(fā)到不同端口的項目上。
第一臺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; } }
第二臺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文件用以對兩個nginx服務(wù)器進(jìn)行操作
啟動文件如下: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啟動命令:start nginx.exe 或者 nginx
nginx重新加載配置命令:nginx -s reload
ngin重啟命令:nginx -s reopen
ngin關(guān)閉命令:nginx -s stop
以上這篇nginx配置域名后的二級目錄訪問不同項目的配置操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue iview組件表格 render函數(shù)的使用方法詳解
下面小編就為大家分享一篇vue iview組件表格 render函數(shù)的使用方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Vue報錯Syntax?Error:TypeError:?this.getOptions?is?not?a?
前幾天在vue運(yùn)行項目過程中報錯了,所以下面這篇文章主要給大家介紹了關(guān)于Vue報錯Syntax?Error:TypeError:?this.getOptions?is?not?a?function的解決方法,需要的朋友可以參考下2022-07-07vue使用Vue.extend創(chuàng)建全局toast組件實例
這篇文章主要介紹了vue使用Vue.extend創(chuàng)建全局toast組件實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Vue3項目引入阿里iconfont圖標(biāo)與字體及使用教程
Iconfont國內(nèi)功能很強(qiáng)大且圖標(biāo)內(nèi)容很豐富的矢量圖標(biāo)庫,提供矢量圖標(biāo)下載、在線存儲、格式轉(zhuǎn)換等功能,下面這篇文章主要給大家介紹了關(guān)于Vue3項目引入阿里iconfont圖標(biāo)與字體及使用教程,需要的朋友可以參考下2023-05-05關(guān)于Vue3父子組件emit參數(shù)傳遞問題(解決Vue2this.$emit無效問題)
相信很多人在利用事件驅(qū)動向父組件扔?xùn)|西的時候,發(fā)現(xiàn)原來最常用的this.$emit咋報錯了,竟然用不了了,下面通過本文給大家分享關(guān)于Vue3父子組件emit參數(shù)傳遞問題(解決Vue2this.$emit無效問題),需要的朋友可以參考下2022-07-07