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

时间:2019-01-20 04:43:22

标签: ssl go nginx

我已经在互连网上进行了一些挖掘,但没有遇到类似的问题(至少在为我解决的任何解决方案附近)。

基本上,我正在class MyAny: Equatable, Decodable { static func == (lhs: MyAny, rhs: MyAny) -> Bool { return lhs.id == rhs.id } var id: Int } class Sample: Decodable { var something: Array<MyAny>? } 上本地运行Golang服务器,我希望可以全局访问它,因此我使用Nginx将流量从127.0.0.1:1337转发到我的API以检索信息。

话虽如此,我只是将Golang服务器设置为侦听并在端口 https://api.example.com/ 上进行服务,并且将Nginx配置设置为将所有HTTP流量(针对所有域)重定向到HTTPS:

1337

然后在此处将流量重定向到端口1337:

server {
    listen 80 default_server;

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

与此相关的问题是,我发现自己不断从HTTPS重定向到HTTP(按照 wget 进行重定向),最终遇到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; } 错误。

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

1 个答案:

答案 0 :(得分:0)

server_name _;匹配找不到匹配项的服务器名称。

我以前做过。

查看我的Nginx配置以代理api后端:

# ssl
ssl_certificate      /etc/nginx/cert/live/ybilly.com/fullchain.pem;
ssl_certificate_key  /etc/nginx/cert/live/ybilly.com/privkey.pem;

# http to https
server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name ybilly.com www.ybilly.com *.ybilly.com;
  return 301 https://$host$request_uri;
}

# api backend
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name *.ybilly.com;

  location / {
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
    proxy_set_header Host $host;
    proxy_set_header X-Real-Ip $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass_header Set-Cookie;
    proxy_read_timeout                 900;
    proxy_buffers 32 4k;
    proxy_pass http://127.0.0.1:8080/;
  }

}

相关问题