问题描述
开发目的,我想使用自定义域而不是localhost
还有其他方法吗?
我可以找到 PHP 的解决方案,但找不到 angular应用程序的解决方案。
解决方法
更新您的angular.json
这是您应该做的:
"projects": {
"project-name": {
...
"architect": {
"serve": {
"options": {
"host": "customdomain.baz","port": 80
}
}
}
}
}
如果您使用的是Mac / linux,请更新您的主机文件
转到/etc/hosts
##
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 customdomain.baz // or whatever your domain is
255.255.255.255 broadcasthost
::1 localhost
,
在hosts
文件中添加一行:
127.0.0.1 my-custom-domain.com
但是这仍然需要指定端口。您必须使用my-custom-domain.com:4200
。
要使用默认端口:80
,请用作参考this post
如果要在同一端口上运行多个站点,但服务于不同的域,请使用Nginx或Apache作为代理。
这是Nginx的示例服务器配置:
server {
listen 80;
server_name my-custom-domain.com;
location / {
proxy_pass http://localhost:4200;
}
}