使用Nginx的Docker容器中的反向代理问题

时间:2019-01-07 06:03:37

标签: docker nginx reverse-proxy

我有2个容器,一个运行SITE A(端口8001),另一个运行BITE(端口8080)。我正在尝试做的是来自Nginx的反向代理。在nginx conf中,我将站点A的配置配置为

server {
       listen 8000;
       client_max_body_size 500M;
       server_name code;

       location / {
          proxy_pass   http://code:8001; #code is my container name in dockercompose
          proxy_set_header Host $http_host;
          proxy_set_header X-Forwarded-Host $host;
          proxy_set_header X-Forwarded-Server $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
      #Static file location
      location /static/ {
             alias   /library/static_root/;
         if ($request_filename ~* ^.*?/([^/]*?)$)
            {
             set $filename $1;
            }

         if ($filename ~* ^.*?\.(eot)|(ttf)|(woff)$)
         {
         add_header Access-Control-Allow-Origin *;
         }
             expires 30d;
      }


}

我在站点B的conf是

server
    {
     listen 8000;
     server_name kolibri;

     location /learn/
     {
         proxy_pass http://kolibri:8080
         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-Host $server_name;
     }
  }

但是,每当我尝试访问/ learn uri时,它会将我定向到站点A并发生错误

nginx_1      | 192.168.5.100 - - [07/Jan/2019:05:51:22 +0000] "GET /static      /learn_module/learn_module0.10.3.css HTTP/1.1" 404 143 "http://192.168.5.232/learn/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0"
nginx_1      | 2019/01/07 05:51:22 [error] 9#9: *10 open() "/etc/nginx/html/static/learn_module/learn_module-0.10.3.js" failed (2: No such file or directory), client: 192.168.5.100, server: kolibri, request: "GET /static/learn_module/learn_module-0.10.3.js HTTP/1.1", host: "192.168.5.232", referrer: "http://192.168.5.232/learn/"
nginx_1      | 192.168.5.100 - - [07/Jan/2019:05:51:22 +0000] "GET /static/learn_module/learn_module-0.10.3.js HTTP/1.1" 404 143 "http://192.168.5.232/learn/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0"

我在SITE A中配置了/ static / URI,因此它可能已重定向到SITE A,但如何调用此SITE B本身。希望这是有道理的。请帮助

0 个答案:

没有答案