反代服务的实现
安装环境Ubuntu20.04,使用apt安装ngnix作为反代服务器。(以后可以尝试CDN)
比如应用的场景:家里的宽带有公网IP没有80端口;或者想隐藏实际IP地址;或者多端口转发。
www.a.net -> 访问到192.168.1.100
www.b.net -> 访问到192.168.1.100:1234
sudo apt install nginx
用apt安装的Nginx 的默认配置文件位于目录 /etc/nginx/sites-enabled/
中。我们使用熟悉的编辑器(比如 nano 或者 vim)编辑默认生成的文件。
server {
listen 80 default_server;
root /var/www/html;
server_name dgideas.net;
location / {
}
}
首先,把需要配置/etc/nginx/sites-enabled/ 中的default文件编辑,按照如下方式:
www.a.net 需要请求到8080端口的应用
server {
listen 80;
server_name www.a.net;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
}
www.a.cn 需要请求到8081端口的应用
server {
listen 80;
server_name www.a.cn;
location / {
proxy_pass http://127.0.0.1:8081;
index index.html index.htm;
}
}
最后,在系统中执行下述指令以应用更改:
# 检查配置文件语法是否正确
nginx -t -c /etc/nginx/nginx.conf
#重新加载配置,方法一
nginx -s reload -c /etc/nginx/nginx.conf
#重新加载配置,方法二
systemctl reload nginx