3

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

}
Skam
  • 7,298
  • 4
  • 22
  • 31
Sredny M Casanova
  • 4,735
  • 21
  • 70
  • 115
  • Just want to clarify, you have different domains e.g. `foo.com`, `bar.com`, not subdomains `app1.foo.com`, `app2.foo.com`, whose DNS resolves to your CENTOS server? – Skam Jul 04 '17 at 22:25
  • @SeeDart I will have boths domains and subdomains for some apps – Sredny M Casanova Jul 04 '17 at 22:29
  • Can you add your http block and verify the golang servers can be accessed from localhost? – Skam Jul 04 '17 at 22:40
  • @SeeDart I added the HTTP block. I checked and the process of the Golang app is running. But if U try to access the application directly with `IP:port` from the browser I also cannot see the website – Sredny M Casanova Jul 04 '17 at 22:46
  • If you can't access it from `ip:port` then it isn't an nginx issue and this question should be closed – Skam Jul 04 '17 at 23:19

1 Answers1

3

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.

Skam
  • 7,298
  • 4
  • 22
  • 31