ubuntu - 在 Linux 服务器上提供 Go API

标签 ubuntu go nginx server

在学习了几个教程之后,我仍然无法让我的 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/”。

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

我现在得到的错误是“错误:”网络错误“和一个 CORS 错误,即使 CORS 确实在本地工作并且我添加了 Gorilla Mux 为您提供的 CORS 方法。

希望有人可以帮助我。

目前服务器 block 文件:
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 指令并将位置上下文添加到服务器 block 内的配置文件中。

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

server {
    listen 80;
    server_name example.com;

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

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

关于ubuntu - 在 Linux 服务器上提供 Go API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59892169/

相关文章:

go - 插入自定义时间,实现了 Scanner 和 Valuer——但仍然出错

python - Django 没有反射(reflect) urls.py 的更改

ubuntu - 更改了 ubuntu.how 中 HOME 文件夹的访问权限,使其恢复为默认权限

windows - wxPython 在 Windows 和 Ubuntu 上给出不同的结果

pointers - 什么时候指针是惯用的?

nginx - 从 ingress-nginx 提供静态文件

ruby-on-rails - ActionCable - 无法在生产环境中升级到 WebSocket

python - 如何将linux应用程序编译成win *.exe文件

ubuntu - 使用 Alpine Linux 的 docker 容器中的 LaTeX(Latex 存储库)

pointers - Go 相当于 C 中的 void 指针