cbd*_*per 4 dns localhost hosts webpack webpack-dev-server
我正在尝试重命名我localhost
在开发中使用的名称。
我已经更新了我的C:\Windows\System32\drivers\etc\hosts
文件并添加了这一行(以管理员身份运行):
# copyright (c) 1993-2009 microsoft corp.
#
# this is a sample hosts file used by microsoft tcp/ip for windows.
#
# this file contains the mappings of ip addresses to host names. each
# entry should be kept on an individual line. the ip address should
# be placed in the first column followed by the corresponding host name.
# the ip address and the host name should be separated by at least one
# space.
#
# additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# for example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within dns itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 my-dev-environment.com // <===== ADDED THIS LINE
127.0.0.1:8080 my-dev-environment.com // <===== I ALSO TRIED THIS
Run Code Online (Sandbox Code Playgroud)
但我不断收到此错误:
This site can’t be reached
"my-dev-environment.com" refused to connect
Run Code Online (Sandbox Code Playgroud)
我正在使用webpack-dev-server
开发,这是我当前的配置:
webpack.config.js
devServer: {
contentBase: './public',
compress: true,
hot: true,
historyApiFallback: {
index: '/app.html'
},
index: 'app.html'
},
Run Code Online (Sandbox Code Playgroud)
题
难道我做错了什么?为什么它不起作用?
更新(已解决):
遵循以下答案中的@Joel 建议,我已将其添加到我的 webpack.config.js
devServer: {
public: 'my-dev-environment.com',
port: 80,
}
Run Code Online (Sandbox Code Playgroud)
这是在我的hosts
文件中:
127.0.0.1 my-dev-environment.com
Run Code Online (Sandbox Code Playgroud)
现在我可以访问它 my-dev-environment.com
在主机中:127.0.0.1 my-dev-environment.com
通过运行刷新您的 DNS 缓存ipconfig /flushdns
。
启动webpack-dev-server 时:
添加标志--host 0.0.0.0
和--public my-dev-environment.com:PORT
:PORT
通过浏览器访问时可以省略吗?
由于http://
默认为:80
. 让您的应用程序在端口上运行:80
。除非你有一个 apache 实例或类似的东西,否则在:8080
.
如果您有https://
协议,请:443
改为在端口上运行您的应用程序。
在 Webpack 5 中,devserver
配置从更改public
为host
:
devServer: {
host: 'my-dev-environment.com',
port: 8080,
}
Run Code Online (Sandbox Code Playgroud)
Webpack 文档:https ://webpack.js.org/configuration/dev-server/#devserverhost