千家信息网

怎么让多端口网站用一个Nginx进行反向代理

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇内容介绍了"怎么让多端口网站用一个Nginx进行反向代理"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有
千家信息网最后更新 2025年12月02日怎么让多端口网站用一个Nginx进行反向代理

本篇内容介绍了"怎么让多端口网站用一个Nginx进行反向代理"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一:用nginx做反向代理

为了解决这两个问题,自然第一反应想到的就是使用反向代理,我的理想构思下应该是下图这样的。

既用户所有的请求都经过nginx,让nginx来判断当前url需要跳转到哪一个后端代理上,比较好的策略应该是让nginx来判断当前的host是什么来决定跳转到后端的哪一个webserver上,比如a.mip.com 就跳转到apollo,j.mip.com 就跳转到jenkins. 以此类推,这样就可以完美解决了,是吧?在nginx中你完全可以使用rewrite模块下if指令来进行判断。

二:使用if指令

这里要提一下,nginx比较原始化,如果需使用第三方module,你还需要重新编译nginx,用起来很麻烦,所以这里干脆使用OpenResty,它扩展了nginx,并且集成了很多成熟的lua模块,自行下载最新的1.15.8,安装方式和nginx一模一样。

默认是安装到/usr/local/目录下,当你看到有一个openresty目录表示你安装成功。

[root@localhost local]# ls bin  etc  games  include  lib  lib64  libexec  openresty  sbin  share  src [root@localhost local]# pwd /usr/local

接下来你可以使用 nginx -v 来看一下openresty版本号啥的。

[root@localhost sbin]# pwd /usr/local/openresty/nginx/sbin [root@localhost sbin]# [root@localhost sbin]# ./nginx -v nginx version: openresty/1.15.8.1

为了方便,我就直接使用nginx开启三个server:

192.168.23.129:80   nginx上开启的第一个网站,就是proxy了。

192.168.23.129:8001 nginx上开启的第二个网站,模拟apollo。

192.168.23.129:8002 nginx上开启的第三个网站,模拟jenkins。

1. apollo的模拟

server {         listen       8001;         server_name  somename  alias  another.alias;         location / {             root   html;             index  apollo.html;         }     }

8001端口网站的默认页是apollo.html,这个apollo.html所在路径就是在nginx下的html目录,如下所示。

[root@localhost html]# pwd /usr/local/openresty/nginx/html [root@localhost html]# ls 50x.html  apollo.html  index.html  jenkins.html

2. jenkins的模拟

server {        listen       8002;        server_name  somename  alias  another.alias;        location / {            root   html;            index  jenkins.html;        }    }

jenkins.html的文件所在路径如上所示哈。不再赘述。

3. proxy的模拟

server {         listen       80;         server_name  localhost;          location / {             if ($host = "a.mip.com") {                proxy_pass http://localhost:8001;            }             if ($host = "j.mip.com") {                proxy_pass http://localhost:8002;            }     }

可以看到,只需要使用rewrite模块下的if条件语句,通过$host系统变量判断当前的url中的host的值跳转到相应的网站。

4. host映射

好了,接下来只需要将 a.mip.com 和 j.mip.com 映射到nginx的ip地址192.168.23.129即可。因为这些域名方便记忆而不是真实存在的。

192.168.23.129 a.mip.com 192.168.23.129 j.mip.com

5. 启动nginx

[root@localhost sbin]# ./nginx [root@localhost sbin]# [root@localhost sbin]# [root@localhost sbin]# netstat -tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      3802/nginx: master tcp        0      0 0.0.0.0:8002            0.0.0.0:*               LISTEN      3802/nginx: master tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3802/nginx: master tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1172/sshd tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1724/master tcp6       0      0 :::22                   :::*                    LISTEN      1172/sshd tcp6       0      0 ::1:25                  :::*                    LISTEN      1724/master

通过上图可以看到,80,8001,8002 端口都已经开启了,接下来大家可以到浏览器去验证一下了。

以下是全部的nginx.conf。

#user  nobody; worker_processes  1;  #error_log  logs/error.log; #error_log  logs/error.log  notice; #error_log  logs/error.log  info;  #pid        logs/nginx.pid;   events {     worker_connections  1024; }   http {     include       mime.types;     default_type  application/octet-stream;      log_format  main  '$host ----> $remote_addr - $remote_user [$time_local] "$request" '                       '$status $body_bytes_sent "$http_referer" '                       '"$http_user_agent" "$http_x_forwarded_for"';      access_log  logs/access.log  main;      sendfile        on;     #tcp_nopush     on;      #keepalive_timeout  0;     keepalive_timeout  65;      #gzip  on;      server {         listen       80;         server_name  localhost;          #charset koi8-r;          #access_log  logs/host.access.log  main;      # location = /get {         #     set_unescape_uri $key $arg_key;  # this requires ngx_set_misc         #     redis2_query get $key;         #     redis2_pass 10.105.13.174:6379;         # }          location / {             if ($host = "a.mip.com") {                proxy_pass http://localhost:8001;            }             if ($host = "j.mip.com") {                proxy_pass http://localhost:8002;            }             root   html;            index  index.html index.htm;          }          #error_page  404              /404.html;          # redirect server error pages to the static page /50x.html         #         error_page   500 502 503 504  /50x.html;         location = /50x.html {             root   html;         }          # proxy the PHP scripts to Apache listening on 127.0.0.1:80         #         #location ~ \.php$ {         #    proxy_pass   http://127.0.0.1;         #}          # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000         #         #location ~ \.php$ {         #    root           html;         #    fastcgi_pass   127.0.0.1:9000;         #    fastcgi_index  index.php;         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;         #    include        fastcgi_params;         #}          # deny access to .htaccess files, if Apache's document root         # concurs with nginx's one         #         #location ~ /\.ht {         #    deny  all;         #}     }       # another virtual host using mix of IP-, name-, and port-based configuration     #     server {         listen       8001;         server_name  somename  alias  another.alias;         location / {             root   html;             index  apollo.html;         }     }      server {         listen       8002;         server_name  somename  alias  another.alias;         location / {             root   html;             index  jenkins.html;         }     }      # HTTPS server     #     #server {     #    listen       443 ssl;     #    server_name  localhost;      #    ssl_certificate      cert.pem;     #    ssl_certificate_key  cert.key;      #    ssl_session_cache    shared:SSL:1m;     #    ssl_session_timeout  5m;      #    ssl_ciphers  HIGH:!aNULL:!MD5;     #    ssl_prefer_server_ciphers  on;      #    location / {     #        root   html;     #        index  index.html index.htm;     #    }     #}  }

"怎么让多端口网站用一个Nginx进行反向代理"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

网站 代理 端口 接下来 就是 模块 目录 三个 内容 所在 指令 更多 知识 路径 原始 成熟 一模一样 以此类推 学有所成 上图 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 数据库防护系统 温州运营网络技术哪家便宜 戴尔服务器虚拟化哪家便宜 张掖网络安全平台作业 嵌入式软件开发薪资6 数据库的网状 方舟服务器优质和普通有什么区别 江苏网络技术咨询价格 数据仓库与传统数据库差别 境外赌博平台服务器技术分析 网络安全防护规范 杭州软件开发工资怎么样 网络安全发展有前途的城市 引文数据库的用处 战地I无法连接服务器 商洛市网络安全宣传周 怎么查看数据库列的字段类型 用友网络属于网络安全吗 曙光服务器的管理口ip 视频会议拼接服务器 数据库防护系统 计算机网络技术大连理工大学 数据库分析为什么最难 网络安全法律事件2018 网络技术和计算机应用的差别 数据库管理员自我介绍简短 it数据库技术岗位及能力 守护部队网络安全我该做什么 奇闻杂谈中国互联网科技 前端用得上数据库吗
0