我有一台 CentOS 7 服务器,我在其中运行了一些 Golang 应用程序。如您所知,每个应用都在自己的端口上运行,比方说:9000、9100、9200 等等。
现在,我已经安装了 Nginx 来为所有网站提供服务,我为每个网站都有一个域,我想在端口 80 中接收所有请求,然后仅基于该域我必须重定向到对应的应用程序.
到目前为止,我正在尝试使用在端口 9094 中运行的站点之一来实现它,我没有使用 Nginx 的经验,所以我只是阅读以了解该怎么做,但它似乎不起作用。在文件 nginx.conf
我添加了这些行:
server {
listen 80;
server_name mydomain.com;
access_log logs/mydomain.log main;
location / {
proxy_pass http://127.0.0.1:9094;
}
}
我必须提到我没有删除文件中默认的这些行:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
配置好了吗?并允许我添加更多网站?谢谢 如果我 ping 到域,一切正常,但如果我在浏览器中打开域,则会收到状态代码 502
编辑:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name mydomain.com;
access_log logs/mydomain.log main;
location / {
proxy_pass http://127.0.0.1:9094;
}
}
}
最佳答案
您的服务器配置看起来不错,502 Status Code意味着你没有正确配置 Go 服务器。具体来说,Nginx 完全按照您的预期行事,代理与上游之间的请求,但从您的 Go 服务器收到无效响应。
关于nginx - Nginx中Golang App的基本配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44914491/