由于生產(chǎn)環(huán)境限制,只能對(duì)外開(kāi)放一個(gè)端口9988
,其他人寫(xiě)的項(xiàng)目也要用這個(gè)端口,所以只能用不同的請(qǐng)求路徑來(lái)區(qū)分不同的項(xiàng)目。
我的項(xiàng)目是前后分離的。頁(yè)面打包的內(nèi)容放到服務(wù)器目錄是:/usr/local/m2m/nbMsg/browser/dist
,java后臺(tái)的端口是:8989
,且后臺(tái)接口統(tǒng)一前綴/api/v1
。
我想配置成訪問(wèn)http://[host]:9988/nbmsg
,能夠默認(rèn)返回/usr/local/m2m/nbMsg/browser/dist
目錄下的html靜態(tài)頁(yè)面文件,然后頁(yè)面上的ajax請(qǐng)求都是/api/v1
開(kāi)頭的,需要代理到8989端口
。
下面是我的配置。一個(gè)server是原來(lái)已有的9988
端口的虛擬主機(jī),另一個(gè)8999
端口的虛擬主機(jī)是我加上去的。我應(yīng)該如何修改配置?讓http://[host]:9988/nbmsg
的請(qǐng)求都轉(zhuǎn)發(fā)到8999
端口的虛擬主機(jī)上?
server {
listen 9988;
root /server/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.php index.nginx-debian.html;
location / {
index index.html index.htm index.php;
# try_files $uri $uri/ =404;
}
location /business/ {
# root /server/html;
try_files $uri $uri/ /business/public/index.php?$query_string;
}
####################################################################
# request entrance
####################################################################
location /nbmsg {
proxy_pass http://127.0.0.1:8999;
proxy_set_header Host $http_host;
proxy_set_header X-Real_IP $remote_addr;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /server/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
}
}
####################################################################
# my virtural host
####################################################################
server{
listen 8999;
index index.html;
location / {
alias /usr/local/m2m/nbMsg/browser/dist;
}
location /api/v1 {
proxy_pass http://127.0.0.1:8989;
proxy_pass_header Server;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_set_header Host $http_host;
proxy_set_header X-Real_IP $remote_addr;
client_max_body_size 300m;
client_body_buffer_size 256k;
proxy_buffer_size 128k;
proxy_buffers 8 32k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
}
}
"由于生產(chǎn)環(huán)境限制,只能對(duì)外開(kāi)放一個(gè)端口9988,其他人寫(xiě)的項(xiàng)目也要用這個(gè)端口,所以只能用不同的請(qǐng)求路徑來(lái)區(qū)分不同的項(xiàng)目。" 也就是說(shuō)你們通過(guò)路徑(location)來(lái)區(qū)分的應(yīng)用,并不是域名(server_name)因此不存在后續(xù)的所謂“虛擬主機(jī)”的概念。而且nginx并不存在一個(gè)虛擬主機(jī)的概念(吐槽:部分博客、技術(shù)文章里面概念亂入),參見(jiàn):Server Block Examples
從你的上下文分析,你的需求:
順便說(shuō)下,你這段含糊不清
我想配置成訪問(wèn)http://[host]:9988/nbmsg,能夠默認(rèn)返回/usr/local/m2m/nbMsg/browser/dist目錄下的html靜態(tài)頁(yè)面文件,然后頁(yè)面上的ajax請(qǐng)求都是/api/v1開(kāi)頭的,需要代理到8989端口。
很容易讓人理解為你的ajax完成的url是http://[host]:9988/api/v1
,但從你的配置中看又不是, 同時(shí)如果你使用了html base標(biāo)簽等,但代碼中使用/api/v1類(lèi)似相對(duì)路徑的話又受base標(biāo)簽影響,也就是/api/v1這個(gè)路徑的實(shí)際url僅從配置分析得出的是http://[host]:9988/nbmsg/api/v1
基于以上分析,調(diào)整后的配置如下:
server {
listen 9988;
root /server/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.php index.nginx-debian.html;
location / {
index index.html index.htm index.php;
# try_files $uri $uri/ =404;
}
location /business/ {
# root /server/html;
try_files $uri $uri/ /business/public/index.php?$query_string;
}
####################################################################
# request entrance
####################################################################
location /nbmsg {
alias /usr/local/m2m/nbMsg/browser/dist;
}
location /nbmsg/api/v1 {
proxy_pass http://127.0.0.1:8989;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_set_header Host $http_host;
proxy_set_header X-Real_IP $remote_addr;
client_max_body_size 300m;
client_body_buffer_size 256k;
proxy_buffer_size 128k;
proxy_buffers 8 32k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /server/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
}
}
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。