本文适合有一定nginx基础或者web开发基础的人员阅读
不同域名访问同一主机:
步骤1、在C:\Windows\System32\drivers\etc\hosts中配置两个域名
127.0.0.1 xx
127.0.0.1 yy
步骤2、用myeclipse新建两个工程,在index.jsp文件中写入下面代码:
<body>
xx <br>
<%
request.setCharacterEncoding("utf-8");
out.println("request.getParameterMap:<br />");
Map mp=request.getParameterMap();
for(Object obj:mp.keySet())
out.println(obj+":"+request.getParameter(obj.toString())+"<br />");
%>
</body>
部署到tomcat中,tomcat的端口是8080
步骤3、配置nginx
在%nginx_home%/conf/niginx.conf中加入:
http{
include server/xx.conf;
include server/yy.conf;
}
在conf下创建server目录
分别创建xx.conf和yy.conf,文件内容分别如下:
xx.conf:
server {
listen 80;
server_name xx;
location / {
proxy_pass http://xx:8080/xx/;
proxy_set_header X-Real-IP $remote_addr;
rewrite /file/(.*) /?fileID=$1;
rewrite /article/(.*) /?articleID=$1;
}
}
yy.conf
server {
listen 80;
server_name yy;
location / {
root html;
index yy.html;
}
}
步骤4、测试结果
重启nginx,
在浏览器中输入:http://xx/,显示如下:
xx
request.getParameterMap:
在浏览器中输入:http://yy/,(yy.html是我放在’%nginx_home%/html/‘目录下的一个文件)显示如下:
Welcome to nginx!
my server name is 'yy'
至此,不同域名访问同一主机不同工程演示完成。
niginx中URL重写和伪静态化配置
配置说明
在xx.conf中
proxy_pass http://xx:8080/xx/;表示,将请求交给tomcat的xx工程代理
rewrite /file/(.*) /?fileID=$1; 表示把http://xx/file/12345 这个链接交给http://xx:8080/xx?fileID=12345处理
测试结果
在浏览器中输入:http://xx/file/12345,显示如下:
xx
request.getParameterMap:
fileID:12345
至此nginx的url重写已经完成。