Installing Nginx in front of node.js on local windows 7 - node.js

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

Related

Nginx reverse proxy not working on domain name

I have tried all the solution on SO but no success. I want to use Nginx as a "Node.js" app reverse proxy. With my current configurations, I was able to make it work when connecting to it through the server IP but not when using its domain name.My configuration details pastebin.com/gMqpmDwj
http://Ipaddress:3000 works but http://example.com doesn't.
Here is the configuration of my Nginx proxy, stored in /etc/Nginx/conf.d/domain.conf.
server {
listen 80;
server_name domain_name;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://ipaddress:3000;
}
}
But when I try to access it works fine on ip:port but when on domain:port or without port it doesn't
Try this configuration:
/etc/nginx/nginx.conf
user nobody;
worker_processes 1;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
client_max_body_size 8M;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log /var/log/nginx/error.log crit;
gzip on;
gzip_min_length 100;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/cloudflare.inc;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/domain.conf
upstream nodejs_app {
server <ipaddress>:3000;
keepalive 8;
}
server {
listen 80;
listen [::]:80;
server_name <domain_name>;
location / {
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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://nodejs_app/;
proxy_redirect off;
}
}
I solved my issue after following this link.I had multiple configuration files active that was causing problem.
How to Configure Nginx Reverse Proxy for Nodejs on Centos

POST Error when running multiple SocketIO endpoints behind nginx

I am trying to run multiple socketio endpoints behind a nginx proxy. My first socket site is at the endpoint '/'. This one works fine. The second socketio endpoint is at '/red/'. This appears to work. The socketio client in my html
var socket = io.connect('http://www.vagrantdevhost.com:8080', {path:'/red/'});
socket.on('connect', function() {
socket.emit('connectevent', {data: 'I\'m connected!'});
});
appears to connect properly. My connect event is called. However, I get the following errors in my console.
POST http://www.vagrantdevhost.com:8080/red/?EIO=3&transport=polling&t=MKUdom- 405 (METHOD NOT ALLOWED) index.js:83
Any explanations? I'm on day three of this and would appreciate any advice. I have included my nginx config.
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
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 8080 default_server;
listen [::]:8080 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /socket.io {
proxy_pass http://127.0.0.1:8092/socket.io;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location / {
proxy_pass http://127.0.0.1:8092;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /red/ {
proxy_pass http://127.0.0.1:8093/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
Usually these error messaged are caused by the limitation that Nginx can't serve static content on POST request. So, there is a hack in this post where you redirect 405 to 200.
check here: POST request not allowed - 405 Not Allowed - nginx, even with headers included
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 403 /403.html;
# To allow POST on static pages
error_page 405 =200 $uri;
# ...
}

nginx proxy_pass only works locally

I set up a nodejs application on port 8082 and a nginx-webserver with those configurations:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name mydomain.com;
access_log \logs\access.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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:8082;
}
}
}
in the nginx.conf file. The machine is a windows server 2008.
I control it with remote deskop, so i can check whether i can
ggo to the url mydomain.com and it works on that machine so far - the nodejs
app works fine.
On any other machine i cannot get to the mydomain.com url, i.e. i get "ERR_CONNECTION_TIMED_OUT", but i can go to the ipadress on port 8082 and it works fine as well. I have the mydomain.com domain pointed to that ipadress and there is no hosts entry in system32/drivers/etc on the server as well
edit:
problem was the firewall, it was set up for the IIS, not for nginx - so all incoming requests got blocked

How to set and configure node.js with express to nginx reverse proxy on centos 6.4 virtualbox client?

I am creating a development VM on my Windows 8.1 system using VirtualBox 4.3.4 and installed CentOS 6.4 as my VMServer. I installed NginX and Node.js with no problems. With little knowledge of administration I deleted the contents of /etc/sysconfig/iptables allowing all connections for development purpose only as of the moment (which will be changed later).
NginX is working fine, on my Windows host, and pointing the browser to http://192.168.1.20 shows that the "Welcome to nginx on EPEL!" page is working.
Verifying that my node.js app is working correctly, I created a node app on my VMServer by running
[~]# express node_nginx_practice
[~]# cd node_nginx_practice && npm install
[~]# node app
Then, on my Windows host, point the URL to http://192.168.1.20:3000, the "Express default" page is running with no problem.
The question is how could I set and configure node app to serve on nginx reverse-proxy?
example: http://192.168.1.20 //will point to Express default page
I tried setting my nginx config in /etc/nginx/nginx.conf by following some tutorials on the web with no luck url still points to the NginX "Welcome to nginx on EPEL!" page.
Here's my nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-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;
}
}
}
I know I am missing something. Could someone point me to right direction?
EDIT ANSWER:
Here's what i did to solve the issue in step by step! (Thanks to "C Blanchard")
edit the default NginX config file:
[~]# vim /etc/nginx/conf.d/default.conf
and comment everything that is inside by prepending "#" to all of the lines.
restart NginX
[~]# /etc/init.d/nginx restart
Now the url points to the express page.
EDIT UPDATE: Better solution.
Open /etc/nginx/conf.d/default.conf find the location block and comment it by appending "#", and change specify the right location block pointing to node.js app. See code example below.
...
#access_log logs/host.access.log main;
# Comment this block
#location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
#}
# This is the changed location block
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-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;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
...
Now I remove the server block that I specified in /etc/nginx/nginx.conf
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;
# Remove from here
server {
listen 80;
server_name localhost;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-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;
}
}
# Remove till here
}
The directive in your configuration (shown below) loads the default server block. This is the reason why you see the nginx welcome page when you request localhost:80 on a newly installed instance of nginx
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;
You'll find that nginx is already listening on port 80 for localhost so your requests may never reach your newly defined reverse-proxy
Try disabling your default nginx server by opening /etc/nginx/conf.d/default.conf, commenting out the server block there and then restarting nginx

502 Bad Gateway in node + nginx proxy setup on Heroku

I'm using this buildpack to serve static files on Heroku with a node + nginx setup. While static assets are served properly, trying to serve content through node results in a 502 Bad Gateway. Node on its own works fine and so does nginx. The problem is when the two need to work together which I guess is because I haven't configured the nginx upstream settings right.
Here's my nginx conf:
worker_processes 1;
error_log /app/nginx/logs/error.log;
daemon off;
events {
worker_connections 1024;
}
http {
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
upstream node_conf {
server 127.0.0.1:<%= ENV['PORT'] %>;
keepalive 64;
}
server {
listen <%= ENV['PORT'] %>;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_redirect off;
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_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://node_conf;
}
location ~* ^.+\.(jpg|gif|png|ico|css|js|html|htm)$ {
root /app;
access_log off;
expires max;
}
location /static {
root /app;
index index.html index.htm;
}
}
}
.
My _static.cfg:
SERVER_TYPE="nginx"
BUILD_WEB_ASSETS="true"
.
My node server:
var app = require( 'express ')()
app.get( '/', function(req, res) { res.send( 'This is from Node.' ) })
app.listen( process.env.PORT )
.
I also have a sample html file in /static to test if nginx works:
<html>This is from nginx.</html>
.
With this config, appname.herokuapp.com should display "This is from Node." but instead I get the 502.
appname.herokuapp.com/static displays "This is from nginx" as it should, so no problems with nginx and static content.
I have tried every combination of values for upstream in nginx server settings but none have worked. What else can I try to make nginx proxy requests to node?
Here's my Heroku Procfile in case it helps: web: bin/start_nginx
I am not really familiar with Heroku, and pretty new to Nginx, but I'll give it a shot: To me it looks like the Nginx-config is saying that Nginx and the node.js app are using the same port (<%= ENV['PORT'] %>).
What you want is Nginx to listen to incoming connections (usually port 80), and have it forward them to the node.js app. Here is an example Nginx config:
# the IP(s) on which your node server is running. I chose port 4000.
upstream xxx.xxx.xxx.xxx { #Your IP adress as seen from the internet
server 127.0.0.1:4000; #Your local node.js process
}
# the nginx server instance
server {
listen 0.0.0.0:80; #Have Nginx listen for all incoming connections on port 80
server_name my-site;
access_log /var/log/nginx/my-site.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://xxx.xxx.xxx.xxx/; #Your IP adress as seen from the internet
proxy_redirect off;
}
}
This config is working for me on a webserver I am hosting in my living-room. Good luck!
Here's a README with information to get nginx and Node.js working together from a project I created a while back. Also included is an example nginx.conf.
As an overview, basically you're just creating sockets with Node then setting nginx to pipe those upstream. I commonly use this when I want to run multiple Node processes and have nginx stand in front of it.
It also includes working with socket.io out of the box, so you can use those to see how to configure your Node instance as well.

Resources