淺談 Web Server and Application Server

什麼是伺服器?

vic
7 min readAug 20, 2018

當我們寫完程式後,都會說「把程式丟到伺服器run看看」,但什麼是伺服器呢?在 Web 的世界中,網路的架構大多數 client-server 的架構,而這裡的伺服器最主要的工作就是

接受 client 的 reuqest,然後根據請求回傳一個 response

而就因為這個主要的工作,伺服器可以被分為兩種伺服器,Web Server 和 Application Server。

Web 1.0 和 Web Server

在 Web 1.0 的時代,網路還只能單向傳送訊息,無法與使用者互動,而這時的網路架構中的伺服器,主要就是 Web Server,Web Server 「負責處理 HTTP 協定的傳輸」,然而 Web Server 只能傳送「靜態資料」,例如 HTML Page,圖片,或者是 redirect 的網址,也就是說 Web Server 其實就像一個 File System,接收 client 的 request 並根據 request 中的 file pa回傳一個「已經準備好的檔案」回去。

Web Server 概念圖

Web 2.0 和 Application Server

隨著網路普及,使用者不甘只單方面接收資料,使用者想要發送更個人的需求接受更客製化的資訊,於是網路走向了 Web 2.0,在 Web 2.0 中,使用者的請求可以透過「表單」傳送更個人化資訊,但是 Web Server 無法依照使用者的資訊動態產生客製化的頁面,此時需要有另一個伺服器幫他完成這件事,也就是Application Server。

Application Server 「負責 business logic 的執行」以及「資料庫的存取」,也就是實際存放以及執行程式碼的伺服器,所謂的 business logic 的執行就是去調用某個物件的method,或者一個函數,有了 Application Server後,使用者可以特過執行特定的業務邏輯後獲得他們想要的結果,但是,Client 端要如何跟 Application Server溝通呢?

Web Server 和 Application Server

由於網路中的傳輸協定是採用 HTTP,而負責此協定的伺服器是 Web Server,所以使用者理論上只能與 Web Server 做溝通,但是當使用者需要個人化結果時,又必須使用 Application Server 中的業務邏輯,究竟該如何讓使用者使用 Application Server 中的功能呢?答案是透過 Web Server。

然而就像 Client 跟 Server 溝通需要 HTTP 協定來規範傳輸方式,Web Server 和 Application Server的溝通也需要一個協定來規範標準,例如 Common Gateway Interface,就是一個用來規範 Web Server 跟 Application Server 溝通的技術,除此之外還有 Java 的 Servlet,Python 的WSGI和Ruby的Rack 都是類似的技術。

而實際上Web Server 和 Application Server 如何溝通呢?可以參考以一下的情境

Application server: Hey, WebServer…
Web Server: Yo, what’s up?
Application server: Hey, not much.
Web Server: What can I do for you?
Application server: Well, I know that you’re really great at serving up static files and all, but you’re going to get some crazy requests for JSPs and Servlets that you won’t be able to find on your file system.
Web Server: Really? What am I going to do? I won’t be able to find any of these JSPs and Servlets, and I’ll end up sending a bunch of 404 errors back to clients, and the clients will be pissed!
Application server: Hey, calm down. Here’s what you do: just take those requests and send them to me. I’ll handle the request, generate some HTML, give that HTML back to you, and you can send the HTML back to the client.
Web Server: Kewl. You do the work, but the client thinks it’s me handling the request? I like this arrangement already. How do I know what files to send to you though?
Application server: Don’t worry. I’ll make a thorough list and write it all down in a special XML file. Just read that file every once in a while and keep up to date on which files you need to send back to me.
Web Server: Great. But when I do get a request for an item on the list, how will I know where to send it.
Application server: Hey, don’t worry. I’ve got it all covered. That XML file also contains a list of which IP addresses/port combinations to send the requests to. It’s all right there in that XML file. And if you have a problem understanding how to use it, here’s a .dll file that explains everything to you as well. Read it every time you start up.
Web Server: Kewl. I think this is going to be a great relationship.

以上內容來自於 Understanding How the Application Server’s Web Container Works

Take-Home Messages

  • Web Server 負責處理 HTTP 協定的傳輸以及接受 HTTP request 與回傳 HTTP response,但只能傳送靜態資料(i.e 已經寫死的檔案)。
  • Application Server 負責管理並執行業務邏輯以及資料庫的存取,但無法透過HTTP協定直接與 client 溝通,他只能接受從 Web Server 傳過來的 request 並回傳 執行後的結果。
  • Web Server 可以做為 Application Server 的代理接受 Client 的請求,並將請求傳給 Application Server,請他執行程式後並回傳結果給 Web Server,Web Server 透過 HTTP 回傳給 Client。
  • Web Server 與 Application Server 的溝通也需要一個傳輸協定(i.e Interface),Common Gateway Interface是其中一個技術。

--

--