我可以在同一台计算机上运行两个 Web 服务器吗?
我刚刚发现我可以使用 Python 编写一个真正的简单的 Web 服务器。我已经有一个 Apache Web 服务器,我想在这台机器上尝试基于 Python 的 Web 服务器。但我担心如果我尝试的话会引起某种冲突。我的意思是两个网络服务器如何“决定”谁需要处理来自客户端的请求?
I just found out that I can write a really simple web server using Python. I have already an Apache web server I would like to try the Python based web server on this machine. But I am afraid that I can get some kind of conflict if I try it. I mean how two web server will "decide" who needs to server a request from a client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
让他们监听不同的端口,你就会没事的。
默认 Web 端口为 80。当您在浏览器中打开某个 url 而不指定端口时,默认使用 80。
您可以将 Web 服务器配置为侦听不同的端口,但您还需要在 url 中明确指定它:
选择端口时,请注意您已安装和运行的任何软件尚未使用此特定端口号在你的盒子上。否则,正如你所猜对的那样,将会发生冲突。
PS 就在几天前重新安装时,我发现 IIS 无法启动(似乎没有原因)。原来新版本的Skype占用了这个默认端口!必须删除其设置“使用端口 80 和 443 作为传入连接的替代端口”。
Make them listen to different ports and you will be fine.
The default web port is 80. When you open some url in browser without specifying a port, 80 is used by default.
You can configure your web server to listen to a different port but then you will also need to specify it explicitly in the url:
When choosing a port pay attention that this particular port number is not yet in use by any software you have installed and running on your box. Otherwise, as you correctly guessed, there will be a conflict.
P.S. Just a few days ago doing reinstallation I got IIS not able to start (seemingly without reason). Turned out the new version of Skype occupied this default port! Had to remove its setting "Use port 80 and 443 as alternatives for incoming connections".
如果您实际上想运行单独的服务器来测试服务器软件,请参阅其他答案,但是...
听起来(因为您是开发人员,而不是系统管理员,对吗?)您真的只是想在同一台计算机上运行 Python 和 PHP 站点。因此,如果我正在阅读您的问题,请原谅我,但这种设置允许我在一台服务器 Apache 中使用相同端口(端口 80)在同一台计算机上运行两种类型的站点。
我在 /etc/hosts 文件(或 Windows 上的 C:\Windows\System32\drivers\etc\hosts)中创建新条目并将它们指向 127.0.0.1:
然后在 Apache 中为每个站点添加 VirtualHosts:
因此,PHP站点像往常一样在
DocumentRoot
中运行。 Python 站点在 WSGI 中运行。它们都运行在 Apache 中。然后为了测试,我只需在我用来处理本地副本的任何浏览器中添加“.local”。If you actually want to run separate servers to test out server software see the other answers, but...
It sounds like (because you are a developer, not a sysadmin right?) you really just want to run Python and PHP sites on the same computer. So, forgive me if I'm reading into your question, but this setup allows me to run both kinds of sites on the same computer with the same port (port 80) in one server, Apache.
I make new entries in my /etc/hosts file (or C:\Windows\System32\drivers\etc\hosts on Windows) and point them to 127.0.0.1:
Then in Apache I add VirtualHosts for each site:
So, the PHP sites run in the
DocumentRoot
like they always do. And the Python sites run in WSGI. And they both run in Apache. Then to test, I just add ".local" in whatever browser I'm using to work on my local copy.Web 服务器绑定到特定端口。通常,这是端口 80。如果未明确指定端口,则这是浏览器将尝试访问的端口。
您可以让备用服务器在不同的端口上运行(8080 或 8081 似乎是 Web 服务器的流行替代端口,但选择权在您手中)。
这将阻止冲突的发生。发送到端口 80 的所有内容都会到达您的旧服务器。所有访问 8080(或您决定在其上运行服务器的任何端口)的内容都将到达您的简单 python 服务器。
要访问其他服务器,请使用如下 URL:-
http://localhost:8080/
A web server is tied to a specific port. Normally, this is port 80. If the port is not explicitly specified, this is the port that a browser will attempt to hit.
You can get your alternate server to run on a different port ( 8080 or 8081 seem to be popular alts for web servers, but the choice is yours ).
This'll stop the conflict from happening. Everything going to port 80 hits your old server. Everything going to 8080 ( or whatever port you decide to run your server on ) will hit your simple python server.
To hit your other server, use a URL like :-
http://localhost:8080/
您不能在同一端口(默认为80)打开两个Web服务器,如果您想创建两个或多个Web服务器,则必须使用不同的端口。
如果您使用 DNS,则可以轻松设置 Web 服务器以使用不同的网站响应不同的请求,如果您需要为子域或不同的域设置不同的网站,这可能会很有用。
You cannot open two web servers in the same port (which default is 80), if you desire to make two or more web servers, you have to use different ports.
If you are using a DNS, you could easily set up your web server to respond with different web sites to different requests, that could be useful if you need to have different web sites for subdomains or different domains.
是的,您可以通过两种方法在同一台计算机上运行两个不同的服务器(当然还有另一种方法是在同一IP上运行不同的端口号)
与您共享此服务器 1:
服务器 2:
127.0.0.0
-127.255.255.255
)Yes, you can run two different servers on the same computer via two methods (of course there is another method of running on the same IP with a different port number)
Server 1:
Server 2:
127.0.0.0
-127.255.255.255
)网络服务器对于谁服务连接请求没有发言权(此任务仍然在操作系统级别)。此外,如果没有特殊的套接字选项,套接字必须绑定到接口、互联网地址和端口的唯一组合。
The webservers would have no say in who services a connection request (this task is still at the operating system level). Furthermore, without special socket options, the socket must be bound to a unique combination of interface, internet address and port.
我建议您专用一台服务器来服务 https(端口 443)请求。
这样您就可以避免其他人提到的端口冲突,同时也不要求用户在浏览器中输入任何奇怪的内容(任意端口号)。您甚至可以让每个服务器将流量重定向到另一个服务器(例如,http 服务器收到对其知道 https 服务器处理的主机名的 http 请求,因此它可以将请求重定向到具有相同主机名的 https,从而将请求传输到合适的服务器)。
服务器A:
服务器B:
I would suggest you dedicate one server to serving https (port 443) requests.
That way you can avoid the port conflict others have mentioned while also not requiring users to type anything strange into their browsers (arbitrary port numbers). You can even have each server redirect traffic to the other (e.g. the http server recieves an http request for a host name it knows the https server handles so it can redirect the request to https witht the same host name, thereby transferring the request to the appropriate server).
Server A:
Server B: