Nginx转发多个二级域名及前后端分离

简介: 一、直接上代码(以下配置方案为亲在广州某物联网公司做架构时的配置)/etc/nginx/conf.d/ssl.conf 配置文件:其中sink-node1与web-node1为内部服务器的hostname。

一、直接上代码(以下配置方案为亲在广州某物联网公司做架构时的配置)

/etc/nginx/conf.d/ssl.conf 配置文件:

其中sink-node1与web-node1为内部服务器的hostname。

#

# HTTPS server configuration

server {

    listen 443;

    server_name localhost;

    ssl on;

    root html;

    index index.html index.htm;

    ssl_certificate  cert/all-yourdomain.pem;

    ssl_certificate_key  cert/all-yourdomain.key;

    ssl_session_timeout 5m;

    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    ssl_prefer_server_ciphers on;

    # Load configuration files for the default server block.

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


    ### Required configuration. ###

    proxy_set_header HOST $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    #

    ### Nginx supports websocket must be configured. ###

    proxy_http_version 1.1;

    proxy_set_header Upgrade $http_upgrade;

    proxy_set_header Connection "upgrade";

    #

    ### gzip Compressed transmission. ###

    gzip on;

    gzip_min_length 1k;  # min is 1kib

    gzip_buffers 16 64K;

    gzip_http_version 1.1;

    gzip_comp_level 6;

    gzip_types text/plain application/x-javascript text/css application/xml application/javascript;

    gzip_vary on;

    #

    ### Enhanced optional configuration. ###

    client_max_body_size 10m;

    client_body_buffer_size 128k;

    proxy_connect_timeout 60s;

    proxy_send_timeout 120s;

    proxy_read_timeout 86400s;

    proxy_buffer_size 4k;

    proxy_buffers 4 32k;

    proxy_busy_buffers_size 64k;

    proxy_temp_file_write_size 64k;

    #add_header Pragma "no-cache";

    #add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";

    location / {

      if ($host = 'www.yourdomain.com' ) {

          proxy_pass http://web-node1:8080; break;

      }

      if ($host = 'yourdomain.com' ) {

          proxy_pass http://web-node1:8080; break;

      }


      if ($host = 'passport.yourdomain.com' ) {

          proxy_pass http://web-node1:8080; break;

      }

      if ($host = 'www.passport.yourdomain.com' ) {

          proxy_pass http://web-node1:8080; break;

      }


      if ($host = 'mp.yourdomain.com' ) {

          proxy_pass http://web-node1:8091; break;

      }

      if ($host = 'www.mp.yourdomain.com' ) {

          proxy_pass http://web-node1:8091; break;

      }


      if ($host = 'm.yourdomain.com' ) {

          proxy_pass http://web-node1:8091; break;

      }

      if ($host = 'www.m.yourdomain.com' ) {

          proxy_pass http://web-node1:8091; break;

      }


      if ($host = 'image.yourdomain.com' ) {

          proxy_pass http://web-node1:8083; break;

      }

      if ($host = 'www.image.yourdomain.com' ) {

          proxy_pass http://web-node1:8083; break;

      }


      if ($host = 'wx.yourdomain.com' ) {

          proxy_pass http://web-node1:9090; break;

      }

      if ($host = 'www.wx.yourdomain.com' ) {

          proxy_pass http://web-node1:9090; break;

      }


      if ($host = 'im.yourdomain.com' ) {

          proxy_pass http://sink-node1:10031; break;

      }

      if ($host = 'www.im.yourdomain.com' ) {

          proxy_pass http://sink-node1:10031; break;

      }


      if ($host = 'ms.yourdomain.com' ) {

          proxy_pass http://sink-node1:29088; break;

      }

      if ($host = 'www.ms.yourdomain.com' ) {

          proxy_pass http://sink-node1:29088; break;

      }


      if ($host = 'kibana.yourdomain.com' ) {

          proxy_pass http://sink-node1:5601; break;

      }

      if ($host = 'www.kibana.yourdomain.com' ) {

          proxy_pass http://sink-node1:5601; break;

      }


  # subproject为其中一个子系统(前后端分离)的二级域名转发配置

  # 以下逻辑为Nginx支持“与”、“或”、“非”等运算符的实现,See: https://www.jianshu.com/p/15a5cf8369f9

      set $eff_flag 'EFF';

      if ($host = 'subproject.yourdomain.com' ) {

          set $eff_flag '${eff_flag}F';

      }

      if ($host = 'www.subproject.yourdomain.com' ) {

          set $eff_flag '${eff_flag}F';

      }

      if ($request_uri ~* '/eff/*' ) {

          set $eff_flag '${eff_flag}BKE';

      }

      if ($eff_flag ~* 'EFFFBKE' ) {

          proxy_pass http://web-node1:29099; break;

      }

  # 关键此,这专门使用nginx来处理前端分离的静态资源,而不是转发到nodejs/webpack服务

      # subproject static resource forwarding.

      if ($eff_flag ~* 'EFFF' ) {

          proxy_pass http://web-node1:58888; break;

      }

      # Other pan parsing by cid.

      proxy_pass http://web-node1:8082;

    }

    error_page 404 /404.html;

        location = /40x.html {

    }

    error_page 500 502 503 504 /50x.html;

        location = /50x.html {

    }

}


/etc/nginx/conf.d/static.conf 配置文件:

其中58888端口专为 子系统subproject开放的静态资源端口,其实就是代替nodejs的web服务。

#

# HTTP front/static server configuration

server {

    # The port that is transferred to the front end and the end is not open to the external network.

    listen      58888;

    server_name  localhost;

    location / {

      # Fixed format (if additional front and back end separation items should be configured in this format),

      # both root and index instructions are necessary, because the static file of the project will be under

      # the directory of the root instruction, otherwise it will not be loaded..

      root /usr/share/nginx/html/efficiency-front;

      index index.html;

    }

    error_page 404 /404.html;

        location = /40x.html {

    }

    error_page 500 502 503 504 /50x.html;

        location = /50x.html {

    }

}

目录
相关文章
|
1月前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
40 0
|
5月前
|
域名解析 缓存 网络协议
Nginx系列教程(01) - DNS域名解析过程
Nginx系列教程(01) - DNS域名解析过程
328 0
|
3月前
|
前端开发 Java 应用服务中间件
nginx结合前后端分离项目springboot+vue的配置
nginx结合前后端分离项目springboot+vue的配置
|
4天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
2月前
|
应用服务中间件 nginx
nginx 根据域名转发
nginx 根据域名转发
42 0
|
6月前
|
域名解析 负载均衡 应用服务中间件
nginx反向代理https域名时,请求报错502问题排查
nginx反向代理https域名时,请求报错502问题排查
|
3月前
|
应用服务中间件 nginx
前后端分离项目中nginx部署的相关配置
前后端分离项目中nginx部署的相关配置
|
4月前
|
缓存 负载均衡 应用服务中间件
Linux-----nginx的简介,nginx搭载负载均衡以及nginx部署前后端分离项目
Linux-----nginx的简介,nginx搭载负载均衡以及nginx部署前后端分离项目
209 2
|
5月前
|
应用服务中间件 Linux nginx
linux nginx域名证书到期,拿着客户给的新证书文件在nginx中对证书进行替换
业务场景: 📕1.找到原证书位置,可在nginx 配置文件中查找到 🖥️2.找到位置后将原文件名字 加上.old (需要更换两个文件 一个.pem 一个.key) 🔖3.将新证书文件放在原目录,并重命名为原证书文件名称 📌4.cd到sbin目录 reload nginx 📇5.查询是否替换成功 🧣6、最后的话 业务场景: 域名证书到期,拿着客户给的新证书文件在nginx中对证书进行替换
161 0
|
6月前
|
应用服务中间件 nginx Windows
66分布式电商项目 - nginx基于域名配置虚拟主机
66分布式电商项目 - nginx基于域名配置虚拟主机
42 0