Golang应用在Nginx中的基本配置

huangapple go评论123阅读模式
英文:

Basic Configuration of Golang App in Nginx

问题

我有一个运行着一些Golang应用程序的CentOS 7服务器。正如你所知,每个应用程序都在自己的端口上运行,比如: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;
        }
    }
}

以上是你提供的配置文件内容。

英文:

I have a CentOS 7 server where I have running some Golang apps. As you know, every app is running on his own port, lets say: 9000,9100,9200 and so on.

Now, I have installed Nginx to serve all the websites, I have a domain for every site and I want to receive all the petitions in the port 80 and then just based on the domain i have to redirect to the application that corresponds.

By now,am trying to make it with one of the site that is running in the port 9094, I have no experience with Nginx so I was just reading to know what to do,but it seems like it's not working. in the file nginx.conf I added these lines:

server {
    listen          80;
    server_name     mydomain.com;
    access_log      logs/mydomain.log main;

    location / {
        proxy_pass      http://127.0.0.1:9094;
    }
}

I have to mention that I didn't delete these lines that comes for default in the file:

  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 {
        }
    }

Is the configuration ok? and will allow me to add more sites? Thank you
If I ping to the domain everything is ok, but if I open the domain in the browser then I get status code 502

EDIT:

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;
                }
        }

}

答案1

得分: 3

您的服务器配置看起来没问题,502状态码表示您没有正确配置Go服务器。具体来说,Nginx按照您的期望将请求代理到您的上游服务器,并从上游服务器接收到了一个无效的响应。

英文:

Your server configuration looks okay and the 502 Status Code means you didn't configure the Go servers correctly. Specifically, Nginx did exactly what you expected it to, proxied the request to and from your upstream, but received an invalid response from your Go server.

huangapple
  • 本文由 发表于 2017年7月5日 06:01:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/44914491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定