Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过nginx在Docker中转发热模块请求_Nginx_Webpack_Docker Compose_Nginx Reverse Proxy_Hot Module Replacement - Fatal编程技术网

通过nginx在Docker中转发热模块请求

通过nginx在Docker中转发热模块请求,nginx,webpack,docker-compose,nginx-reverse-proxy,hot-module-replacement,Nginx,Webpack,Docker Compose,Nginx Reverse Proxy,Hot Module Replacement,我正在制作一个支持热模块替换的网页包应用程序。由于我添加了一个nginx前端,我在连接热模块替换件时遇到了问题。Nginx提供页面服务,但是js包无法连接到另一个Docker容器中运行的webpack dev server 我认为问题可能源于两个方面:域解析问题(Docker容器和nginx之间)或者请求缺少正确的升级/主机头 此项目的源代码 我在这个项目中有两个docker容器: app-webpack-为网站服务的webpack-dev服务器 app nginx-反向代理 我的nginx

我正在制作一个支持热模块替换的网页包应用程序。由于我添加了一个nginx前端,我在连接热模块替换件时遇到了问题。Nginx提供页面服务,但是js包无法连接到另一个Docker容器中运行的
webpack dev server

我认为问题可能源于两个方面:域解析问题(Docker容器和nginx之间)或者请求缺少正确的升级/主机头

此项目的源代码

我在这个项目中有两个docker容器:

  • app-webpack
    -为网站服务的
    webpack-dev服务器
  • app nginx
    -反向代理
我的nginx配置文件位于
docker/nginx

理想情况下,用户会转到
localhost
,nginx会选择该主机并将其重定向到
app webapp:3000
。然后,webpack dev服务器通过
socketjs节点
socket地址发送热模块更换代码,页面在本地更新

我已经确认
应用程序网页包
容器可以提供支持HMR的页面


提前感谢您的帮助,如果有其他信息我可以提供,请告诉我

做了一点小修补,但结果证明我没有正确地升级标题。对于将来引用此内容的任何人,请查看链接的repo以获取完整的源代码

这是我的
etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile on;
    keepalive_timeout 65;

    # The server settings
    include /etc/nginx/conf.d/*.conf;
}
以及
etc/nginx/conf.d/default.conf/

# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  default $http_x_forwarded_proto;
  ''      $scheme;
}

# If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
# server port the client connected to
map $http_x_forwarded_port $proxy_x_forwarded_port {
  default $http_x_forwarded_port;
  ''      $server_port;
}

# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
  default upgrade;
  '' close;
}

# Apply fix for very long server names
server_names_hash_bucket_size 128;

# Set appropriate X-Forwarded-Ssl header
map $scheme $proxy_x_forwarded_ssl {
  default off;
  https off;
}

gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
                 '"$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent"';

# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
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 $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;

# Mitigate httpoxy attack (see README for details)
proxy_set_header Proxy "";

server {
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
    listen 80;
}

server {
    server_name localhost;
    listen 80;

    location /sockjs-node/ {
        proxy_pass https://app-webapp:3000/sockjs-node/;
    }

    location / {
        proxy_pass http://app-webapp:3000;
    }
}