【问题标题】:How to redirect request to different container using nginx?如何使用 nginx 将请求重定向到不同的容器?
【发布时间】:2021-07-08 20:11:25
【问题描述】:

如果请求来自 abc.com/something 我想将请求重定向到 nuxt.js docker 容器

如果请求来自 abc.com/api(如果第一部分是 api),那么总是进入 Laravel Docker 容器

如果请求来自 www.abc.com 然后将路径更改为 abc.com 然后再次重定向到 nuxt.js docker..

【问题讨论】:

    标签: laravel docker nginx nuxt.js multi-tenant


    【解决方案1】:

    我的理解是你想实现一个反向代理。

    请点击链接:https://www.bogotobogo.com/DevOps/Docker/Docker-Compose-Nginx-Reverse-Proxy-Multiple-Containers.php

    根据您的用例示例 nginx conf 文件

        user nginx;
        worker_processes 1;
        error_log  /var/log/nginx/error.log;
        events {
          worker_connections  1024;
        }
        http {
          add_header x-content-type-options nosniff;
          include mime.types;
          default_type  application/octet-stream;
          sendfile on;
          keepalive_timeout  65;
          server {
              listen       30040;
              server_name  _;
              proxy_pass_header Server;
                  location /something {
                      proxy_pass http://dockerhost:portnuxt.js/;
                  }
                  location /api {
                      proxy_pass http://dockerhost:portLaravelDocker/;
                  }
          }
        }
      mime.types: |     
        types {
        text/html                                        html htm shtml;
        text/css                                         css;
        text/xml                                         xml;
        image/gif                                        gif;
        image/jpeg                                       jpeg jpg;
        application/javascript                           js;
        application/atom+xml                             atom;
        application/rss+xml                              rss;
    
        text/mathml                                      mml;
        text/plain                                       txt;
        text/vnd.sun.j2me.app-descriptor                 jad;
        text/vnd.wap.wml                                 wml;
        text/x-component                                 htc;
    
        image/png                                        png;
        image/svg+xml                                    svg svgz;
        image/tiff                                       tif tiff;
        image/vnd.wap.wbmp                               wbmp;
        image/webp                                       webp;
        image/x-icon                                     ico;
        image/x-jng                                      jng;
        image/x-ms-bmp                                   bmp;
    
        application/font-woff                            woff;
        application/java-archive                         jar war ear;
        application/json                                 json;
        application/mac-binhex40                         hqx;
        application/msword                               doc;
        application/pdf                                  pdf;
        application/postscript                           ps eps ai;
        application/rtf                                  rtf;
        application/vnd.apple.mpegurl                    m3u8;
        application/vnd.google-earth.kml+xml             kml;
        application/vnd.google-earth.kmz                 kmz;
        application/vnd.ms-excel                         xls;
        application/vnd.ms-fontobject                    eot;
        application/vnd.ms-powerpoint                    ppt;
        application/vnd.oasis.opendocument.graphics      odg;
        application/vnd.oasis.opendocument.presentation  odp;
        application/vnd.oasis.opendocument.spreadsheet   ods;
        application/vnd.oasis.opendocument.text          odt;
        application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                         pptx;
        application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                         xlsx;
        application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                         docx;
        application/vnd.wap.wmlc                         wmlc;
        application/x-7z-compressed                      7z;
        application/x-cocoa                              cco;
        application/x-java-archive-diff                  jardiff;
        application/x-java-jnlp-file                     jnlp;
        application/x-makeself                           run;
        application/x-perl                               pl pm;
        application/x-pilot                              prc pdb;
        application/x-rar-compressed                     rar;
        application/x-redhat-package-manager             rpm;
        application/x-sea                                sea;
        application/x-shockwave-flash                    swf;
        application/x-stuffit                            sit;
        application/x-tcl                                tcl tk;
        application/x-x509-ca-cert                       der pem crt;
        application/x-xpinstall                          xpi;
        application/xhtml+xml                            xhtml;
        application/xspf+xml                             xspf;
        application/zip                                  zip;
    
        application/octet-stream                         bin exe dll;
        application/octet-stream                         deb;
        application/octet-stream                         dmg;
        application/octet-stream                         iso img;
        application/octet-stream                         msi msp msm;
    
        audio/midi                                       mid midi kar;
        audio/mpeg                                       mp3;
        audio/ogg                                        ogg;
        audio/x-m4a                                      m4a;
        audio/x-realaudio                                ra;
    
        video/3gpp                                       3gpp 3gp;
        video/mp2t                                       ts;
        video/mp4                                        mp4;
        video/mpeg                                       mpeg mpg;
        video/quicktime                                  mov;
        video/webm                                       webm;
        video/x-flv                                      flv;
        video/x-m4v                                      m4v;
        video/x-mng                                      mng;
        video/x-ms-asf                                   asx asf;
        video/x-ms-wmv                                   wmv;
        video/x-msvideo                                  avi;
    

    【讨论】:

    • 非常感谢这个..但是我想要的是进入 laravel 容器,如果 /api 在 url 中.. 否则进入 nuxt 容器。位置 /something { proxy_pass dockerhost:portnuxt.js; } 位置 /api { proxy_pass dockerhost:portLaravelDocker;我不明白的第一个位置块。
    • 如果您在本地系统上运行 docker 主机将是 localhost,端口将是您的 nuxt.js 容器的端口
    猜你喜欢
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    相关资源
    最近更新 更多