簡體   English   中英

在 Linux 服務器上提供 Go API

[英]Serve Go API on Linux 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/ ”。

我有我的標准服務器塊(只有 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測試文件以確保語法正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM