仅在单个端口 8080 上转发到本地主机(Windows)可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/332651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:41:08  来源:igfitidea点击:

Forwarding only on a single port 8080 to localhost (windows) possible?

windowsportforwardinghosts

提问by Ben

I know how to set up a local webserver using xampp on windows... I enter my alias and target on the hosts file (c:\windows\system32\drivers\etc\hosts) and then add a respective entry on my apache vhosts config file. This way, assuming that my webserver is listening to port 80, I can for example map example.comto my local webserver.

我知道如何在 Windows 上使用 xampp 设置本地网络服务器...我在主机文件 (c:\windows\system32\drivers\etc\hosts) 上输入我的别名和目标,然后在我的 apache 虚拟主机上添加相应的条目配置文件。这样,假设我的网络服务器正在侦听端口 80,我可以例如映射example.com到我的本地网络服务器。

I've always entered the whole domain name (that is e.g. example.com) in my hosts file and any requests on that name would be directed to localhost.

我总是在我的主机文件中输入整个域名(例如 example.com),并且对该名称的任何请求都将被定向到 localhost。

Now I was wondering if there's a way to only forward example.com on a certain port(for example only example.com:8080) to the local webserver, and leave example.com (on the default port 80) alone, so that it would still go to my live production website.

现在我想知道是否有一种方法可以仅将某个端口上的 example.com(例如仅 example.com:8080)转发到本地网络服务器,而将 example.com(在默认端口 80 上)单独保留,以便它仍然会去我的现场制作网站。

As far as I understand this might not be possible using only the hosts file (I tried adding the port :8080 to my domain names - didn't seem work ;-) )...

据我所知,仅使用主机文件可能无法实现(我尝试将端口 :8080 添加到我的域名中 - 似乎不起作用;-))...

I really don't know much on this topic so any ideas, insights, links, reading material, tools are welcome.

我真的对这个主题了解不多,所以欢迎提供任何想法、见解、链接、阅读材料和工具。

Edit: Arnout's reply answers the question I've asked above but doesn't solve my actual problem. Rerouting example.com:8080 to localhost:80 does work and if I access example.com it loads up the frontpage of my local version, but all links on that page of course don't know about the port number and therefore point to the production version... The actual solution to my problem seems to be to bite into the sour apple and fixmy application (following Rob's suggestion) and remove all hardcoded urls, so that it works on any domain...

编辑:Arnout 的回复回答了我在上面提出的问题,但没有解决我的实际问题。将 example.com:8080 重新路由到 localhost:80 确实有效,如果我访问 example.com 它会加载我本地版本的首页,但是该页面上的所有链接当然都不知道端口号,因此指向生产版本......我的问题的实际解决方案似乎是咬住酸苹果并修复我的应用程序(按照Rob的建议)并删除所有硬编码的网址,以便它适用于任何域......

采纳答案by Arnout

Internet Junkbuster (a proxy server) can do this using its forwardingfunctionality.

Internet Junkbuster(代理服务器)可以使用其转发功能来做到这一点。

Just add a line like

只需添加一行

example.com:8080   localhost:80   .   .

to sforward.ini, and uncomment the forwardfileline in junkbstr.ini. Now configure your browser to use a proxyserver running at port 8000 of localhost, and you're set.

sforward.ini,并取消注释中的forwardfilejunkbstr.ini。现在将您的浏览器配置为使用在 localhost 的端口 8000 上运行的代理服务器,您就完成了。

I'm sure other proxy servers have a similar feature — I just like Junkbuster since it's a simple standalone executable.

我确信其他代理服务器也有类似的功能——我只是喜欢 Junkbuster,因为它是一个简单的独立可执行文件。

回答by Rob Williams

You are playing in the area of domain name services (DNS). Technically, with an advanced DNS configuration (which can include remapping ports), what you propose is possible and it is done routinely on the Internet. However, it is unlikely that you would want to go to that much trouble and expense locally.

您正在玩域名服务 (DNS) 领域。从技术上讲,通过高级 DNS 配置(可以包括重新映射端口),您提出的建议是可能的,并且在 Internet 上常规完成。但是,您不太可能想要在本地解决那么多麻烦和费用。

On the other hand, I suspect that your real issue can be addressed more easily. Why would you want to have "example.com" resolve to your local web server? You can already reference your local web server as "localhost", as "127.0.0.1", and via its assigned machine name "workstation-x".

另一方面,我怀疑您的真正问题可以更轻松地解决。为什么要将“example.com”解析为本地 Web 服务器?您已经可以将本地 Web 服务器引用为“localhost”、“127.0.0.1”,并通过其分配的机器名称“workstation-x”。

The only reason that I can think of, and that I have seen, for wanting to reference your local web server with the same host name as your production web server is due to hard-coding the server host name into your links within your application's web pages. If that is the case here, the answer is simple: don't! You should ALWAYS implement your web applications so that they reference everything (other pages, CSS, JS, images, etc.) relative to the deployed server. If you deploy across multiple servers, then your references must be absolute based on configurable server host names. This is easy to do, and there is no reason not to do it.

我能想到和看到的唯一原因是希望使用与生产 Web 服务器相同的主机名来引用本地 Web 服务器,因为将服务器主机名硬编码到应用程序 Web 中的链接中页。如果是这种情况,答案很简单:不要!您应该始终实现您的 Web 应用程序,以便它们引用与部署的服务器相关的所有内容(其他页面、CSS、JS、图像等)。如果您跨多个服务器部署,那么您的引用必须是基于可配置服务器主机名的绝对引用。这很容易做到,没有理由不去做。

If this is not the case, then please explain what you are trying to achieve and what motivates you to try.

如果情况并非如此,那么请解释您想要达到的目标以及促使您尝试的原因。

EDIT: Since you have confirmed my guess that the problem is hard-coded references to the production host name in the web pages, let me simply add that I have ALWAYS, WITHOUT EXCEPTION, found that it is cheaper to fix the real problem than to accumulate workarounds (such as your attempt to remap ports). I have never encountered an author who has argued the opposite.

编辑:既然你已经确认我的猜测是问题是对网页中生产主机名的硬编码引用,让我简单地补充一下,我总是,无一例外,发现解决真正的问题比解决问题更便宜积累解决方法(例如您尝试重新映射端口)。我从未遇到过持相反观点的作者。

Fixing the pages should be little harder than a global search & replace, especially with a decent text editor. Even writing a simple script to change the source files would be vastly cheaper and easier than your attempts at a workaround.

修复页面应该比全局搜索和替换更难,尤其是使用像样的文本编辑器。即使编写一个简单的脚本来更改源文件,也比您尝试解决方法要便宜得多且容易得多。

Regardless, best wishes to you, and let us know how it turned out.

无论如何,向您致以最良好的祝愿,并让我们知道结果如何。

回答by Rob Williams

Can you confirm if 8080 port is listening on webserver? use the command "netstat -na" to see if the port 8080 is listening.

您能否确认 8080 端口是否正在侦听网络服务器?使用命令“netstat -na”查看端口 8080 是否正在侦听。

Did you add 8080 port on apache config file as the same as the default 80 port?

您是否在 apache 配置文件中添加了与默认 80 端口相同的 8080 端口?

Once you add port 8080 on apache config file to make it listen and double check if 8080 port is listening on webserver, then you will be able to access the page using port 8080. like http://mytestwebserver:8080/index.htmlor something like this....

在 apache 配置文件中添加端口 8080 以使其侦听并仔细检查 8080 端口是否正在侦听网络服务器,然后您将能够使用端口 8080 访问该页面。如http://mytestwebserver:8080/index.html或像这样的东西....