简体   繁体   English

使用带有 webpack 的 npm run build 后如何使用 nginx 提供静态文件

[英]How to serve static files with nginx after using npm run build with webpack

after generating a development build with npm run build i get the message saying "Tip: built files are meant to be served over an HTTP server. Opening index.html over file:// won't work."使用 npm run build 生成开发构建后,我收到消息“提示:构建的文件旨在通过 HTTP 服务器提供服务。通过 file:// 打开 index.html 将不起作用。”

What is the best way to do this with nginx?使用 nginx 执行此操作的最佳方法是什么? Currently to test it i am using an npm module called serve.目前为了测试它,我使用了一个名为 serve 的 npm 模块。

Also, if i got to my homepage at mydomain.com and search for a user, everything works like it is supposed to, redirecting me to mydomain.com/users/brad but if i then do a url search formydomain.com/users/brad i get a not found error, any help is appreciated!此外,如果我在 mydomain.com 上访问我的主页并搜索用户,则一切正常,将我重定向到 mydomain.com/users/brad 但如果我随后进行 url 搜索 formmydomain.com/users/布拉德我得到一个未找到的错误,任何帮助表示赞赏!

In my case, when I have to serve static content with nginx, it often looks like :就我而言,当我必须使用 nginx 提供静态内容时,它通常看起来像:

   location /static {
       alias $myroot/staticfiles;
   }

Also, if you haven't already, read the NginX guide to Serving Static Content .另外,如果您还没有,请阅读 NginX 服务静态内容指南。

If your are familiar with the Docker technology i would recommend to use a Docker Nginx Container and add your static content from the Webpack Buildflow to your container (this can be automated with a build server).如果您熟悉 Docker 技术,我建议您使用 Docker Nginx 容器并将您的静态内容从 Webpack Buildflow 添加到您的容器(这可以通过构建服务器自动完成)。 Have a look at the following Docker Image from Nginx: https://hub.docker.com/_/nginx/ .查看 Nginx 的以下 Docker 镜像: https : //hub.docker.com/_/nginx/

Otherwise you have to install nginx on your Server where you host your Homepage.否则,您必须在托管主页的服务器上安装 nginx。 For this have a look at your Server OS and reach out to the web for a detailed nginx setup for your server.为此,请查看您的服务器操作系统,并访问网络以获取服务器的详细 nginx 设置。 Without any configurations nginx will serve the static content on Linux-like server from /usr/share/nginx/html没有任何配置,nginx 将从/usr/share/nginx/html类似 Linux 的服务器上的静态内容

If you only have FTP Access to your server you can transfer your built files via FTP/SFTP to a specific folder eg /myHomepage and then your static content is server from yourdomain.de/myHompage.如果您只有 FTP 访问您的服务器,您可以通过 FTP/SFTP 将您构建的文件传输到特定文件夹,例如 /myHomepage,然后您的静态内容是来自 yourdomain.de/myHompage 的服务器。

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

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