Why do we need to specify a port number while using HTTP protocol?

醉酒当歌 提交于 2020-01-01 09:08:10

问题


Why do we need to specify a port number with an IP address even if we are using HTTP protocol? For example - http://xyz:8080... - what does this even mean? We already know that while using HTTP the request will be served at port 80, so why do we specify a port explicitly?

Please bear with me with this stupid question.


回答1:


The default port for HTTP is 80 and HTTPS is 443 but port numbers range from 0 to 65535. Most web servers listen on the default port so it's simpler to refer to the URL without the port. For example, http://www.cnn.com/ refers to the same resource as http://www.cnn.com:80/ since port 80 is the default port. If a HTTP/HTTPS service is other than the default port then the port must be specified in the URL.

The URL is defined by RFC 1738 with this simplified syntax which includes the port:

<scheme>://<host>:<port>/<url-path>

So for example, the URL http://xyz:8080/... refers to a host machine name or IP address listening to port 8080 with HTTP as the protocol. The HTTP (or web) server at that address then resolves the url-path to the particular service or file.

The port is optional when it is the default port for the given scheme or protocol (e.g., HTTP=80).

A given machine can host multiple different products with HTTP services from different ports. For example, Apache web server is listening to port 80 on a given server while Apache Tomcat is listening to port 8080 on the same machine. Various database and messaging products typically have HTTP services on different ports. The port is part of the address from which a client can reference a specific service.




回答2:


A port is like a "channel" in a way... If you have to access different functions of the same website, you use different ports. HTTP is port 80, HTTPS is 443, SSH is 22, and so on.



来源:https://stackoverflow.com/questions/50888270/why-do-we-need-to-specify-a-port-number-while-using-http-protocol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!