PulledBull
PulledBull

Reputation: 995

Using NGINX to serve HTTP traffic on port 80 for node.js app for domain hosted on Amazon EC2 with virtual host

I have 2 domains hosted on Amazon EC2 running bitnami wordpress, using apache virualhost

  1. wordpres.com > '/apps/wordpress'

  2. website.com > '/apps/website'

I have created a node.js graph app hosted inside '/apps/website/graph'

I wish to access this app on website.com/graph

const express = require('express');
const bodyParser = require('body-parser');
const app = express();


app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');

app.get('/graph', function (req, res) {
  res.render('index');
});

app.post('/graph', function (req, res) {
  console.log(req.body.speed + " " + req.body.freq);
  res.render('index');
})

var port = 3000;

app.listen(port, function () {
  console.log('Server Running on port ' + port)
});

The server runs well on website.com:3000/graph and also on wordpress.com:3000/graph

Question1: How do i make it work only on website.com:3000/graph?

The second part of the question is how to serve the HTTP traffic on port 80 with nginx to run on website.com/graph?

I have created this 'graph' nginx file inside '/sites-available' and linked in '/sites-enabled':

server {
  listen 80;
  server_name website.com;
  location /graph{
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host       $http_host;
    proxy_pass        http://127.0.0.1:3000/graph;
  }
}

Then I restarted nginx, but it is not working when I go to website.com/graph.

Question2: How do I make this HTTP traffic work and only on website.com/graph?

What am I doing wrong or missing here? I'm a front-end designer I have little experience with server side so excuse the ignorance :)

For reference, I was following this tutorial.

Thanks in Advance.

nginx.conf

[email protected]..:/etc$ cat nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;

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;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # 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/x-javascript text/xml appli                                                                                                                                            cation/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

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

Upvotes: 1

Views: 571

Answers (1)

Ryan Z
Ryan Z

Reputation: 2785

@PulledBull and I solved this together in chat. They had the following Apache config:

<VirtualHost *:80>
  ServerName domain.com
  ServerAlias www.domain.com
  DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"

  Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>

<VirtualHost *:80>
  ServerName other.domain
  ServerAlias www.other.domain
  DocumentRoot "/opt/bitnami/apps/is/htdocs"

  ErrorLog "logs/otherdomain-error_log"
  CustomLog "logs/otherdomain-access_log" common
</VirtualHost>

<VirtualHost *:443>
  ServerName domain.com
  ServerAlias www.domain.com
</VirtualHost>

We solved the issue by editing the existing config instead of using NGINX. We added the following line to each virtualhost where we wanted the Node.js app to be available:

ProxyPass /traingraph 127.0.0.1:3000

We only wanted /traingraph to be accessible on other.domain, so we ended up with the following config:

<VirtualHost *:80>
  ServerName domain.com
  ServerAlias www.domain.com
  DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"

  Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>

<VirtualHost *:80>
  ServerName other.domain
  ServerAlias www.other.domain
  DocumentRoot "/opt/bitnami/apps/is/htdocs"

  ErrorLog "logs/otherdomain-error_log"
  CustomLog "logs/otherdomain-access_log" common
  ProxyPass /traingraph 127.0.0.1:3000
</VirtualHost>

<VirtualHost *:443>
  ServerName domain.com
  ServerAlias www.domain.com
</VirtualHost>

Upvotes: 1

Related Questions