登录
首页 >  Golang >  Go问答

在Linux服务器上提供Go API

来源:stackoverflow

时间:2024-04-13 10:54:33 185浏览 收藏

各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题《在Linux服务器上提供Go API》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

问题内容

在学习了几个教程后,我仍然无法让我的 api 正常工作。它在本地运行良好,但当我将其添加到服务器上时,我似乎无法连接。有人可以帮我介绍一下吗?

我正在使用带有 nginx 的 ubuntu 18.04 服务器。 api 是用 golang 编写的。

我的文件夹结构是:

/var/www/example.com/ /var/www/example.com/goapi/

显然,api 位于 goapi 文件夹中(我知道,命名不好,但这只是为了学习目的)。

该 api 使用 gorilla mux 并在“:8000”上提供服务。我的前端是用 typescript 编写的并使用 axios。它尝试连接到“http://example.com/goapi/”。

我有我的标准服务器块(只有 1 个 atm),它刚刚定义了我的服务器名称和“尝试文件”,目前就是这样。我尝试添加代理通行证,但似乎没有任何作用。

我现在收到的错误是“错误:“网络错误”和 cors 错误,即使 cors 确实在本地工作并且我添加了 gorilla mux 为您提供的 cors 方法。

希望有人能帮助我。

当前服务器块文件:

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

  root /var/www/example.com;
  index index.html index.htm index.nginx-debian.html;

  server_name example.com www.example.com;

  location / {
        try_files $uri $uri/ =404;
  }
}

nginx 配置是开箱即用的,我没有更改任何内容。

我的配置文件如下所示:

user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf;

events {    worker_connections 768;     # multi_accept on; }

http {

    ##  # Basic Settings    ##

    sendfile on;    tcp_nopush on;  tcp_nodelay on;     keepalive_timeout 65;   types_hash_max_size 2048;   # server_tokens off;

    server_names_hash_bucket_size 64;   # server_name_in_redirect off;

    include /etc/nginx/mime.types;  default_type application/octet-stream;

    ##  # SSL Settings  ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE  ssl_prefer_server_ciphers on;

    ##  # Logging Settings  ##

    access_log /var/log/nginx/access.log;   error_log /var/log/nginx/error.log;

    ##  # Gzip Settings     ##

    gzip on;

    # gzip_vary on;     # gzip_proxied any;     # gzip_comp_level 6;    # gzip_buffers 16 8k;   # gzip_http_version 1.1;    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##  # Virtual Host Configs  ##

    include /etc/nginx/conf.d/*.conf;   include /etc/nginx/sites-enabled/*; }


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

解决方案


您当前没有在 nginx 配置文件中设置任何内容来连接到端口 8000 上的服务。最简单、最快的方法是使用 proxypass 指令并将位置上下文添加到服务器内的配置文件中堵塞。

作为一个例子,你的应该看起来像这样

server {
    listen 80;
    server_name example.com;

    location /GoAPI {
        proxy_pass http://127.0.0.1:8000;
    }
 }

请记住,每当您调整 nginx 配置文件时,都必须重新加载/重新启动服务才能使更改生效。您还可以使用 nginx -t 测试文件以确保语法正确。

本篇关于《在Linux服务器上提供Go API》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>