利用 Nginx 反向代理使得可在 Windows 主機(jī)訪問 Linux 虛擬機(jī)的 http 服務(wù)
1. 前置信息
假設(shè)虛擬機(jī) IP 地址為 192.168.56.101。
在虛擬機(jī)上,下列命令可獲得對(duì)應(yīng)輸出。
curl http://localhost:8888/v1/chain/get_info
現(xiàn)在,想在 Windows 主機(jī)上通過瀏覽器訪問 http://localhost:808/v1/chain/get_info 獲得輸出。
注意,這里將 808 端口映射為 8888。
2. 設(shè)置反向代理
這可以通過在虛擬機(jī)上配置 nginx 反向代理來實(shí)現(xiàn)。如:把反向代理的端口設(shè)置為 808,就可以通過 http://192.168.56.101:808 來訪問。
nginx.conf
server {
listen 808;
location / {
proxy_pass http://localhost:8888
}
}
3. 打開防火墻的對(duì)應(yīng)端口 808
sudo firewall-cmd --zone=public --add-port=808/tcp --permanent
sudo firewall-cmd --reload
4. 在 Windows 上通過瀏覽器訪問
在 Windows 上通過瀏覽器訪問 http://localhost:808/v1/chain/get_info,即可獲得對(duì)應(yīng)輸出。
Reference
- How to Install Nginx on CentOS 7, https://www.tecmint.com/install-nginx-on-centos-7/
- 本地瀏覽器如何訪問虛擬機(jī)下nginx啟動(dòng)頁(yè)(2),https://jingyan.baidu.com/article/8ebacdf00bb81749f65cd5ac.html
- CentOS 7.X 關(guān)閉SELinux,https://www.cnblogs.com/activiti/p/7552677.html
Contributor
- Windstamp, https://github.com/windstamp
附錄 1. Nginx
1.1 Nginx 安裝
1.2 Nginx 的啟動(dòng)
1.2.1 啟動(dòng)
nginx
1.2.2 重啟
nginx -s reload
1.2.3 狀態(tài)
nginx -s status
附錄 2. 防火墻配置
防火墻的具體配置命令什么的和 Linux 系統(tǒng)分支及其版本有關(guān)。這里是 CentOS 7.
2.1 關(guān)閉防火墻和 SELinux
2.1.1 查看狀態(tài)
setstatus
2.1.2 臨時(shí)關(guān)閉
setenforce 0
2.1.3 永久關(guān)閉
sudo vim /etc/selinux/config
SELINUX=disabled
關(guān)閉之后,需要 reboot 重啟之后才能生效。
2.2 打開對(duì)應(yīng)端口
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
sudo firewall-cmd --zone=public --add-port=808/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --zone=public --permanent --add-service=https
sudo firewall-cmd --reload
注意,打開對(duì)應(yīng)端口之后,需要執(zhí)行 sudo firewall-cmd --reload 激活配置。