0

Can I serve my static website content and run a node server on the same ec2 Instance . My ec2 instance is a t2.micro ubuntu 14.04 Image. Tasks done : Nodejs,npm installed. Nginx installed configured. Set up the ../../www/ directory to serve content through nginx.

I have a domain xyz.com pointed its @ value to the elastic IP associated with the instance.

uploaded website files to directory . So now if I access xyz.com I get my website content through the nginx . Cool.

But the node app is not accessible. My node app is running on the port 3000.

tried adding

server {

root /var/www;
index: example.js

server_name _; 

location / {
    proxy_pass http://127.0.0.1:3000;
    expires 30d; # not required
    access_log off;
}

}

/etc/nginx/sites-available to point to the node server .

Cool . now xyz.com runs my node app . But no website content is accessible now : ( And if I try hitting any API whose routes are defined in my Express routes there are no logs on the server side . only an nginx 404 error eg:

xyz.com/myroute -404 nginx error, works on localhost:3000/myroute
xyz.com/ - returns the default route set for /

xyz.com:3000 - doesnot hit the node server. no logs.

Many online resources suggest rerouting the port 80 in the security zone to serve content over the port on which node is running. I could run the node app this way but again the whole website would have to be served through node.

I want to run them separately .

xyz.com - serves me the website

xyz.com:3000 - allows me to hit my node server Should I separate the app and website on to two different instances ? How can they be routed??

Saleem Ahmed
  • 2,719
  • 2
  • 18
  • 31
  • You can only have one webserver open on port 80, but you can have a single webserver serve 2 backends. See http://stackoverflow.com/questions/13240840/nginx-reverse-proxy-multiple-backends – Master_Yoda Oct 18 '15 at 13:44
  • So If I add another elastic IP to the instance and set up its server location proxied through nginx, to serve only the nodejs app , would that work ? – Saleem Ahmed Oct 18 '15 at 13:55
  • 1
    You don't need another IP, you just need to clarify to nginx what you are wanting. If the domain name of both sites is the same, your `location /` block is routing too much to node, "hiding" the content of the main site. The paths of the two sites need to be unambiguous and non-conflicting, e.g. everything in the node app might be under `location /api { ... }`. – Michael - sqlbot Oct 18 '15 at 15:05

1 Answers1

0

Hi I am running a similar setup but with a Jetty server instead of Node.

Below is my config;

server {
    listen 80;

    root /var/www/html/;
    index index.php index.html index.htm;

    server_name my_domain;

    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name                                ;
            include fastcgi_params;
    }

    location /app {
            proxy_pass    http://localhost:8080;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
    }
}

As I understand it, you should only have to change the location /app to whatever your app path is, and obviously point the proxy_pass at the right port.

The other way of going about it will be to proxy pass on subdomain and have 2 virtual servers in nginx, so for example app.mydomain.com and app2.mydomain.com. For that kind of setup have a look at; nginx reverse proxy multiple backends

I believe you are looking for the kind of setup I have above. Good luck.

Community
  • 1
  • 1
Nacer
  • 16
  • 1