POSTS / 使用Nginx进行反向代理

Published: 2023-07-01
Updated:   2024-03-19

解析域名

参考:

  1. 阿里云用户指南-如何设置子域名?

Nginx安装

参考:

  1. ubuntu-tutorials.
  2. 如何在 Ubuntu 20.04 上安装 Nginx-阿里云开发者社区 (aliyun.com)

运行指令:

$ sudo apt update
$ sudo apt install nginx

Nginx配置

参考:

  1. 如何在 Ubuntu 20.04 上安装 Nginx-阿里云开发者社区 (aliyun.com)
  2. 如何配置Nginx反向代理 | myfreax
  3. 你应该知道的Nginx命令 | myfreax
  4. 单机上通过 Nginx 反向代理部署多个域名(子域名)的 Docker 应用 (atjiang.com)

Example: Miniflux

/etc/nginx/sites-available中创建miniflux.xyz文件, 写入如下配置:

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
server {
  listen 80;
  listen [::]:80;
  server_name miniflux.xyz;
  location / {
    proxy_redirect off;
    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_pass http://127.0.0.1:xxxx;
  }
}

然后将其链接到/etc/nginx/sites-enabled目录下面:

$ ln -s /etc/nginx/sites-available/miniflux.xyz /etc/nginx/sites-enabled/miniflux.xyz

重新加载Nginx, 便可以访问子域名了.

$ sudo systemctl reload nginx

[!TIP] 如果是第一次启动Nginx需要使用start指令而不是reload.

$ sudo systemctl start nginx