>[danger] **棄用提醒:**
> *由于看云對于免費用戶的限制愈發嚴苛,本文檔已經遷移至語雀。本文檔將不做維護。*
> **語雀地址**:[https://www.yuque.com/a632079/nodebb](https://www.yuque.com/a632079/nodebb)
*****
# 配置 NginX 作為代理
[TOC]
NodeBB默認運行在端口`4567`上,這意味著通常需要 `URL:Port` 才能訪問 NodeBB(例如: `http://example.org:4567`)
為了允許NodeBB在沒有端口的情況下可以被訪問,可以設置 NginX 來讓所有對特定主機名(或子域)的請求代理到在任何端口上運行的 NodeBB 。
## 要求
* NginX 版本 1.10.0 或 更高
- ~~軟件包管理器可能不提供足夠新的版本。 要獲取最新版本,請自行編譯,或者在Ubuntu上使用NGINX Stable或NGINX Development PPA 后再進行安裝。如果您使用的是Debian,請使用DotDeb存儲庫獲取最新版本的Nginx。~~
現在,包管理器安裝的 NginX 基本已滿足需求。
- 您可以用 `nginx -V` 查看 NginX 版本
## 修改配置
>[success] `/path/to/nginx` 意味著需要你替換成 Nginx 的安裝路徑。(如:`/usr/local/nginx`,`C:\ProgramData\Chocolatey\lib\nginx\tools\nginx-1.12.1\`)
編輯文件 `/path/to/nginx/conf/nginx.conf`,在 http 塊加入內容。如:
>[info] 如果您查看過 NodeBB 官方文檔,你會發現它的 配置文件位置 已經過時。

下面是基本的 nginx 配置,NodeBB 運行在 4567 端口:
```
server {
listen 80;
server_name forum.example.org;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:4567;
proxy_redirect off;
# Socket.IO Support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
```
下面的 nginx 配置,NodeBB 運行在 `["4567","4568"]` 端口:
```
server {
listen 80;
server_name forum.example.org;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://io_nodes;
proxy_redirect off;
# Socket.IO Support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
upstream io_nodes {
ip_hash;
server 127.0.0.1:4567;
server 127.0.0.1:4568;
}
```
下面是使用SSL的例子:
```
### redirects http requests to https
server {
listen 80;
server_name forum.example.org;
return 302 https://$server_name$request_uri;
}
### the https server
server {
# listen on ssl, deliver with speedy if possible
listen 443 ssl spdy;
server_name forum.example.org;
# change these paths!
ssl_certificate /path/to/cert/bundle.crt;
ssl_certificate_key /path/to/cert/forum.example.org.key;
# enables all versions of TLS, but not SSLv2 or 3 which are weak and now deprecated.
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# disables all weak ciphers
ssl_ciphers 'AES128+EECDH:AES128+EDH';
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:4567;
proxy_redirect off;
# Socket.IO Support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
```
### 注意:
* ~~nginx 必須大于1.4.x版本才能支持websockets。Debian / Ubuntu 使用1.2,NodeBB 一樣能夠運行,因為有降級機制。~~ 好吧,我們現在要求的版本至少為 `1.10.0` ,并不會存在上面的問題。
* 如果你的 NodeBB 在和 nginx 是用一臺物理機運行,那么 `proxy_pass` 的 IP 應該是 `127.0.0.1`。如果存在于不同的環境,請修改為 NodeBB 所在服務器的 IP.
* 這個配置能讓您的 nginx 服務器監聽 `forum.example.org` 的請求。但它不能把互聯網路由到這里,所以,你還需要更新你的域名的 DNS 設置,把 `forum.example.org` 解析到 nginx 對應的機器!
* 在CentOS 7上,您可能會遇到“Bad Gateway”錯誤。您可以通過運行這條指令 `setsebool -P httpd_can_network_connect on` 來解決此問題。
## 配置 Nginx 以使用自定義的錯誤頁面
此示例將演示如何在您的論壇未運行時將 Nginx 配置為使用自定義的502錯誤頁面。
### 創建您的自定義錯誤頁面
創建 `502.html`并將其放在 `/path/to/nginx/html` 目錄中(這是Nginx默認文檔根目錄的位置)然后添加內容到您的 `502.html`文件。以下是可以復制和粘貼的示例:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert your page title here</title>
</head>
<body>
<p>Insert your content here.</p>
</body>
</html>
```
### 配置Nginx以使用您的自定義錯誤頁面
編輯您的 Nginx 配置 文件(上面我們所修改的`/path/to/nginx/conf/nginx.conf`),找到你剛才添加的`Server` 塊,然后在里面加入下面的內容:
```
server {
#在這里開始添加內容
error_page 502 /502.html;
location = /502.html {
root /path/to/nginx/html;
internal;
}
}
```
使用`error_page`指令,以便在發生502錯誤時提供您創建的自定義頁面。位置塊確保根與我們的文件系統位置匹配,并且文件只能通過內部Nginx重定向訪問。
重新啟動Nginx: `sudo service nginx restart`,使配置生效。這樣,下次用戶訪問您的論壇時,他們會看到您的自定義頁面。
>[info] 編寫: NodeBB Development & a632079
維護: a632079
審核: PA Team
最后更新: 2017.08.08
- 序
- 贊助
- 導言
- 安裝
- 通過操作系統
- Windows + Mongodb/Redis
- Ubuntu/Debian + Redis/Mongodb
- CentOS + Redis
- CentOS + Mongodb
- FreeBSD/OpenBSD + Redis
- Arch Linux + Redis
- OSX + Redis
- 通過云服務
- 通過主機面板安裝
- AppNode
- CPanel
- 寶塔
- 使用
- FAQ
- 高級
- 運行 NodeBB
- 配置 Config.json
- 配置 Nginx
- 配置 MongoDB
- 更新 NodeBB
- 設置 Widgets
- 安裝 Yarn
- 更新 MongoDB
- 數據庫備份與恢復
- 重置管理員密碼
- 讓 NodeBB 支持搜索
- 優化
- 優化配置,提升NodeBB處理能力
- Google字體庫 -> 360公共前端庫
- Google字體庫 -> 中科大鏡像
- 海外VPS提升NodeBB訪問速度
- 通過 NodeBB API 自動發帖
- 開發
- 準備
- 常用方法 & 變量
- 插件制作
- 使用工具包編寫一個插件
- 主題制作
- 使用工具包編寫一個主題
- 部件制作
- 國際化
- 鉤子(hook)使用說明