localhost 是本機回送位址的主機名,對應 IP 127.0.0.1(IPv4)或 ::1(IPv6)。瀏覽器訪問 https://localhost/ 實際連線至本機 Web 伺服器的 80 埠(HTTP 預設)。
+----------+ HTTP Request +----------+
| Client | --------------------> | Server |
| (Browser)| | (Apache/ |
| | <-------------------- | Nginx/ |
+----------+ HTTP Response | PHP-FPM)|
Request/Response Cycle +----------+
雙方約定的資料格式與流程規範。Web 核心協定為 HTTP(HyperText Transfer Protocol)。
# 請求報文
GET /index.php?name=value HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0...
Accept: text/html,application/xhtml+xml
Cookie: PHPSESSID=abc123
# 回應報文
HTTP/1.1 200 OK
Date: Mon, 04 Jul 2024 06:21:35 GMT
Server: Apache/2.4.58
Content-Type: text/html; charset=UTF-8
Content-Length: 1234
<!DOCTYPE html>
<html>...</html>
| 方法 | 語意 | 冪等 | 典型用途 |
|---|---|---|---|
| GET | 取得資源 | 是 | 瀏覽頁面、API 查詢 |
| POST | 提交資料建立資源 | 否 | 表單送出、API 建立 |
| PUT | 完整更新資源 | 是 | API 更新 |
| PATCH | 部分更新資源 | 否 | API 部分更新 |
| DELETE | 刪除資源 | 是 | API 刪除 |
| HEAD | 僅取得 Header | 是 | 檢查資源是否存在/快取驗證 |
| OPTIONS | 詢問支援方法 | 是 | CORS 預檢 |
| 分類 | 代碼 | 含義 |
|---|---|---|
| 2xx 成功 | 200 | OK |
| 201 | Created | |
| 204 | No Content | |
| 3xx 重導向 | 301 | Moved Permanently |
| 302 | Found (暫時重導向) | |
| 304 | Not Modified (快取有效) | |
| 4xx 客戶端錯誤 | 400 | Bad Request |
| 401 | Unauthorized | |
| 403 | Forbidden | |
| 404 | Not Found | |
| 422 | Unprocessable Entity (驗證失敗) | |
| 5xx 伺服器錯誤 | 500 | Internal Server Error |
| 502 | Bad Gateway | |
| 503 | Service Unavailable |
Apache + mod_php
請求 → Apache → 載入 PHP 模組 → 執行 PHP → 回應
Nginx/Apache + PHP-FPM
請求 → Nginx → FastCGI 協定 → PHP-FPM 工作者進程池 → 執行 PHP → 回應
請求 /style.css
↓
Nginx 直接讀取檔案 → 回應(極快,無 PHP 參與)
請求 /api/users
↓
Nginx 轉發 FastCGI → PHP-FPM
↓
PHP 解析腳本 → 連接 DB → 查詢 → 組裝 JSON → 回應
↓
Nginx 回傳給瀏覽器
autoindex 或在 Nginx/Apache 設定中啟用目錄瀏覽Options Indexes FollowSymLinksautoindex on; 在 location 區塊F12 開啟開發者工具 → Network 分頁 → 重新整理頁面,可檢視: