首页 > 系统相关 >nginx-端口复用,不同域名映射到不同服务中

nginx-端口复用,不同域名映射到不同服务中

时间:2024-05-26 11:23:59浏览次数:27  
标签:set http Forwarded header 端口 复用 nginx proxy

我使用docker容器运行NGINX,并将主机的80端口映射到容器的8080端口中。

[root@harbor 20240526]# docker ps
CONTAINER ID   IMAGE                                 COMMAND                   CREATED          STATUS                        PORTS                                                                            NAMES
2e110b2f2572   goharbor/nginx-photon:v2.10.0         "nginx -g 'daemon of…"   38 minutes ago   Up About a minute (healthy)   0.0.0.0:80->8080/tcp, :::80->8080/tcp, 0.0.0.0:443->8443/tcp, :::443->8443/tcp   nginx

然后我的服务器中有kafaka监控的web服务器EFAK和EMQX的Dashboard web。

我的NGINX配置如下:

worker_processes auto;
pid /tmp/nginx.pid;

events {
  worker_connections 3096;
  use epoll;
  multi_accept on;
}

http {
  client_body_temp_path /tmp/client_body_temp;
  proxy_temp_path /tmp/proxy_temp;
  fastcgi_temp_path /tmp/fastcgi_temp;
  uwsgi_temp_path /tmp/uwsgi_temp;
  scgi_temp_path /tmp/scgi_temp;
  tcp_nodelay on;
  include /etc/nginx/conf.d/*.upstream.conf;

  # this is necessary for us to be able to disable request buffering in all cases
  proxy_http_version 1.1;

  upstream core {
    server core:8080;
  }

  upstream portal {
    server portal:8080;
  }

  log_format timed_combined '$remote_addr - '
    '"$request" $status $body_bytes_sent '
    '"$http_referer" "$http_user_agent" '
    '$request_time $upstream_response_time $pipe';

  access_log /dev/stdout timed_combined;

  map $http_x_forwarded_proto $x_forwarded_proto {
    default $http_x_forwarded_proto;
    ""      $scheme;
  }

  include /etc/nginx/conf.d/*.server.conf;

  server {
    listen 8443 ssl;
#    server_name harbordomain.com;
    server_tokens off;
    # SSL
    ssl_certificate /etc/cert/server.crt;
    ssl_certificate_key /etc/cert/server.key;

    # Recommendations from https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers '!aNULL:kECDH+AESGCM:ECDH+AESGCM:RSA+AESGCM:kECDH+AES:ECDH+AES:RSA+AES:';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

    # disable any limits to avoid HTTP 413 for large image uploads
    client_max_body_size 0;

    # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
    chunked_transfer_encoding on;

    # Add extra headers
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
    add_header X-Frame-Options DENY;
    add_header Content-Security-Policy "frame-ancestors 'none'";

    # customized location config file can place to /etc/nginx dir with prefix harbor.https. and suffix .conf
    include /etc/nginx/conf.d/harbor.https.*.conf;

    location / {
      proxy_pass http://portal/;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $x_forwarded_proto;

      proxy_cookie_path / "/; HttpOnly; Secure";

      proxy_buffering off;
      proxy_request_buffering off;
    }

    location /c/ {
      proxy_pass http://core/c/;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $x_forwarded_proto;

      proxy_cookie_path / "/; Secure";

      proxy_buffering off;
      proxy_request_buffering off;
    }

    location /api/ {
      proxy_pass http://core/api/;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $x_forwarded_proto;

      proxy_cookie_path / "/; Secure";

      proxy_buffering off;
      proxy_request_buffering off;
    }

    location /v1/ {
      return 404;
    }

    location /v2/ {
      proxy_pass http://core/v2/;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $x_forwarded_proto;
      proxy_buffering off;
      proxy_request_buffering off;
      proxy_send_timeout 900;
      proxy_read_timeout 900;
    }

    location /service/ {
      proxy_pass http://core/service/;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $x_forwarded_proto;

      proxy_cookie_path / "/; Secure";

      proxy_buffering off;
      proxy_request_buffering off;
    }

    location /service/notifications {
      return 404;
    }
  }
  server {
    listen 8080;
    server_name kafka.efak.com;

    location / {
        proxy_pass http://192.168.24.25:8048;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
  }
  server {
    listen 8080;
    server_name emqx.dashboard.com;

    location / {
        proxy_pass http://192.168.24.25:18083;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
  }

}
View Code

前面的不看,我是直接复用的harbor的NGINX容器。看这里:

  server {
    listen 8080;
    server_name kafka.efak.com;

    location / {
        proxy_pass http://192.168.24.25:8048;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
  }
  server {
    listen 8080;
    server_name emqx.dashboard.com;

    location / {
        proxy_pass http://192.168.24.25:18083;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
  }

将配置复制到容器中, 然后重启容器

[root@harbor 20240526]# docker cp ./nginx.conf nginx:/etc/nginx/nginx.conf
Successfully copied 6.66kB to nginx:/etc/nginx/nginx.conf
[root@harbor 20240526]# docker container restart nginx
nginx
[root@harbor 20240526]#

然后再Windows本地配置hosts内容:

192.168.24.5 kafka.efak.com
192.168.24.5 emqx.dashboard.com

然后就可以在本地访问了。

标签:set,http,Forwarded,header,端口,复用,nginx,proxy
From: https://www.cnblogs.com/love-DanDan/p/18213435

相关文章

  • Linux 防火墙只允许指定IP 端口访问
    开启和关闭防火墙命令如下:查看防火状态systemctlstatusfirewalld2:暂时关闭防火墙systemctlstopfirewalld3:永久关闭防火墙systemctldisablefirewalldsystemctlstopfirewalld.service4:重启防火墙systemctlenablefirewalld5、查看防火墙已开通的端口:sudo......
  • Vue3实战笔记(43)—Vue3组合式API下封装可复用ECharts图表组件
    文章目录前言一、封装echart图标钩子二、使用步骤总结前言接上文,已经安装好了ECharts,开始封装组件方便使用。一、封装echart图标钩子首先应用我们之前学习的钩子方式,在hooks目录下创建一个名为useECharts.js的文件,用于封装ECharts的逻辑:import{ref,onMo......
  • 如何让ALG、FPGA、EMU、SOC、SubIp实现驱动复用
       摘要        在芯片验证场景中,我们通常涉及到算法team、fpga测试team、EMUteam、SOC验证和Subip验证如何对芯片的完备性测试的探讨。由于各个team都是相互独立的,很多flow都是独立开发出来,对于交互的文件也是五花八门,这些文件各team协助起来很不方便,如何打通......
  • Vue3实战笔记(40)—组件逻辑复用:自定义Hooks的完全指南
    文章目录前言一、状态管理二、副作用处理三、生命周期钩子总结前言自定义Hooks是Vue3中的一个重要特性,它允许您创建可重用的函数,以便在组件之间共享状态和逻辑。以下是一些关于自定义Hooks的常见用法。一、状态管理使用reactive或ref来创建响应式数据,并在组件中......
  • 服务器端口号怎么看?如何查看服务器端口号呢?有哪些需要注意的?
    简单来说,端口号就是计算机与外界通讯交流的出口,每个端口都有不同的编号,也就是“端口号”。它们是唯一的,用于标识不同的服务和应用程序。通过端口号,我们可以知道哪些服务正在运行,以及如何与它们进行通信。那么,如何查看服务器端口号呢?以下是一些常用的方法:一、通过命令行查看......
  • 容器配置nginx
    1.docker命令#启动容器dockerrun-it--namenginx-test-p8888:80--mounttype=bind,source=/data/volumes/nginx,target=/data--mounttype=bind,source=/data/volumes/nginx/nginx.conf,target=/etc/nginx/nginx.confnginx:latest#命令行进入容器dockerexec-......
  • 从零手写实现 nginx-01-为什么不能有 java 版本的 nginx?
    前言大家好,我是老马。很高兴遇到你。作为一个java开发者,工作中一直在使用nginx。却发现一直停留在使用层面,无法深入理解。有一天我在想,为什么不能有一个java版本的nginx呢?一者是理解nginx的设计灵魂,再者java开发者用java语言的服务器不是更加自然吗。于是动手开......
  • Linux-Nginx安装及启动命令
    目录1、安装依赖2、nginx安装3、防火墙打开80端口4、nginx启动停止1、安装依赖yum-yinstallgcc-c++ yum-yinstallpcrepcre-devel yum-yinstallzlibzlib-devel yum-yinstallopensslopenssl—devel2、nginx安装mkdir/usr/local/src/nginxcd......
  • Nginx简介详解
    0-nginx三大功能1.web服务器作为web服务器,Nginx是一个轻量级,而且能够处理的并发量更大2.反向代理服务器正向代理隐藏真实客户端,反向代理隐藏真实服务端3.邮件服务器也可用充当一个IMAP/POP3/SMTP服务器1-nginx特点(1)跨平台:Nginx可以在大多数UnixlikeOS编译运行,而且也......
  • 记录Nginx开机自动启动(Windows环境)
    参考:Nginx配置及开机自启动(Windows环境)_nginx开机自启动windows-CSDN博客winsw下载地址Indexofreleases/com/sun/winsw/winsw或者参考Nginx安装、配置以及开机启动(Win10篇)_win10怎么查看nginx启动成功-CSDN博客......