1

I have node.js server running locally on windows 7 and would like to add Nginx server to serve static files. After downloading and extracting Nginx, when I run nginx.exe, Nginx is accessible to node only inside the nginx folder.

How can I make it run automatically and accessible globally in node? Going over various tutorials, they describe the configuration for linux but I assume there are differences in the .conf file? If so, what are they?

Edit

Using express.js I keep getting 404 when trying to serve a css file... Here is the request URL: 127.0.0.1:3000/public/css/style.css And here is the config file:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 64;
    sendfile        on;
    keepalive_timeout  65;
server {

    listen       0.0.0.0:80;
    server_name localhost;
    access_log path_to_log;
    location / {

      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;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass    http://127.0.0.1:3000/;
      proxy_redirect off;
    }
  location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
    # access_log        off;
    root path_to_local_folder_with_static_files;
    expires           15d;
  }
 }
Omer Greenwald
  • 758
  • 7
  • 20
  • What do you mean by "Nginx is accessible to node only inside the nginx folder"? Why would your Node.js app ever have to access Nginx? And, what errors are you getting? Node.js doesn't execute Nginx, Nginx would just proxy over HTTP to Node.js. Or, do you have some very strange configuration? Please elaborate. – Brad Oct 27 '14 at 17:44
  • Sorry, this is my first time using nginx and/or node. I meant that the Nginx is not defined globally in the console, so I have to go to Nginx folder path each time in order to start it. For example "Nginx start" command will not work from the root folder of the node installation. Also, I'm not sure if there should be different settings in the nginx.conf file. Thanks – Omer Greenwald Oct 27 '14 at 18:10
  • You could add `nginx` to your path, but there is no reason to start Nginx from the directory your Node.js application is in. And, your configuration file is the same regardless of what system you run Nginx on. – Brad Oct 27 '14 at 18:51
  • What should be the relation between the location of the nodejs app to the nginx server? Should the nginx *contain* the node? – Omer Greenwald Oct 28 '14 at 17:08
  • No, there is no relation at all, what so ever. They are completely independent. You can put them on disk wherever you would like. – Brad Oct 28 '14 at 17:11
  • I've set configuration similar to http://stackoverflow.com/a/5015178/3800705 but I cannot get it to serve css whatsoever, getting 404... :( – Omer Greenwald Oct 28 '14 at 19:25
  • I can't help you with that without seeing your config. – Brad Oct 28 '14 at 19:25
  • Edited question to add config. thanks – Omer Greenwald Oct 28 '14 at 19:32
  • Is the 404 you're seeing coming from Express, or Nginx? That is, is Nginx erroneously proxying your CSS request to your Express app? – Brad Oct 28 '14 at 19:40
  • Is says X-Powered-By:Express in the response headers,not sure how else to check where it's from. I really appreciate your help btw. – Omer Greenwald Oct 28 '14 at 19:42
  • Then, the response is coming from Express. Your first location block is taking precedence over the last. – Brad Oct 28 '14 at 19:43
  • How should I fix it? I added the last after a few tests with the first only. Deleting the last and adding root the the first didn't help much – Omer Greenwald Oct 28 '14 at 19:45
  • I tried to pass "add_header X-my-header "this is a header";" in the location clause but I don't see it. Does it mean nginx is not running? – Omer Greenwald Oct 28 '14 at 20:32
  • If you're connecting to port 80, Nginx is definitely running. – Brad Oct 28 '14 at 20:32
  • maybe i should install nginx on windows as a service? – Omer Greenwald Oct 28 '14 at 20:45
  • That won't change anything at all. Simply fix your Nginx configuration file. – Brad Oct 28 '14 at 21:18
  • I've tried many changes in it. Could you suggest how to fix it? – Omer Greenwald Oct 28 '14 at 21:21
  • I'm not an Nginx config expert. I've had similar troubles before, but often resolve them by digging through the documentation and experimenting with the matches. There is also a rewrite log you can enable to help debug the problem. – Brad Oct 29 '14 at 02:32

0 Answers0