0
点赞
收藏
分享

微信扫一扫

nginx 转发 go 服务器的 https 请求

nginx 转发 go 服务器的 https 请求

需求

我有一个 ​​kylebing.cn​​​ 的站点,目前已经是 nginx 管理的 ​​https​​​ ,走的 ​​SSL​​​ 通道
现在我有一个名为 ​​​qr​​​ 的项目,用 go 语言写的,它监听的端口是 ​​:9999​​​,也就是 ​​https://kylebing.cn:9999​​​ 我需要配置 nginx 转发 端口 ​​9999​​ 的请求到 ​​https://kylebing.cn/qr-portal​​ 这个路径下

实现

首先你需要实现的是,保证 go 程序已经能够走 ​​https​​ 请求

具体可以参照这一篇文章:​​Golang/Gin框架添加对HTTPS的支持​​

能够实现 ​​https://kylebing.cn:9999​​​ 这个路径正常访问之后,只需要在 ​​nginx​​​ 配置中的 ​​ssl​​ 配置下面添加如下内容

location /qr-portal/ {
proxy_pass https://qr_server/;
proxy_set_header Host $host:$server_port;
}

完整版是这样的

upstream qr_server {
server localhost:9999;
keepalive 2000;
}

server {
listen 443 ssl;
server_name kylebing.cn;
root /usr/share/nginx/html;
index index.php index.html index.htm;
# ssl setup
ssl_certificate cert/kylebing.cn.pem;
ssl_certificate_key cert/kylebing.cn.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /qr-portal/ {
proxy_pass https://qr_server/;
proxy_set_header Host $host:$server_port;
}
}

最终结果:

两个地址,一个原地址,一个 nginx 转发的地址

nginx 转发 go 服务器的 https 请求_linux
nginx 转发 go 服务器的 https 请求_linux_02


举报

相关推荐

0 条评论