简单记录下转发 Docker 映射出来的端口以及转发到别的服务器上的过程
安装 Nginx#
1.debain 系统安装#
sudo apt-get update
sudo apt-get install nginx
2. 验证 NGINX 是否成功安装。在终端中输入以下命令:#
sudo service nginx start
申请 SSL 证书#
1. 获取 CerBot 客户端#
install certbot
2. 获取证书#
certbot certonly --webroot -w /var/www/example -d example.com -d www.example.com
这个命令会为 example.com 和 www.example.com 这两个域名生成一个证书,使用 --webroot 模式会在 /var/www/example 中创建 .well-known 文件夹,这个文件夹里面包含了一些验证文件,certbot 会通过访问 example.com/.well-known/acme-challenge 来验证你的域名是否绑定的这个服务器。这个命令在大多数情况下都可以满足需求,
满足需求,
但是有些时候我们的一些服务并没有根目录,例如一些微服务,这时候使用 --webroot 就走不通了。certbot 还有另外一种模式 --standalone , 这种模式不需要指定网站根目录,他会自动启用服务器的 443 端口,来验证域名的归属。我们有其他服务(例如 nginx)占用了 443 端口,就必须先停止这些服务,在证书生成完毕后,再启用。
certbot certonly --standalone -d example.com -d www.example.com
证书生成完毕后,我们可以在 /etc/letsencrypt/live/ 目录下看到对应域名的文件夹,里面存放了指向证书的一些快捷方式。
这时候我们的第一生成证书已经完成了,接下来就是配置我们的 web 服务器,启用 HTTPS。
新建转发配置#
1. 配置 NGINX 反向代理。在终端中输入以下命令:#
blog.xxxx.xxx
我就用域名作为配置文件了
touch /etc/nginx/sites-available/blog.xxxx.xxx
2. 配置文件参考#
这个的意思是转发到 localhost 的 3000 端口
server {
listen 80;
listen [::]:80;
server_name wqq.xxxx.xxx;
return 301 https://$server_name$request_uri;
location / {
proxy_pass http://localhost: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;
port_in_redirect off;
proxy_redirect http://localhost:3000 https://wqq.xxxx.xxx;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
send_timeout 300;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name wqq.xxxx.xxx;
ssl_certificate /etc/letsencrypt/live/wqq.xxxx.xxx/fullchain.pem; # 替换成您的证书路径
ssl_certificate_key /etc/letsencrypt/live/wqq.xxxx.xxx/privkey.pem; # 替换成您的证书私钥路径
# 强制 HTTPS
if ($scheme != "https") {
return 301 https://$server_name$request_uri;
}
location / {
proxy_pass http://localhost: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;
port_in_redirect off;
proxy_redirect http://localhost:3000 https://wqq.xxxx.xxx;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
send_timeout 300;
}
}
3. 开启 sites-enabled 的软链接#
之后需要在 /etc/nginx/sites-enabled 用符号链接链接到上述 /etc/nginx/sites-available/blog.xxxx.xxx 文件,命令如下:
ln -s /etc/nginx/sites-available/blog.ryujinx.top /etc/nginx/sites-enabled/
然后检查 nginx 配置文件是否正确:sudo nginx -t
, 如果提示 "Syntax OK" 说明配置正确,再执行 sudo systemctl reload nginx
重启 nginx,就完成了配置。