项目背景:在 Termux
中运行了多个 web
项目,但是只有一个域名。这种情况下可以使用 Nginx
反向代理将不同访问路径映射到不同的项目。比如访问 itfox.net/express/
会映射到 8080
端口的 express
项目,而访问 itfox.net
会映射 3000
端口的到默认项目。
安装 Nginx
1 2 3 4 5
| $ pkg install nginx
$ nginx
|
修改配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server { listen 8088; server_name *.itfox.net localhost;
location ~ /express/ { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location / { proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root /data/data/com.termux/files/usr/share/nginx/html; } }
}
|
参考资料
nginx 反向代理+路径重写 配置
nginx 配置文件 nginx.conf 之 server 及 server_name 的意义详解
重启 Nginx
1 2 3 4 5 6
| $ nginx -s reload
$ nginx -s stop $ nginx
|