2019-07-21 10:59:45 +08:00
|
|
|
|
# Nginx 反向代理
|
2019-07-21 00:49:03 +08:00
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
[Nginx](http://nginx.org/) 是一個高性能的 `HTTP` 和反向代理服務器,代碼完全用 `C` 實現,基於它的高性能以及諸多優點,我們可以把它設置為 `hyperf` 的前置服務器,實現負載均衡或 HTTPS 前置服務器等。
|
2019-07-21 00:49:03 +08:00
|
|
|
|
|
2019-07-22 03:30:08 +08:00
|
|
|
|
## 配置 Http 代理
|
2019-07-21 00:49:03 +08:00
|
|
|
|
|
|
|
|
|
```nginx
|
2019-12-12 16:24:04 +08:00
|
|
|
|
# 至少需要一個 Hyperf 節點,多個配置多行
|
2019-07-22 03:30:08 +08:00
|
|
|
|
upstream hyperf {
|
|
|
|
|
# Hyperf HTTP Server 的 IP 及 端口
|
|
|
|
|
server 127.0.0.1:9501;
|
|
|
|
|
server 127.0.0.1:9502;
|
2019-07-21 00:49:03 +08:00
|
|
|
|
}
|
2019-07-22 03:30:08 +08:00
|
|
|
|
|
|
|
|
|
server {
|
2019-12-12 16:24:04 +08:00
|
|
|
|
# 監聽端口
|
2019-07-22 03:30:08 +08:00
|
|
|
|
listen 80;
|
2019-12-12 16:24:04 +08:00
|
|
|
|
# 綁定的域名,填寫您的域名
|
2019-07-22 03:30:08 +08:00
|
|
|
|
server_name proxy.hyperf.io;
|
|
|
|
|
|
2019-07-23 02:08:28 +08:00
|
|
|
|
location / {
|
2019-12-12 16:24:04 +08:00
|
|
|
|
# 將客户端的 Host 和 IP 信息一併轉發到對應節點
|
2019-07-21 00:49:03 +08:00
|
|
|
|
proxy_set_header Host $http_host;
|
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
|
|
2020-02-29 13:23:57 +08:00
|
|
|
|
# 轉發Cookie,設置 SameSite
|
|
|
|
|
proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
# 執行代理訪問真實服務器
|
2019-07-22 03:30:08 +08:00
|
|
|
|
proxy_pass http://hyperf;
|
2019-07-21 00:49:03 +08:00
|
|
|
|
}
|
2019-07-22 03:30:08 +08:00
|
|
|
|
}
|
2019-07-21 10:59:45 +08:00
|
|
|
|
```
|
2019-07-21 00:49:03 +08:00
|
|
|
|
|
2019-07-22 03:30:08 +08:00
|
|
|
|
## 配置 Websocket 代理
|
|
|
|
|
|
2019-07-21 00:49:03 +08:00
|
|
|
|
```nginx
|
2019-12-12 16:24:04 +08:00
|
|
|
|
# 至少需要一個 Hyperf 節點,多個配置多行
|
2019-07-22 03:30:08 +08:00
|
|
|
|
upstream hyperf_websocket {
|
2019-12-12 16:24:04 +08:00
|
|
|
|
# 設置負載均衡模式為 IP Hash 算法模式,這樣不同的客户端每次請求都會與同一節點進行交互
|
2019-07-21 10:59:45 +08:00
|
|
|
|
ip_hash;
|
2019-07-22 03:30:08 +08:00
|
|
|
|
# Hyperf WebSocket Server 的 IP 及 端口
|
|
|
|
|
server 127.0.0.1:9503;
|
|
|
|
|
server 127.0.0.1:9504;
|
2019-07-21 00:49:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 10:59:45 +08:00
|
|
|
|
server {
|
2019-07-22 03:30:08 +08:00
|
|
|
|
listen 80;
|
|
|
|
|
server_name websocket.hyperf.io;
|
2019-07-21 00:49:03 +08:00
|
|
|
|
|
2019-07-22 03:30:08 +08:00
|
|
|
|
location / {
|
|
|
|
|
# WebSocket Header
|
|
|
|
|
proxy_http_version 1.1;
|
|
|
|
|
proxy_set_header Upgrade websocket;
|
|
|
|
|
proxy_set_header Connection "Upgrade";
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
# 將客户端的 Host 和 IP 信息一併轉發到對應節點
|
2019-07-22 03:30:08 +08:00
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
|
proxy_set_header Host $http_host;
|
2019-07-21 00:49:03 +08:00
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
# 客户端與服務端無交互 60s 後自動斷開連接,請根據實際業務場景設置
|
2019-07-22 03:30:08 +08:00
|
|
|
|
proxy_read_timeout 60s ;
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
# 執行代理訪問真實服務器
|
2019-07-22 03:30:08 +08:00
|
|
|
|
proxy_pass http://hyperf_websocket;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|