dpp42324
2019-01-20 04:43
浏览 219
已采纳

使用SSL在Nginx反向代理后面运行Go服务器

I've done some digging through the interwebs and haven't been able to come across anything similar (at least near any solution that has worked out for me).

Essentially, I am running a Golang server locally on 127.0.0.1:1337, I want this to be accessible globally so I use Nginx to forward traffic from https://api.example.com/ to my API to retrieve information.

With that being said, I have simply setup my Golang server to listen and serve on port 1337 and my Nginx configuration is setup to redirect all HTTP traffic (for all domains) to HTTPS:

server {
    listen 80 default_server;

    server_name _; 
    return 301 https://$host$request_uri;
}

and then I redirect traffic to port 1337 here:

server {
    server_name api.example.com;
    location / {
        proxy_pass http://127.0.0.1:1337;
    }

    listen 443 ssl;
    ssl_certificate_key /etc/nginx/ssl/private.key;
    ssl_certificate /etc/nginx/ssl/cert.crt;   
}

The issue with this is that I find myself to keep getting redirects from HTTPS to HTTP (as per wget) and I end up getting a Too Many Redirects error.

If anyone can provide some guidance, I'd very much appreciate it!

图片转代码服务由CSDN问答提供 功能建议

我已经完成了一些对跨网的挖掘工作,但没有遇到类似的问题(至少在附近 </ p>

基本上,我正在 127.0.0.1:1337 </ code>上本地运行Golang服务器,我希望可以访问此服务器 因此,我使用Nginx将流量从 https://api.example.com/ </ code>转发到我的API以检索信息。</ p>

话虽如此,我 只需将我的Golang服务器设置为侦听并在端口 1337 </ code> </ strong>上进行服务,并且我的Nginx配置已设置为将所有HTTP流量(用于所有域)重定向到HTTPS:</ p>

 服务器{
监听80 default_server; 
 
 server_name _;  
返回301 https:// $ host $ request_uri; 
} 
 </ code> </ pre> 
 
 

,然后在此处将流量重定向到端口1337:</ p> \ n

 服务器{
 server_name api.example.com; 
位置/ {
 proxy_pass http://127.0.0.1:1337;
} 
 
监听443 ssl; 
 ssl_certificate_key  /etc/nginx/ssl/private.key;
 ssl_certificate /etc/nginx/ssl/cert.crt;  
} 
 </ code> </ pre> 
 
 

与此有关的问题是,我发现自己不断从HTTPS重定向到HTTP(按照 wget </ em>) 我最终遇到重定向过多</ code>错误。</ p>

如果有人可以提供一些指导,我将非常感谢!</ p> < / DIV>

1条回答 默认 最新

相关推荐 更多相似问题