问题描述
在 golang 中使用 unix 套接字创建服务器应用程序
服务器应用代码:
package main
import (
"fmt"
"net"
"net/http"
"os"
)
func main() {
sock := "/var/web.sock"
err := os.Remove(sock)
if err != nil {
fmt.Printf("Can not remove socket config file.")
}
unixListener,err := net.Listen("unix",sock)
if err != nil {
fmt.Printf("Can not create unix socket listener,")
}
http.HandleFunc("/3dviewer/upload",uploadFile)
http.Serve(unixListener,nil)
}
func uploadFile(w http.ResponseWriter,r *http.Request) {
fmt.Printf("WORKING")
}
需要客户端角度代码才能连接到服务器应用程序。请有人可以帮助我。
提前致谢
解决方法
可以使用Nginx等反向代理来处理Angular HTTP/HTTPS调用,并转发到Unix Socket
如果您使用 nginx,您可以在 /etc/nginx/site-available/your_configuration_file
处创建配置
然后启用它
sudo ln -s /etc/nginx/sites-available/your_configuration_file /etc/nginx/sites-enabled/
Nginx 配置文件示例
server {
listen 80;
server_name server_domain_or_IP;
location / {
include proxy_params;
proxy_pass http://unix:/home/myproject/myproject.sock;
}
}