首页 > 编程知识 正文

nginx不同域名指向不同地址,nginx反向代理多台服务器

时间:2023-05-03 19:53:54 阅读:282814 作者:1989

nginx反向代理配置解决不同域名默认页面不同问题 背景设计思路步骤

背景

需求:使用同一个项目(http://localhost:8080/thzhdj)映射两个域名(test1.com,test2.com),当访问http://test1.com时 默认打开http://test1.com/thzhdj/portal/index.jsp 页面,访问http://test2.com时默认打开http://test2.com/thzhdj/portal/orgmap/index/index.jsp

设计思路

使用ngnix反向代理的原理达到上述需求

步骤

以下已window 系统为例:

下载nginx :http://nginx.org/en/download.html选择稳定版本下载解压启动/重启/停止: 打开解压后的路径进入cmd
启动:start nginx 或 nginx.exe (建议用前者)
使用http://localhost:80 访问出现如图所示即启动成功。
重启: nginx -s reload
停止: nginx -s stop(快速停止) 、nginx -s quit
注:直接关掉命令框是没停止nginx服务的配置 :配置文件路径confnginx.conf //配置监听访问test1.com 出现的页面为http://localhost:8080/thzhdj/portal/index.jsp;server { listen 80; server_name test1.com; location = / { proxy_pass http://localhost:8080/thzhdj/portal/index.jsp; } location / { proxy_pass http://localhost:8080/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } //配置监听访问test2.com 出现的页面为http://localhost:8080/thzhdj/portal/orgmap/index/index.jsp;server { listen 80; server_name test2.com; location = / { proxy_pass http://localhost:8080/thzhdj/portal/orgmap/index/index.jsp; } location / { proxy_pass http://localhost:8080/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 保存重启nginx : nginx -s reload;分别访问 :http://test1.com 和 http://test2.com OK搞定;注意 : server 中一定要配两个localhost
1、localhost = / {} 作用:只有当域名后面没有其他路径时走该分配
2、localhost /{} 作用:所有的访问都可匹配,但如果被上面那种已经处理过的就不会再走这个了

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。