需求:
多個(gè)項(xiàng)目向一臺(tái)nginx服務(wù)請(qǐng)求資源,nginx服務(wù)需要確定當(dāng)前訪問的項(xiàng)目是哪一個(gè)項(xiàng)目,如果返回該項(xiàng)目對(duì)應(yīng)的資源文件.
解決:
通過域名來區(qū)分項(xiàng)目
- 1.在Windows hosts下面配置:
139.199.2.52 goknow.kna.com
139.199.2.52 goknow.knb.com
139.199.2.52 goknow.knc.com
- 2 配置nginx.conf
進(jìn)入 nginx conf目錄:
vi nginx.conf
在末尾最后一個(gè)'}'括號(hào)上一行加入:
server {
listen 81;
server_name goknow.kna.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root kna;
index index.html index.htm;
}
}
server {
listen 81;
server_name goknow.knb.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root knb;
index index.html index.htm;
}
}
server {
listen 81;
server_name goknow.knc.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root knc;
index index.html index.htm;
}
}
- 3 創(chuàng)建各個(gè)域名訪問的資源目錄:
回到nignx目錄下
cp -r html kna
cd kna
vi index.html
修改HTML的標(biāo)題內(nèi)容為:Welcome to nginx!-----kna
同理分別創(chuàng)建 knb和knc目錄
- 4 重啟nginx:
nignx -s reload
-
5 測(cè)試:
通過端口來區(qū)分項(xiàng)目:
同上修改nginx.conf 里面的sever{ }端口號(hào),即可
eg:修改這個(gè)為82
server {
listen 82;
server_name goknow.knb.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root knb;
index index.html index.htm;
}
}