简体   繁体   English

在 Nginx 子路径上部署 Webpack

[英]Deploy Webpack on Nginx Subpath

Explanation of my Doubt我的疑惑的解释

I got an app running and holded on a Docker Container usign:我运行了一个应用程序并保存在 Docker 容器上:

  • Webpack网络包
  • React Router 4反应路由器 4
  • Express.js Express.js
  • GraphQL图形语言

My app works perfect under a Root Folder www.mycoolurl.com/ .我的应用程序在根文件夹www.mycoolurl.com/下完美运行。 But i got other app running on my root so i created www.mycoolrul.com/coolsubpath但是我在我的根上运行了其他应用程序,所以我创建了www.mycoolrul.com/coolsubpath

Express表达

app.use(express.static(__dirname + "/dist"));

app.get("/*", function(req, res) {
    res.sendFile(__dirname + "/dist/index.html");
});

Nginx Config Nginx 配置

upstream coolapp{
    server       1.1.1.1:8000;
}



location /coolsubpath {
    # Upstream
    proxy_pass         http://coolapp/;
    proxy_redirect     off;
    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;
    client_max_body_size 3M;
}

My first experience with this was awful ... every request tried to mount my App, Vendor and Manifest (webpack stuff, that only need to be mounted on root).我的第一次经历很糟糕……每个请求都试图挂载我的应用程序、供应商和清单(webpack 的东西,只需要挂载在 root 上)。 So i end up doing this:所以我最终这样做:

Webpack Config网络包配置

output: {
    filename: "[name].[chunkhash].js",
    path: resolve(__dirname, "../dist"),
    chunkFilename: "[chunkhash].js",
    publicPath: "/"
},

Now only the root will load the assets but... when i go to www.mycoolrul.com/coolsubpath only my index.html is found and ...现在只有根会加载资产但是...当我去www.mycoolrul.com/coolsubpath只找到我的 index.html 并且... 资产加载

The assets are looking for my ex.资产正在寻找我的前任。 www.mycoolurl.com/app.761c16f76fcba6601577.js/ instead of www.mycoolrul.com/coolsubpath/app.761c16f76fcba6601577.js/ . www.mycoolurl.com/app.761c16f76fcba6601577.js/而不是www.mycoolrul.com/coolsubpath/app.761c16f76fcba6601577.js/

What i Expect我期待什么

I tested adding React Router 4 subpath... and my Express.js the subpath name to the .get and .use methods.我测试了添加React Router 4子路径...和我的Express.js子路径名称到.get.use方法。 Yes, it works.是的,它有效。 But now i want my subpath to be instead of coolsubpath like ex.但是现在我希望我的子路径不是像 ex 那样的coolsubpath mynewsupercoolsubpath . mynewsupercoolsubpath I have to rebuild and deploy the app again ?.我必须再次重建和部署应用程序吗?。

What i tried...我试过的...

I tried rewriting the request url but had no luck, i know the code is wrong... but i dont know how to do it我尝试重写request url但没有运气,我知道代码是错误的...但我不知道该怎么做

if ($http_referer ~* ^www.mycoolrul.com/coolsubpath ) {
   set $request_url /coolsubpath/$1;
}

您必须在 webpack 配置中指定 publicPath 设置 - https://webpack.js.org/guides/public-path/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM