Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1949|回复: 1
打印 上一主题 下一主题

使用nginx 同一端口根据不同域名转发到不同端口

[复制链接]

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
跳转到指定楼层
楼主
发表于 2017-5-3 11:57:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
需求:在一台服务器上部署两个tomcat项目,都想共用同一个端口号80。但是一台服务器tomcat端口号不能重复?那么这个问题怎么解决呢?
答案:使用nginx 同一端口根据不同域名转发到不同端口
实现方案:
①本地启动两个tomcat ,两个tomcat端口号分别为8080,8081一个nginx 端口号为80
②分别访问以下两个和 nginx
tomcat1 端口号8080 访问地址:127.0.0.1:8080



tomcat2 端口号8081 访问地址:127.0.0.1:8081





nginx 端口号80 访问地址 127.0.0.1

③在windows host文件中,建立两个域名映射地址
分别为:
127.0.0.1 8080.itmayiedu.com
127.0.0.1 8081.itmayiedu.com
8080.itmayiedu.com 对应tomcat端口号为8080
8081.itmayiedu.com 对应tomcat端口号为8081
④ 在nginx nginx.conf 配置使用nginx 同一端口根据不同域名转发到不同端口
#### 输入8080.itmayiedu.com 跳转到 http://127.0.0.1:8080
server {
listen 80;
server_name 8080.itmayiedu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
}
## 输入8081.itmayiedu.com 跳转到 http://127.0.0.1:8081
server {
listen 80;
server_name 8081.itmayiedu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8081;
index index.html index.htm;
}
}
⑤运行效果
输入8080.itmayiedu.com 通过nginx转发跳转到http://127.0.0.1:8080

输入8081.itmayiedu.com 通过nginx转发跳转到http://127.0.0.1:8081

就这样我们可以看到,在不同域名通过同一个端口转发到不同的端口。

回复

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
沙发
 楼主| 发表于 2018-3-1 14:05:05 | 只看该作者

  1. #user  nobody;
  2. worker_processes  1;

  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;

  6. #pid        logs/nginx.pid;


  7. events {
  8.     worker_connections  1024;
  9. }


  10. http {
  11.     include       mime.types;
  12.     default_type  application/octet-stream;

  13.     sendfile        on;
  14.     keepalive_timeout  65;

  15.     server {
  16.         listen       80;
  17.         server_name  localhost;

  18.         location / {
  19.             root   html;
  20.             index  index.html index.htm;
  21.         }
  22.         error_page   500 502 503 504  /50x.html;
  23.         location = /50x.html {
  24.             root   html;
  25.         }
  26.     }

  27.         server {
  28.                 listen 80;
  29.                 server_name www.chunhuitech.cn;
  30.                 location / {
  31.                         root html/chunhuitech;
  32.                         index index.html index.htm;
  33.                 }
  34.         }
  35.        
  36. #        server {
  37. #                listen 80;
  38. #                server_name www.chunhuitech.cn;
  39. #                #charset koi8-r;
  40. #                #access_log logs/host.access.log main;
  41. #                location / {
  42. #                        proxy_pass http://127.0.0.1:9528;
  43. #                }
  44. #        }
  45. }
复制代码
简单配置文件
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-7 04:29 , Processed in 0.056046 second(s), 18 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表