应用设置设置https代理

应用设置设置https代理

使用nginx进行https反向代理

准备材料

  • 域名及对应ssl证书
  • 需要https反向代理的应用
  • nginx配置文件

域名及证书

域名可以从云服务厂商购买,便宜的有几元钱首年的,续费大概在50元左右

一般云服务厂商购买证书后都有可以申请证书,或者执行申请免费证书,并进行域名解析,可以参考各厂商的文档

总结步骤如下:

  1. 购买域名

  2. 申请ssl证书

    1. 得到 *.crt​,*.key​文件
    2. 存放于/path/to/your/certs/​随后挂载进入nginx容器
  3. 域名解析至目标服务器

服务器不一定需要云服务器,目前各运营商都陆续支持ipv6,可以在自己本地服务器开启ipv6,将域名动态解析至本地服务器(可以使用docker+ddsn-go部署)

启动应用

启动需要反向代理的应用,比如Calibre-Web​在线电子书城服务,端口为8083

https反向代理

nginx配置

我们将本机的/path/to/your/nginx/conf.d​目录映射为容器的/etc/nginx/conf.d​,该目录存放我们方向代理的规则

在目录下添加默认配置文件default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

添加我们的配置文件yourWebSide.conf

server {
 #SSL 默认访问端口号为 443
 listen 443 ssl;
 listen  [::]:443 ssl;
 #请填写绑定证书的域名
 server_name love.autoboy.run; 
 #请填写证书文件的相对路径或绝对路径
 ssl_certificate  /ssl/love.autoboy.run.crt; 
 #请填写私钥文件的相对路径或绝对路径
 ssl_certificate_key /ssl/love.autoboy.run.key; 
 ssl_session_timeout 5m;
 #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
 #请按照以下协议配置
 ssl_protocols TLSv1.2 TLSv1.3;
 ssl_prefer_server_ciphers on;
 location / {
			# 设置需要反向代理的服务url
            proxy_pass http://localhost:8083;
			# Host 头设置为原始主机+端口,识别反代服务器
			proxy_set_header Host $host:$server_port;
			# X-Real-IP 设置用户真实IP,便于服务器获取
            proxy_set_header X-Real-IP $remote_addr;
			# X-Real-PORT 设置用户源端口
            proxy_set_header X-Real-PORT $remote_port;
			# X-Forwarded-For 记录完整的用户IP链,通过反向代理服务器的记录
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
}
server {
 listen 80;
 listen [::]:80;
 #请填写绑定证书的域名
 server_name love.autoboy.run; 
 #把http的域名请求转成https
 return 301 https://$host$request_uri; 
}

部署nginx容器

可以直接在服务器安装nginx进行,本文使用docker部署nginx,docker compose文件如下所示:

version: "3"
services:
  calibre-proxy:
    image: nginx:latest
    container_name: calibre-proxy
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /path/to/your/certs/:/ssl # 证书目录
      - /path/to/your/nginx/conf.d:/etc/nginx/conf.d # nginx反向代理规则
    network_mode: host
    restart: unless-stopped

随后就可以正常显示了

参考资料

Nginx 服务器 SSL 证书安装部署

posted @ 2024-02-28 21:31  biiigwang  阅读(3)  评论(0编辑  收藏  举报