需求:
多个项目向一台nginx服务请求资源,nginx服务需要确定当前访问的项目是哪一个项目,如果返回该项目对应的资源文件.
解决:
通过域名来区分项目
- 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
进入 nginx conf目录:
vi nginx.conf
在末尾最后一个'}'括号上一行加入:
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 创建各个域名访问的资源目录:
回到nignx目录下
cp -r html kna
cd kna
vi index.html
修改HTML的标题内容为:Welcome to nginx!-----kna
同理分别创建 knb和knc目录
- 4 重启nginx:
nignx -s reload
-
5 测试:
通过端口来区分项目:
同上修改nginx.conf 里面的sever{ }端口号,即可
eg:修改这个为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;
}
}