简体   繁体   English

开发环境的Node.js Express安装代理,对于Mac OS X,在URL中避免使用localhost

[英]Node.js Express setup proxy for development environment, avoid localhost in url for Mac OS X

I want to setup my dev environment so that all requests from www.dev.com in my browser are routed to localhost:8080 . 我想设置我的开发环境,以便浏览器中来自www.dev.com所有请求都路由到localhost:8080

I added 127.0.01 www.dev.com in my /etc/host, but it doesn't do the port forwarding. 我在/ etc / host中添加了127.0.01 www.dev.com ,但是它不执行端口转发。 If I go to http://www.dev.com:8080/ it works, but I want/need to use http://www.dev.com/ instead, which is closer to the actual production environment. 如果我访问http://www.dev.com:8080/可以,但是我希望/需要使用http://www.dev.com/ ,它更接近实际生产环境。

How do I do that? 我怎么做? I tried several solutions like nginx, but I don't like it, I'd prefer it to be "scriptable", so that any other developer can use it directly. 我尝试了nginx之类的几种解决方案,但我不喜欢它,我希望它是“可编写脚本的”,以便任何其他开发人员都可以直接使用它。 I'm using Express.js with Node. 我正在使用Express.js和Node。

I read through Assigning a domain name to localhost for development environment on Mac OS X with node.js but it actually doesn't explain the port-forwarding part. 我通读了使用node.js在Mac OS X上为开发环境分配域名给localhost,但实际上并没有解释端口转发部分。

You can use Mac port forwarding. 您可以使用Mac端口转发。 Binding to port 80 requires root privileges (anything below 1024(?) needs root) and it's probably best not to run a development application with root. 绑定到端口80需要root特权(低于1024(?)则需要root特权),最好不要使用root运行开发应用程序。

You can use pfctl port forwarding 您可以使用pfctl端口转发

eg To forward port 80 to 8080 例如,将端口80转发到8080

echo "rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080" | sudo pfctl -ef -

source: https://salferrarello.com/mac-pfctl-port-forwarding/ 来源: https : //salferrarello.com/mac-pfctl-port-forwarding/

You can also use ipfw (Not available on El Capitan) 您也可以使用ipfw (El Capitan上不可用)

sudo ipfw add 100 fwd 127.0.0.1,8080 tcp from any to any 80 in

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

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