Ubuntu 在Linux服务器上提供GoAPI
在完成了几个教程之后,我仍然无法让我的API正常工作。它在本地运行良好,但当我将其添加到服务器上时,我似乎无法连接。有人能带我过去吗 我使用的是带有NGINX的ubuntu18.04服务器。API是用GoLang编写的 我的文件夹结构是: /var/www/example.com/ /var/www/example.com/GoAPI/ 显然,API位于GoAPI文件夹中(我知道,命名不好,但只是为了学习) API使用Gorilla Mux,并在“:8000”上提供服务。我的前端是用Typescript编写的,使用Axios。它试图连接到“” 我有我的标准服务器块(只有1个atm),它刚刚定义了我的服务器名和“try files”,现在就是这样。我尝试添加代理通行证,但似乎没有任何效果 我现在得到的错误是“error:“Network error”和CORS错误,尽管CORS确实在本地工作,我添加了Gorilla Mux提供给您的CORS方法 希望有人能帮我 当前的服务器块文件:Ubuntu 在Linux服务器上提供GoAPI,ubuntu,go,nginx,server,Ubuntu,Go,Nginx,Server,在完成了几个教程之后,我仍然无法让我的API正常工作。它在本地运行良好,但当我将其添加到服务器上时,我似乎无法连接。有人能带我过去吗 我使用的是带有NGINX的ubuntu18.04服务器。API是用GoLang编写的 我的文件夹结构是: /var/www/example.com/ /var/www/example.com/GoAPI/ 显然,API位于GoAPI文件夹中(我知道,命名不好,但只是为了学习) API使用Gorilla Mux,并在“:8000”上提供服务。我的前端是用Typesc
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
测试文件,以确保语法正确。您必须提供更多信息。这可能与您的Nginx服务器配置有关。您可以发布它吗?我知道我会在我的第一篇文章(编辑)中发布。谢谢你的回复:)这里没有任何东西可以连接到端口8000。你正在以文件的形式提供你的Go二进制文件,而不是在运行时提供它公开的服务。非常感谢Wind,我今晚会尝试这个,并让你知道结果。我以前尝试过这个(运气不好)但是因为我学习了一个在线教程,所以当时的位置不一样。我会尝试一下,再次感谢!